Over the past two days, I have been struggling to learn something about StructureMap, using the old project as a concrete implementation example. I tried to simplify my question as much as possible. Although I will post my examples on vb.net, the answers with examples in C # are also in order.
The project includes interfaces called IDatabase, which connect to the database. The important part looks like this.
Public Interface IDatabase Function Connect(ByVal ConnectionSettings As ConnectionSettings) As Boolean ReadOnly Property ConnectionOpen As Boolean [... more functions...] End Interface Public Class MSSQLConnection Implements IDatabase Public Function Connect(ByVal ConnectionSettings As ConnectionSettings) As Boolean Implements IDatabase.Connect [... Implementation ...] End Function [... more implementations...] End Class
ConnectionSettings is a structure that has all the information needed to connect to a database.
I want to open a database connection once and use it for each individual connection in the project, so I am registering an instance in ObjectFactory.
dim foo = ObjectFactory.GetInstance(Of MSSQLConnection)() dim bar as ConnectionSettings foo.connect(bar) ObjectFactory.Configure(Sub(x) x.For(Of IDatabase).Use(foo))
Until this part, everything works like a charm. Now I get to the point where I have classes that need an extra database instance because they connect to the second database.
Public Class ExampleClass Public Sub New(ByVal SameOldDatabase as IDatabase, ByVal NewDatabase as IDatabase) [...] Magic happens here [...] End Sub End Class
I want this second database to behave the same as the first. I want it to use a specific single instance and would like to connect it to another database calling Connect with other ConnectionSettings.
The problem is that although I am pretty sure that this is possible (my initial idea was to register ExampleClass with alternative constructor arguments), I really want to do this without registering ExampleClass. This is probably due to the larger configuration, but I have no idea how to do this.
So, basically, it comes down to this question: How to configure ObjectFactory in such a way that auto-installation always calls a constructor with a Database1 object for the first database parameter and a Database2 object for the second (if there is one?)