Passing constructor arguments with StructureMap Adapter

Sep 10, 2009 at 6:06 AM
Edited Sep 10, 2009 at 6:07 AM


So we are using the structure map adapter and I am trying to get an instance of one of my registered types, which is a domain layer wrapper of a DAO object,  so I want to pass an instance of the DAO object into the constructor

My wrapper class looks something like this:

public class WrapperClass
    public WrapperClass(var someHiddenInternalObject)

        if(someHiddenInternalObject == null)

            throw new ArgumentNullException();

        SomeHiddenInternalObject = someHiddenInternalObject;

    internal var SomeHiddenInternalObject { get; set; }


So then I want an instance of my wrapper class:


var newInstance = ServiceLocator.Current.GetInstance<IWrapperClass>();


of course I get an argumentNull exception. I know when using structure map directly you can call:


var newInstance = ObjectFactory.With(someHiddenInternalObject).GetInstance<IWrapperClass>();


but I can't find the equivalent with the practices library....

Any help much appreciated... even if it's just a point to the docs... is there any? I can't find it.


Sep 22, 2009 at 6:21 AM

Passing parameters to the constructor is a capability that we explicitly excluded from the interface, as not every container supports it. If you need that, you've gone beyond the lowest common denominator we've defined.

The workaround would be to create and register a factory for IWrapperClass, which provides a factory method which can take the extra parameters and construct the real object. You'd then resolve the factory from the container and use it to get the IWrapperClass instead of getting IWrapperClass directly.

Is this for an application you're writing or a library you're going to be sharing? If it's for an application, don't bother with IServiceLocator; just call StructureMap directly. The intended usage of this project was for library authors who don't want to force their choice of DI containers on their consumers. Your reference to DAO and data layers sounds more like you're building an app. Unless you expect to actually remove StructureMap from your application, I wouldn't bother with the abstraction.


Sep 25, 2009 at 7:42 AM

Ok thanks for that.

Yes I am writing an application not a library and the decision to use the ServiceLocator was not mine but made by some higer ups :)

I think the rationale was just to keep it as generic as possible  so if we wanted to we could replace OUR DI container . As to weather I need this functionality? Probably not there's always another way.

Thanks for the help

Sep 13, 2012 at 9:16 AM

I have a class with parameter in a constructor
class is Export by Interface in prism application -> [Export(typeof(interface))]

But I can't get this instance by ServiceLocator;

newInstance = ServiceLocator.Current.GetInstance<Interface>(my parameter); ???????


When remove parameter in a constructor my getinstance it's ok.

how will we do it?