Creating Objects With Injected and Non-Injected Constructor Parameters

Aug 20, 2009 at 1:19 AM

Hello All,

The following is not legal, but I belive it communicates what I would like to do:

class Foo : IFoo { Foo(IMyInterface myInterface, string name, byte address) {...} ...}

IFoo foo = container.Resolve<IFoo>("bar", 42);

Assume IFoo and IMyInterface are registered with the container.

Is it possible to accomplish the intent the pseudocode above conveys?

Thanks,

Dave

 

Aug 20, 2009 at 4:17 AM

Not at this time. Well, you could do it with child containers and a specific type registration, but it's a lot more trouble than it's worth.

I'm actually going to start working seriously on this feature in the next week or two. It probably won't be quite that straightforward (lots of ambiguities there) but it should be at least possible.

 

Aug 21, 2009 at 3:14 PM

Hi Chris, would it look something like AutoFac's delegate factories?  That seems like a nice way to mix resolving dependencies from the container and being able to supply them.  

http://code.google.com/p/autofac/wiki/DelegateFactories

Aug 24, 2009 at 6:41 PM

I was planning on something more similar to AutoFac's Parameter objects, actually. Although the delegate factories idea is a very interesting one, I expect the people screaming for the "pass parameters to resolve" feature don't want explicit factory objects.

 

Sep 4, 2009 at 8:05 PM

Hi

I need such behaviour quite often. What about its worth, problem is if I need create an object with additional runtime parameters I can't do that via Unity, and I have the strange situation - my objects instantiated in two different ways.

Now I use my own dependency injection container based on ObjectBuilder1 and I've implemented such feature for it.

At the moment I'm going to start use Unity as a DI container, and found out following problems.

In order to implement this feature I have to have a method like Resolve<T>(params object[] arguments). The only one way is make a class inherits UnityContainer, am I right?

As far as I understand, extensions must not contains methods with Resolve semantic in your idea

I can inherit UnityContainer class, but I can't extend IUnityContainer interface with the new method.

 

So, it seems that I have to make my own DI container based on ObjectBuilder2 :), because Unity doesn't allow me to do what I need.

Thanks in advance for any other ideas.

Sep 5, 2009 at 5:51 PM

I implemented this last week; it'll be in Tuesday's drop (normally Monday, but it's the holiday).

It's a little less straightforward, but you have the ability to replace constructor parameters anywhere down the object grap, or to completely replace the value for a dependency across the tree. So, taking the class in the original post, you can do this:

container.Resolve<IFoo>(new ParameterOverrides<Foo> { { "name", "bar" }, { "address", 42 } });

Then, when resolving Foo it'll pass "bar" for the name parameter, 42 for the address parameter, and any other ones will be resolved through the container.

 

Sep 10, 2009 at 1:59 PM

Where can I get these changes? I have not found.

thanks

Sep 10, 2009 at 8:11 PM

The drop got a little delayed by other work. Working on it now.

-Chris

Sep 17, 2009 at 7:52 PM
Edited Sep 18, 2009 at 8:41 PM

Hi Chris,

"container.Resolve<IFoo>(new ParameterOverrides<Foo> { { "name", "bar" }, { "address", 42 } });"

This is great news and will seemingly solve a major short coming of the framework.
Will we also be able to pass in already instantiated objects?

Is there a new ETA on the drop?

Thanks!

Joe

 

 

 

Mar 11, 2010 at 9:55 PM
Edited Mar 11, 2010 at 9:59 PM

binding by magic string will make this easy to break.

although it is needed for the original goal of:
 "replace constructor parameters anywhere down the object grap"

order is removed from equation

 

container.Resolve<IFoo>(new ParameterOverrides<Foo> {  "bar",  42 });

resolve by type and required to be in correct order as defined

 

container.Resolve<IFoo>(new ParameterOverrides<Foo> {  "bar",  42, new ParameterUseRegistered(),  new ParameterUseRegistered(), 55 });

order will be required and not all parameters need to be passed in.

 

both could be supported if:

container.ResolveParameterOverridesStrategy = new OverrideByTypeAndOrderStrategy();

container.ResolveParameterOverridesStrategy = new OverrideByMagicStringStrategy();

was added to container.

 

what do you all think?

 

 

 

Mar 12, 2010 at 3:11 PM

Hi All,

How to solve namespace problem?

Error 2 The type or namespace name 'ParameterOverrides' could not be found (are you missing a using directive or an assembly reference?)

Thanks!

 

Mar 12, 2010 at 7:33 PM

today i downloaded from head of repository, entlib-62971, and do not see the overloads to resolve to accomplish this anymore.

Is this feature no longer planned for 5.0 ?

Mar 12, 2010 at 11:35 PM

Why are you downloading it from the entlib repository? The code for Unity is here, not there.

It's also included in our beta 1 releases. Source control hasn't been updated for a while since we've been working to get the binary releases out (which include the source as well).