Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

Tags: vb.net

All the LessThanDot Journals

Sometimes I could hit myself.

by chrissie1


Permalink 21 Nov 2008 02:38 , Categories: Microsoft Technologies Tags: showdialog, vb.net, windows forms

Of course I would never hit a beautiful, charming and intelligent man like myself but I sometimes think about it. Especially if I do stupid things like this.

  1. Public Shadows Sub ShowDialog(ByVal Parent As System.Windows.Forms.Form) Implements Forms.Interfaces.Ifrm.ShowDialog
  2.             MyBase.ShowDialog(Me)
  3.         End Sub

You get this error if you do that.

System.ArgumentException: Form showDialog tried to set an ineligible form as its owner. Forms cannot own themselves or their owners.
Parameter name: owner

Makes perfect sense. So don’t ever do that again. The good thing is that there is an exception for this, so someone did this before.

1 comment »Send a trackback » 58 views

New features in Visual Basic 10

by chrissie1


Permalink 30 Oct 2008 05:55 , Categories: Microsoft Technologies Tags: happy, vb.net

YEEEEEEEEEEEEHA :D the spelling checker didn’t like that one), here are some of the new features in VB 10.
And yes, I did sound happy there, because some of these features are really not optional but much needed. Like the multi-line lambda and the sub lambda.

You can find the whole video on Channel9

Multi-line lambdas will look like this

  1. myfunction.lambdathing(Function(e) e.dosomething
  2.                                    e.dosomethingelse
  3.                        End Function)

Or like this

  1. myfunction.lambdathing(Sub() dosomething
  2.                              dosomethingelse
  3.                        End Sub)

Now this will also be possible. And I’m very happy about that.

  1. myfunction.lambdathing(Sub() dosomething)

They will also support auto properties and collection initializers, neither of which I really needed.

I’m so happy, I think I will stick with VB for a while. Although all the catching up to C# thing is annoying. I just hope it doesn’t become a VS 2011 or later version. Let’s make it a VS 2010 that came out in 2009 version shall we?

And here is some other stuff that should be possible.

  1. Private Sub SomeMethod()
  2.      Dim _counter as Integer = 1
  3.      AddHandler Button1.Click, Sub()
  4.                                       _counter += 1
  5.                                       TextBox1.text = _counter
  6.                                   End Sub
  1. Private Sub SomeMethodRunningInAnotherThread()
  2.      Me.Dispatcher.Invoke(Normal, Sub()
  3.                                       ‘Do some other stuff
  4.                                       SomeTextBox.Text = "Test"
  5.                                   End Sub)
  6.  End Sub

Info found after some random surfing on StackOverflow

2 comments »Send a trackback » 220 views

The law of demeter

by chrissie1


Permalink 14 Oct 2008 01:07 , Categories: Designing Software, Introduction to Architecture & Design Tags: lod, the law of demeter, vb.net

Introduction

If you have been using an OO language to program with then you should have heard of the Law of Demeter. If you haven’t then you should read some of the articles I collected on Google.


Definition

It is actually very simple to me. But explaining it can be a bit more daunting. This is what you can find on Wikipedia about it.

When applied to object-oriented programs, the Law of Demeter can be more precisely called the “Law of Demeter for Functions/Methods” (LoD-F). In this case, an object, A, can request a service (call a method) of an object instance, B, but object A cannot “reach through” object B to access yet another object, C, to request its services. Doing so would mean that object A implicitly requires greater knowledge of object B’s internal structure. Instead, B’s class should be modified, if necessary, so that object A can simply make the request directly of object B, and then let object B propagate the request to any relevant subcomponents. Or A should have a direct reference to object C and make the call directly. If the law is followed, only object B knows its own internal structure.

More formally, the Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects:

1. O itself
2. M’s parameters
3. Any objects created/instantiated within M
4. O’s direct component objects

In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as “use only one dot". That is, the code “a.b.Method()” breaks the law where “a.Method()” does not. There is disagreement as to the sufficiency of this approach.


My side of the story

Let’s oversimplify this a bit. When you see 2 dots in a statement, then you know something is going to be wrong. Look at this:

  1. Dim _Person as Person = New Person
  2. Console.WriteLine(_Person.Addresses.Count)

In that example _Person.Addresses.Count is a smell. Why? Because Addresses could be null and when it is null it can give a nullreferenceexception and crash our program.

There are 2 ways around this problem.

First.

  1. Dim _Person as Person = New Person
  2. Try
  3.    Console.WriteLine(_Person.Addresses.Count)
  4. Catch ex as NullReferenceExceptionException
  5.    Console.WriteLine(0)
  6. End Try

or

  1. Dim _Person as Person = New Person
  2. If _Person.Addresses IsNot Nothing then
  3.    Console.WriteLine(_Person.Addresses.Count)
  4. Else
  5.    Console.WriteLine(0)
  6. End If

What is wrong with this approach? Well every time you call Addresses.Count you have to check if Addresses isn’t null and print 0 instead. This gets boring very quickly and sometimes somebody might forget this little detail.

So we have a better solution. Namely, add a Method/Property to our Class Person. We name it CountAddresses for example and it would look something like this.

  1. Public Function CountAddresses() as Integer
  2.    If _Addresses IsNot Nothing Then
  3.       Return _Addresses.Count
  4.    Else
  5.       Return 0
  6.    End If
  7. End Function

Now we can call CountAdresses instead of Addresses.Count and not have to fear NullReferenceExceptions anymore.

The downside of this is that you have to write more code in your classes. But less code in your calling classes.

So I guess you should use it wisely.

When not

And when doesn’t the Law of Demeter apply?

  • Fluent interfaces (Unlimited amount of dots)
  • Factory classes (You can have 2 dots there)
  • Legacy systems (No laws apply in there)
Leave a comment »Send a trackback » 613 views

VB.Net: Rhino mocks 3.5 and Lambda expressions and AssertWasCalled not always working

by chrissie1


Permalink 07 Oct 2008 06:59 , Categories: Microsoft Technologies Tags: problem, rhino mocks, vb.net

There is no bug in Rhino mocks if that’s what you’re thinking. Rhino mocks 3.5 just uses things that VB.Net 9.0 can’t use and that can be frustrating. I wonder what possessed them to do it like that?

So let me tell the story.

I got a service and a repository, the service calls the repo.

Repo interface:

  1. Namespace Repository
  2.     Public Interface IRepository(Of T)
  3.         Function FindAll() As IList(Of T)
  4.         Sub Update(ByVal ObjectToUpdate As T)
  5.     End Interface
  6. End Namespace

The repo implementation:

  1. Imports System.Data.SqlClient
  2.  
  3. Namespace Repository
  4.     Public Class ProductRepository
  5.         Implements IRepository(Of Model.Product)
  6.  
  7.         ‘'’ <summary>
  8.         ‘'’ Here is where you would go to the database
  9.         ‘'’ </summary>
  10.         ‘'’ <returns>A list of Products</returns>
  11.         ‘'’ <remarks></remarks>
  12.         Public Function FindAll() As System.Collections.Generic.IList(Of Model.Product) Implements IRepository(Of Model.Product).FindAll
  13.             ‘Do something
  14.             Return Nothing
  15.         End Function
  16.  
  17.         Public Sub Update(ByVal ObjectToUpdate As Model.Product) Implements IRepository(Of Model.Product).Update
  18.             ‘Do something
  19.         End Sub
  20.     End Class
  21. End Namespace

The service interface:

  1. Namespace Service
  2.     Public Interface IService
  3.         Function FindAllProducts() As IList(Of Model.Product)
  4.         Sub Update(ByVal Product As Model.Product)
  5.     End Interface
  6. End Namespace

The service implementation:

  1. Namespace Service
  2.     Public Class Service
  3.         Implements IService
  4.  
  5.         Private _Repository As Repository.IRepository(Of Model.Product)
  6.  
  7.         Public Sub New(ByVal Repository As Repository.IRepository(Of Model.Product))
  8.             _Repository = Repository
  9.         End Sub
  10.  
  11.         Public Function FindAllProducts() As IList(Of Model.Product) Implements IService.FindAllProducts
  12.             Return _Repository.FindAll
  13.         End Function
  14.  
  15.         Public Sub Update(ByVal Product As Model.Product) Implements IService.Update
  16.             _Repository.Update(Product)
  17.         End Sub
  18.     End Class
  19. End Namespace

And now we want to test it.

First, we test the service’s findall method. We mock out the repo and see if the repo was called.

  1. <Test()> _
  2.         Public Sub Test_If_Service_Returns_A_List_Of_Products()
  3.             Dim _Repository As Repository.IRepository(Of Model.Product) = MockRepository.GenerateMock(Of Repository.IRepository(Of Model.Product))()
  4.             Dim _Service As Service.IService = New Service.Service(_Repository)
  5.             _Repository.Stub(Function(e) e.FindAll).IgnoreArguments.Return(New List(Of Model.Product))
  6.             Assert.IsNotNull(_Service.FindAllProducts)
  7.             _Repository.AssertWasCalled(Function(e) e.FindAll)
  8.         End Sub

This works just fine.

Now I want to see if the update method is also used as it should be, so I write this:

  1. <Test()> _
  2.         Public Sub Test_If_Service_Updates_A_Product()
  3.             Dim _Repository As Repository.IRepository(Of Model.Product) = MockRepository.GenerateMock(Of Repository.IRepository(Of Model.Product))()
  4.             Dim _Service As Service.IService = New Service.Service(_Repository)
  5.             _Service.Update(Nothing)
  6.             _Repository.AssertWasCalled(Function(e) e.Update(Nothing))
  7.         End Sub

But that gives me an error “Expression does not produce a value.". Mmmm, you can find an explanation for that here. And the solutions is there as well. The solution is, however, a bit clunky, as he says.

So in the end you could do this:

  1. <Test()> _
  2.         Public Sub Test_If_Service_Updates_A_Product()
  3.             Dim _Repository As Repository.IRepository(Of Model.Product) = MockRepository.GenerateMock(Of Repository.IRepository(Of Model.Product))()
  4.             Dim _Service As Service.IService = New Service.Service(_Repository)
  5.             _Service.Update(Nothing)
  6.             _Repository.AssertWasCalled(Function(e) wrap(e))
  7.         End Sub
  8.  
  9.         Function wrap(ByRef repo As Repository.IRepository(Of Model.Product)) As Object
  10.             repo.Update(Nothing)
  11.             Return Nothing
  12.         End Function

Which solve the problem, more or less.

Leave a comment »Send a trackback » 546 views

VB.Net: Rhino Mocks and the AAA syntax

by chrissie1


Permalink 06 Oct 2008 04:44 , Categories: Microsoft Technologies Tags: ayende, rhino mocks, vb.net

So, last week Ayende made a new version of Rhino Mocks available, namely version 3.5. He made a post on what is new and shiny, but I would like to look at what the new AAA syntax has in store for us VB.Net users.

And I want to tell you how it used to look and how it looks now.

This is the old version, works with Rhino mocks 3.4.

  1. Imports NUnit.Framework
  2. Imports Rhino.Mocks
  3.  
  4. Namespace AnalysisManagement
  5.     <TestFixture()> _
  6.     <Category("ModelServices")> _
  7.     <Category("ModelServices.AnalysisManagement")> _
  8.     Public Class TestDAOFill
  9.  
  10. #Region " Private members "
  11.         Private _Mocker As MockRepository
  12.         Private _Factory As DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory
  13.         Private _Fill As ModelServices.AnalysisManagement.Interfaces.IFill
  14. #End Region
  15.  
  16. #Region " Setup "
  17.         <SetUp()> _
  18.         Public Sub Setup()
  19.             _Mocker = New MockRepository()
  20.             _Factory = _Mocker.DynamicMock(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
  21.             _MspFactory = _Mocker.DynamicMock(Of DataAccessLayer.MSP.Crud.Factory.IDAOFactory)()
  22.             _Fill = New ModelServices.AnalysisManagement.DAOFill(_Factory, _MspFactory)
  23.         End Sub
  24.  
  25.         <TearDown()> _
  26.         Public Sub TearDown()
  27.             _Mocker.ReplayAll()
  28.             <em>_Mocker.VerifyAll()</em>
  29.         End Sub
  30. #End Region
  31.  
  32. #Region " Tests "
  33. <Test()> _
  34.         Public Sub FindAllFiberFluorescenceColorN21s()
  35.             Dim _CrudIFiberFluorescenceColorN21 As DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21 = _Mocker.DynamicMock(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
  36.             <em>Expect.Call</em>(_Factory.CrudFiberFluorescenceColorN21).IgnoreArguments.Return(_CrudIFiberFluorescenceColorN21)
  37.             <em>Expect.Call</em>(_CrudIFiberFluorescenceColorN21.FindAll).IgnoreArguments.Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
  38.             <em>_Mocker.ReplayAll()</em>
  39.             Dim _templist As IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21) = _Fill.FindAllFiberFluorescenceColorN21s
  40.             Assert.IsNotNull(_templist)
  41.             Assert.IsInstanceOfType(GetType(IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21)), _templist)
  42.         End Sub
  43. #End Region
  44. End Class
  45. End Namespace

Ok, so we notice the use of _mocker and DynamicMock to create our Mock objects and then we set our expectations, then we do a replayall and execute the methods under test. When the test is done the verifyall is executed which looks if our expectations are met.

Now we switch to Rhino mocks 3.5 and see that we get an error on Expect saying that the signature is not correct. No worry this is because it is choosing the wrong Expect. It is namely trying to use the extension method there. Just add Rhino.Mocks. before the Expect and all is well again. Look how the imports doesn’t do the same thing.

And now on to the new.

  1. Imports NUnit.Framework
  2. Imports Rhino.Mocks
  3.  
  4. Namespace AnalysisManagement
  5.     <TestFixture()> _
  6.     <Category("ModelServices")> _
  7.     <Category("ModelServices.AnalysisManagement")> _
  8.     Public Class TestDAOFill
  9.  
  10. #Region " Private members "
  11.         Private _Factory As DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory
  12.         Private _Fill As ModelServices.AnalysisManagement.Interfaces.IFill
  13. #End Region
  14.  
  15. #Region " Setup "
  16.         <SetUp()> _
  17.         Public Sub Setup()
  18.             _Factory = <em>MockRepository.GenerateMock</em>(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
  19.             _MspFactory = <em>MockRepository.GenerateMock</em>(Of DataAccessLayer.MSP.Crud.Factory.IDAOFactory)()
  20.             _Fill = New ModelServices.AnalysisManagement.DAOFill(_Factory, _MspFactory)
  21.         End Sub
  22.  
  23.         <TearDown()> _
  24.         Public Sub TearDown()
  25.      
  26.         End Sub
  27. #End Region
  28.  
  29. #Region " Tests "
  30. <Test()> _
  31.         Public Sub FindAllFiberFluorescenceColorN21s()
  32.             Dim _CrudIFiberFluorescenceColorN21 As DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21 = <em>MockRepository.GenerateMock</em>(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
  33.             _CrudIFiberFluorescenceColorN21.<em>Stub</em>(Function(e) e.FindAll()).Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
  34.             _Factory.<em>Stub</em>(Function(e) e.CrudFiberFluorescenceColorN21).Return(_CrudIFiberFluorescenceColorN21)
  35.             Dim _templist As IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21) = _Fill.FindAllFiberFluorescenceColorN21s
  36.             Assert.IsNotNull(_templist)
  37.             Assert.IsInstanceOfType(GetType(IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21)), _templist)
  38.         End Sub
  39. #End Region
  40. End Class
  41. End Namespace

Look at how the _mocker.DynamicMock are replaced by MockRepository.GenerateMock and how the _mocker object has disappeared all together. The Expect.Call’s have been replaced with Object.Stub methods. And the ReplayAll and VerifyAll are now gone. Overall, a bit simpler and more meaningful.

Here is another good article about the use of AAA and Rhino mocks but then in C#.

And it only goes to show that even the smartest and best people don’t get it right the first time ;D.

Ayende asked to add this to his wiki and so I did http://ayende.com/wiki/Rhino+mocks+3.5+and+VB.Net+the+AAA+syntax.ashx

Leave a comment »Send a trackback » 303 views

:: Next >>