views:

640

answers:

5

I'm about to put my head thru this sliding glass door. I can't figure out how to execute the following code in VB.NET to save my life.

private static void InitStructureMap()
    {
        ObjectFactory.Initialize(x =>
                                     {
                                         x.AddRegistry(new DataAccessRegistry());
                                         x.AddRegistry(new CoreRegistry());
                                         x.AddRegistry(new WebUIRegistry());

                                         x.Scan(scanner =>
                                                    {
                                                        scanner.Assembly("RPMWare.Core");
                                                        scanner.Assembly("RPMWare.Core.DataAccess");
                                                        scanner.WithDefaultConventions();
                                                    });
                                     });
    }
+1  A: 

My VB.NET isn't up to scratch, so I can't help you with the code directly. What I can tell you, however, is how to do it yourself and it's a doozy. Basically, you need to use Reflector to read the executable that contains this code in - and then you can choose to output it as VB.NET - how cool is that, and this trick works both ways.

Pete OHanlon
A: 

That's awesome, for whatever reason though, it doesn't compile. Ugh.

Here's what it came up with though:

Private Shared Sub InitStructureMap()
ObjectFactory.Initialize(Function (ByVal x As IInitializationExpression) 
    x.AddRegistry(New DataAccessRegistry)
    x.AddRegistry(New CoreRegistry)
    x.AddRegistry(New WebUIRegistry)
    x.Scan(Function (ByVal scanner As IAssemblyScanner) 
        scanner.Assembly("RPMWare.Core")
        scanner.Assembly("RPMWare.Core.DataAccess")
        scanner.WithDefaultConventions
    End Function)
End Function)
End Sub

And I tried adding the _ to make it one line (that didn't work either)

 Private Shared Sub InitStructureMap()
ObjectFactory.Initialize(Function (ByVal x As IInitializationExpression) _
    x.AddRegistry(New DataAccessRegistry) _
    x.AddRegistry(New CoreRegistry) _
    x.AddRegistry(New WebUIRegistry) _
    x.Scan(Function (ByVal scanner As IAssemblyScanner) _
    scanner.Assembly("RPMWare.Core") _
    scanner.Assembly("RPMWare.Core.DataAccess") _
    scanner.WithDefaultConventions() _
    End Function) _
End Function) 
End Sub

Anyone else hate their legacy apps? :P

Kyle West
+7  A: 

At the moment, it's simply not possible. The current version of VB does not support multiline (or statement) lambdas. Each lambda can only comprise one single expression. The next version of VB will fix that (there simply wasn't enough time in the last release).

In the meantime, you'll have to make do with a delegate:

Private Shared Sub Foobar(x As IInitializationExpression)
    x.AddRegistry(New DataAccessRegistry)
    x.AddRegistry(New CoreRegistry)
    x.AddRegistry(New WebUIRegistry)
    x.Scan(AddressOf Barfoo)
End Sub

Private Shared Sub Barfoo(ByVal scanner As IAssemblyScanner) 
    scanner.Assembly("RPMWare.Core")
    scanner.Assembly("RPMWare.Core.DataAccess")
    scanner.WithDefaultConventions
End Sub

' … '
ObjectFactory.Initialize(AddressOf Foobar)
Konrad Rudolph
thanks for the answer. It works and I marked it as so. A quick follow-up. I'm getting a warning that not all code paths return a value. Any way to avoid that?
Kyle West
Change the Function to a Sub if it doesn't have a return value. If it does have a return value, make sure it returns values in *all* code paths and all cases.
configurator
@Kyle: sorry. The code actually shouldn't have compiled (check `Option Strict On` in your options and always use it!) because the functions lacked return values – which calls for the use in `Sub` in VB, as “configurator” correctly suggested.
Konrad Rudolph
thanks. I would turn strict on, but then I'd spend the next couple days getting the project to build again. Luckily it is a small and shirking amount of our total solution in this VB.NET project. No clue why we started in VB in the first place. Someone had too much kool-aid.
Kyle West
@Kyle: Of course, I can’t judge your special case but getting the project cleaned up *might* actually be worth the pain because it eases it later on.
Konrad Rudolph
Barfoo should be Shared, not Static.
DoniG
@DoniG: Thanks. I definitely write too much C# of late. ;-)
Konrad Rudolph
A: 

I tried Konrad's solution, however it does not work for me. Here is my code:

Public Sub BootstrapStructureMap()
        ObjectFactory.Initialize(AddressOf InitMannequins)
        ObjectFactory.AssertConfigurationIsValid()
End Sub

Sub InitMannequins(ByVal x As IInitializationExpression)
        ' this works
        'x.FillAllPropertiesOfType(Of IPDFLayout).TheDefault.Is.OfConcreteType(Of USCanadaLayout)()
        ' this doesn't
        x.AddRegistry(New MannequinRegistry)
End Sub

Any idea what is going on?

Thank you

You could start by posting the error message your compiler gives.
Konrad Rudolph
Thanks. However I figured it out. Your code posted above works fine. I was doing some wrong.