Search code examples
c#dependency-injectionunity-containerprismcag

How to send a parameter to an object's constructor with Unity's Resolve<>() method?


Using Unity in Prism, I would like to send a parameter to the object's constructor like this:

PSEUDO-CODE:

SmartFormPresenter smartFormPresenter1 =
    this.container.Resolve<SmartFormPresenter(customer)>();

But instead I have to instatiate it and then assign a property:

SmartFormPresenter smartFormPresenter1 =
    this.container.Resolve<SmartFormPresenter>();
smartFormPresenter1.ObjectBeingEdited = customer;

Is there any way to send a parameter to the constructor directly?


Solution

  • Here's a related question that answers this pretty well: Can I pass constructor parameters to Unity's Resolve() method?

    The only option you have if you want to do this is a scoped container.

    IUnityContainer subContainer = this.container.CreateScopedContainer();
    subContainer.RegisterInstance<Customer>(customer);
    smartFormPresenter1 = subContainer.Resolve<SmartFormPresenter>();