How can I pass arguments to a constructor in an IOC-framework? I want to do something like: (Trying to be IOC-framework agnostic ;) )
object objectToLogFor = xxx;
container.Resolve<ILogging>(objectToLogFor);
public class MyLogging : ILogging
{
public MyLogging(object objectToLogFor){}
}
It seems that this is not possible in StructureMap. But I would love to see someone prove me wrong.
Are other frameworks more feature-rich? Or am I using the IOC-framework in the wrong way?
In structure map you could achieve this using the With method:
string objectToLogFor = "PolicyName";
ObjectFactory.With<string>(objectToLogFor).GetInstance<ILogging>();
See: http://codebetter.com/blogs/jeremy.miller/archive/2008/09/25/using-structuremap-2-5-to-inject-your-entity-objects-into-services.aspx
For Castle Windsor:
var foo = "foo";
var service = this.container.Resolve<TContract>(new { constructorArg1 = foo });
note the use of an anonymous object to specify constructor arguments.
using StructureMap:
var foo = "foo";
var service = container.With(foo).GetInstance<TContract>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With