Passing constructor arguments when using StructureMap

Posted by Mosh on Stack Overflow See other posts from Stack Overflow or by Mosh
Published on 2010-05-13T02:17:16Z Indexed on 2010/05/13 2:24 UTC
Read the original article Hit count: 351

Hello,

I'm using StructureMap for my DI. Imagine I have a class that takes 1 argument like:

public class ProductProvider : IProductProvider
{
     public ProductProvider(string connectionString)
     { 
         ....
     }
}

I need to specify the "connectionString at run-time when I get an instance of IProductProvider.

I have configured StructureMap as follows:

ForRequestedType<IProductProvider>.TheDefault.Is.OfConcreteType<ProductProvider>().  
WithCtorArgument("connectionString");

However, I don't want to call EqualTo("something...") method here as I need some facility to dynamically specify this value at run-time.

My question is: how can I get an instance of IProductProvider by using ObjectFactory?

Currently, I have something like:

ObjectFactory.GetInstance<IProductProvider>();  

But as you know, this doesn't work...

Any advice would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about dependency-injection

Related posts about structuremap