How to convert C# StructureMap initialization to VB.NET?
-
21-08-2019 - |
Question
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();
});
});
}
Solution
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)
OTHER TIPS
You can see another approach at:
http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/structuremap-is-way-cool-even-in-vb-net
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.
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