views:

400

answers:

2

I am trying to use StructureMap and have essentially 3 levels of abstraction. I have a service a repository and database interface. So the IService depends on IRepo and IRepo depends on IDatabase. My issue is that my IDatabase concrete type takes in db connection information. I am going to create these on the fly, trying to use ObjectFactory.Configure().

So I have a user and the user is linked to a company and the I lookup the company connection information and I want to create the proper IDatabase concrete type based on this information. I have the concrete type configured in my startup code. I only want to use the IService object from outside...so I want to create a named IService object named with the company Name. Having issues with trying to create these types on the fly. ANY information would be helpfull...

I basically want to be able to create a named instance and either set properties or pass specific args to a constructor at runtime, without knowing the concrete type. I have the concrete type setup in the config file. I have tried to use the ObjectFactory.GetInstance and tried to set properties inside the Configure method, but got StackOverflow exception...HOW IRONIC

A: 

Yes, I've been having similar issues... it seems very poorly documented for using named objects/profiles. There is a google group; posting there might help. When I get time to tidy up my own question, I'll probably add a bounty...

Marc Gravell
I saw that you had asked a question about StructuredMap...I almost sent you an email directly..but figured you had better things to do..:)
CSharpAtl
A: 

Found the answer with direction from Jeremy Miller (author of StructureMap). Here is where he pointed me to:

http://structuremap.sourceforge.net/RetrievingServices.htm#section5

here is an example of what I used:

IDatabaseRepository repo =
                ObjectFactory.With("server").EqualTo("servername").
                With("database").EqualTo("dbName").
                With("user").EqualTo("userName").
                With("password").EqualTo("password").
                GetInstance<IDatabaseRepository>();
CSharpAtl