Having one construction parameter injected and another not injected, is it possible?

Feb 16, 2011 at 8:26 PM

Hi everyone...

I'm working on MVC 3 and Unity, actually my controllers are resolving a service, these services have two parameters, the first one can be injected with no problem but the second one is an object that depends on the controller (ModelState).

Here is a code sample:

 

INotificationService service = UnityProvider.Instance.Resolve<INotificationService>(
                                                                    new ParameterOverride("repository", UnityProvider.Instance.Resolve<INotificationRepository>()),
                                                                    new ParameterOverride("modelstate", new ModelStateWrapper(this.ModelState)));

Is it possible to send just the second Parameter and let Unity resolve the first one by dependencies?

 

Thanks.

Feb 17, 2011 at 12:17 AM
Edited Feb 17, 2011 at 12:32 AM

Yes.  Make sure you configured the dependencies that will be used based on the injection(s) you specified upon registering the type.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 17, 2011 at 12:54 PM

So if I configure my first dependency, then just send the second parameters as ParameterOverride should work?

Thanks!

Feb 17, 2011 at 3:14 PM

Short answer is yes.  You can try it out, try removing the one ParameterOverride in your call to Resolve<INotificationService>

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 17, 2011 at 4:55 PM
Thanks! I tried it and works perfectly.
I defined both the dependency and the second parameter without a value and worked.

Thanks!


From: [email removed]
To: [email removed]
Date: Thu, 17 Feb 2011 07:14:58 -0800
Subject: Re: Having one construction parameter injected and another not injected, is it possible? [unity:246350]

From: AvanadeSupport
Short answer is yes. You can try it out, try removing the one ParameterOverride in your call to Resolve<INotificationService>

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com
Read the full discussion online.
To add a post to this discussion, reply to this email (unity@discussions.codeplex.com)
To start a new discussion for this project, email unity@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com