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

The Desktop Developers Journal

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 » 194 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 » 528 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 » 290 views

VB.Net: Setter injection with StructureMap and the FI

by chrissie1


Permalink 22 Sep 2008 06:32 , Categories: Microsoft Technologies Tags: setterinjection, structuremap, vb.net

I’m using the SVN version for this.

Lets give an example of how to do this.

First we create a few interfaces.

  1. Public Interface IPerson
  2.     Function Name() As String
  3. End Interface
  1. Public Interface ITest
  2.     Function Name() As String
  3.     Property Person() As IPerson
  4. End Interface

as we can see ITest has a property Person of type IPerson. Ideally we would like to inject the IPerson.

Then we have the concrete classes.

  1. Public Class Person
  2.     Implements IPerson
  3.  
  4.     Public Function Name() As String Implements IPerson.Name
  5.         Return "Person"
  6.     End Function
  7. End Class
  1. Public Class Test
  2.     Implements ITest
  3.  
  4.     Private _Person As IPerson
  5.  
  6.     Public Sub New()
  7.  
  8.     End Sub
  9.  
  10.     Public Function Name() As String Implements ITest.Name
  11.         Return "test"
  12.     End Function
  13.  
  14.     Public Property Testprop() As IPerson Implements ITest.Person
  15.         Get
  16.             Return _Person
  17.         End Get
  18.         Set(ByVal value As IPerson)
  19.             _Person = value
  20.         End Set
  21.     End Property
  22. End Class

Now I want to inject the Person that I have already made with structureMap.

  1. Dim _Registry As StructureMap.Configuration.DSL.Registry
  2. _Registry.ForRequestedType(Of IPerson).TheDefault.Is.OfConcreteType(Of Person).WithName("default")
  3. _Registry.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test).WithName("default").SetterDependency(Of IPerson).Is(Function(e) e.TheDefault)
  4. Dim _Container As New StructureMap.IContainer(_Registry)

The first line makes a new registry.
The second line adds a Iperson to the registry with as concretetype Person that is also the default for IPerson with a name of default.
The third line adds an ITest to the registry with as concretetype Test that is also the default for ITest with a name of default and a setterdepency on IPerson that wants the default implementation of IPerson which we declared in line 2.
Line 4 creates the container.

Having done all this we can now see the result via console.writeline (our favorite testrunner ;-))

  1. Console.WriteLine(_container.GetInstance(Of ITest).Name)
  2.         Console.WriteLine(_container.GetInstance(Of ITest).Person.Name)
  3.         Console.WriteLine(_container.GetInstance(Of IPerson).Name)

This should give the following as a result.

test
Person
Person

Simple enough. Very usefull for when you want to inject your observable into a usercontrol. Usercontrols should have a default constructor for the designer, actually it is also the only constructor because the designer can have a difficult time otherwise. This is actually the only reason why I use Setterinjection instead of constructorinjection.

Leave a comment »Send a trackback » 173 views

VB.Net: A better configuration for structureMap

by chrissie1


Permalink 21 Sep 2008 01:19 , Categories: Microsoft Technologies, VB.NET Tags: structuremap, vb.net

There used to be time when you would configure StructureMap this way

  1. StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()

and then this to get your implementation

  1. ObjectFactory.GetInstance(Of ITest)()

Fine that all worked nice and dandy but it had a drawback. Structuremapconfiguration was static/shared. This meant that this.

  1. StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
  2. ObjectFactory.GetInstance(Of ITest)
  3. StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()
  4. ObjectFactory.GetInstance(Of ITest)

would lead to an error because you could not add classes to structuremap once objectfactory.getinstance was called. But Jeremy and his compadres (sorry musketeers) to the rescue. In the new svn version (not version 2.4.9 apparently) you can do this.

  1. #Region " Private members "
  2.         Imports TDB2007.Utils.CommonFunctions.IoC.Interfaces
  3. Imports StructureMap
  4.  
  5. Namespace IoC.StructureMapIoC
  6.     ‘'’ <summary>
  7.     ‘'’
  8.     ‘'’ </summary>
  9.     ‘'’ <remarks></remarks>
  10.     Public Class StructureMapContainer
  11.         Implements IoC.Interfaces.IContainerIoC
  12.  
  13. #Region " Private members "
  14. ‘'’ <summary>
  15.         ‘'’
  16.         ‘'’ </summary>
  17.         ‘'’ <remarks></remarks>
  18.         Private _Container As StructureMap.IContainer
  19.         ‘'’ <summary>
  20.         ‘'’
  21.         ‘'’ </summary>
  22.         ‘'’ <remarks></remarks>
  23.         Private _Registry As StructureMap.Configuration.DSL.Registry
  24. #End Region
  25.  
  26. #Region " Constructors "
  27.         ‘'’ <summary>
  28.         ‘'’
  29.         ‘'’ </summary>
  30.         ‘'’ <remarks></remarks>
  31.         Public Sub New()
  32.             _Registry = New StructureMap.Configuration.DSL.Registry
  33.             _Container = New StructureMap.Container(_Registry)
  34.         End Sub
  35. #End Region
  36.  
  37. #Region " Public methods "
  38.         ‘'’ <summary>
  39.         ‘'’
  40.         ‘'’ </summary>
  41.         ‘'’ <typeparam name="T"></typeparam>
  42.         ‘'’ <returns></returns>
  43.         ‘'’ <remarks></remarks>
  44.         Public Function Resolve(Of T)() As T Implements IContainerIoC.Resolve
  45.             Return _Container.GetInstance(Of T)()
  46.         End Function
  47.  
  48.         ‘'’ <summary>
  49.         ‘'’
  50.         ‘'’ </summary>
  51.         ‘'’ <typeparam name="T"></typeparam>
  52.         ‘'’ <param name="ImplamentationName"></param>
  53.         ‘'’ <returns></returns>
  54.         ‘'’ <remarks></remarks>
  55.         Public Function Resolve(Of T)(ByVal ImplamentationName As String) As T Implements IContainerIoC.Resolve
  56.             Return _Container.GetInstance(Of T)(ImplamentationName)
  57.         End Function
  58.  
  59.         ‘'’ <summary>
  60.         ‘'’
  61.         ‘'’ </summary>
  62.         ‘'’ <typeparam name="T"></typeparam>
  63.         ‘'’ <param name="Implemtation"></param>
  64.         ‘'’ <remarks></remarks>
  65.         Public Sub InjectImplementation(Of T)(ByVal Implemtation As T) Implements IContainerIoC.InjectImplementation
  66.             _Container.Inject(Of T)(Implemtation)
  67.         End Sub
  68.  
  69.         ‘'’ <summary>
  70.         ‘'’
  71.         ‘'’ </summary>
  72.         ‘'’ <typeparam name="T"></typeparam>
  73.         ‘'’ <param name="ImplementationName"></param>
  74.         ‘'’ <remarks></remarks>
  75.         Public Sub SetDefaultInstanceName(Of T)(ByVal ImplementationName As String) Implements IContainerIoC.SetDefaultInstanceName
  76.             _Container.SetDefault(GetType(T), ImplementationName)
  77.         End Sub
  78.  
  79.         ‘'’ <summary>
  80.         ‘'’
  81.         ‘'’ </summary>
  82.         ‘'’ <typeparam name="INPUTTYPE"></typeparam>
  83.         ‘'’ <typeparam name="CONCRETETYPE"></typeparam>
  84.         ‘'’ <param name="name"></param>
  85.         ‘'’ <param name="IsDefault"></param>
  86.         ‘'’ <remarks></remarks>
  87.         Public Sub Register(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.Register
  88.             If IsDefault Then
  89.                 _Registry.ForRequestedType(Of INPUTTYPE).TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name)
  90.             Else
  91.                 _Registry.BuildInstancesOf(Of INPUTTYPE).AddConcreteType(Of CONCRETETYPE)(name)
  92.             End If
  93.             _Container = New StructureMap.Container(_Registry)
  94.         End Sub
  95.  
  96.         ‘'’ <summary>
  97.         ‘'’
  98.         ‘'’ </summary>
  99.         ‘'’ <typeparam name="INPUTTYPE"></typeparam>
  100.         ‘'’ <typeparam name="CONCRETETYPE"></typeparam>
  101.         ‘'’ <param name="name"></param>
  102.         ‘'’ <param name="IsDefault"></param>
  103.         ‘'’ <remarks></remarks>
  104.         Public Sub RegisterSingleton(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.RegisterSingleton
  105.             If IsDefault Then
  106.                 _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name)
  107.             Else
  108.                 _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.AddConcreteType(Of CONCRETETYPE)(name)
  109.             End If
  110.             _Container = New StructureMap.Container(_Registry)
  111.         End Sub
  112.  
  113.         ‘'’ <summary>
  114.         ‘'’
  115.         ‘'’ </summary>
  116.         ‘'’ <remarks></remarks>
  117.         Public Sub ResetContainer() Implements IContainerIoC.ResetContainer
  118.             _Registry = New StructureMap.Configuration.DSL.Registry
  119.             _Container = New StructureMap.Container(_Registry)
  120.         End Sub
  121. #End Region
  122.  
  123.     End Class
  124. End Namespace

Or something equally brilliant. You can now add things to the container even after calling resolve.

You can find more of this Using the StructureMap Container independently of ObjectFactory by Jeremy and Comparing .NET DI (IoC) Frameworks, Part 2 by Andrey Shchekin

Leave a comment »Send a trackback » 289 views

:: Next >>