Is it possible to have independent parameters with constructor injection?

Sep 25, 2008 at 3:30 PM
I am wondering if I can use a specific instance of other objects with constuctor injection.

For example

my class's constructor is

public myClass(IDependentSomething objA, SomethingElse objB)

I want objA to be set by unity but I want to pass the value for objB.  Something like

SomethingElse temp = new SomethingElse();
container.Resolve<myClass>(temp)

I know the above doesn't work but is there another way to make that work?  I saw this post which shows how to pass value types 

http://www.codeplex.com/unity/Thread/View.aspx?ThreadId=32205

but I want to pass an object and I don't want its value to be declared in the configuration.  Any ideas?
Sep 26, 2008 at 11:38 AM
Sounds like what you are looking for is RegisterInstance.  An excerpt from the documentation follows:

EmailService myEmailService = new EmailService();
myContainer.RegisterInstance<IMyService>(myEmailService);

If this is not what your looking for I think you'll find the "Setting Up the Unity Container" section under "Key Scenarios" in the Unity documentation very interesting reading - it has numerous scenarios to include the above (I copied the code from it).

Bill
Sep 26, 2008 at 3:12 PM
That's it exactly.  Don't know how I missed it (ok, yes I do...I'm a bit of a noob :)

Thanks for your help....again!