Morning,
in the codebase i’m maintaining there is an old interface. Let’s call it IFoo. It pretty much became obsolete and replaced with the Interface INewFoo with a change a few weeks ago, but for backwards-compatibility purposes, i wrote a wrapper class which implements INewFoo and takes an IFoo in the constructor.
To clarify, consider the following code.
Public Interface IFoo
Sub DoStuff()
End Interface
Public Interface INewFoo
Sub DoPrettyMuchTheSameStuff()
End Interface
Public Class FooToNewFooWrapper
Implements INewFoo
Private _foo As IFoo
Public Sub New(foo As IFoo)
_foo = foo
End Sub
Public Sub DoPrettyMuchTheSameStuff() Implements INewFoo.DoPrettyMuchTheSameStuff
_foo.DoStuff()
End Sub
End Class
For both interfaces, the implementations are loaded by scanning a few assemblies with StructureMap.
Now, let’s get to the bad things. Most implementations for the old interface were put into forms for reason i can neither understand nor change. Because those tend to be displayed and disposed, i have to create a new instance every time i use ObjectFactory.GetAllInstances(Of IFoo). Thats still no problem, but i’d like to register a INewFoo-Wrapper for each registered implementation of IFoo, so that i can just use ObjectFactory.GetAllInstances(of INewFoo) and get all implementations of IFoo AND INewFoo.
I can’t iterate through the implementations of IFoo and register a wrapper for each one because as far as i can see, you can just register those with instances.
Wrong code below:
ObjectFactory.Configure(Sub(config)
config.Scan(Sub(scan)
For Each ass In assemblies
scan.Assembly(ass)
Next
scan.AddAllTypesOf(Of IFoo)()
End Sub)
End Sub)
Dim oldImplementations = ObjectFactory.GetAllInstances(Of IFoo)()
ObjectFactory.Configure(Sub(config)
For Each implementation In oldImplementations
Dim notIterated = implementation
config.For(Of INewFoo).Add(Function(x) New FooToNewFooWrapper(notIterated))
Next
End Sub)
My question is: Is it possible to register a wrapper for each implementation of IFoo which always creates a new instance of the implementation before creating a new instance of the wrapper?
Answers in C# and Vb.net are equally welcome.
Have you tried implementing a custom registration convention to carry out your specific requirement? Custom registration conventions allow very flexible scanning and registering
Configure structure map to use your scanner: