Is it possible to override parameter values when using Method Injection with Unity?

The reason I left it out, quite honestly, was development & test time, plus the fact that method injection is used much, much less than constructor or property injection. In particular, our test and doc teams were holding on by their fingernails trying to keep up with the stuff I was throwing them as it was There's no technical reason to leave it out. The Override objects are extensible, so it's perfectly reasonable to create your own to override method parameter values if you really need it.

The reason I left it out, quite honestly, was development & test time, plus the fact that method injection is used much, much less than constructor or property injection. In particular, our test and doc teams were holding on by their fingernails trying to keep up with the stuff I was throwing them as it was. There's no technical reason to leave it out.

The Override objects are extensible, so it's perfectly reasonable to create your own to override method parameter values if you really need it.

Thank you Chris! – Ian Warburton Aug 22 '10 at 9:04.

In DI frameworks, we usually have constructor injection or property injection. Constructor injection is when the framework constructing instances and automatically supplies instances matching the parameters of a constructor. Property injection is when, after the instance is created, any property, with a type supported by the container, is automatically set to an instance of that type.

Parameters are usually not supported with properties, thus PropertyOverride only makes sense with constructor injection. Update: method injection in Unity allows a method to be called on the instance passing in parameters to the method call: container. RegisterType( new InjectionMethod("InitializeMe", 42.0, new ResolvedParameter("SpecialLogger"))); The InjectionMethod class forces you to provide values for all the methods parameters.

Using a ParameterOverride does not make much sense in this case since we already have provided explicit values for the resolver to use. Note: a technical reason to why ParameterOverride only works with constructor parameters is that supporting overrides for methods have some problematic cases. Consider the following class: public class Foo { public Foo(IService service) { ... } public void Initialize(IService service) { ... } } container.

Resolve(new ParameterOverride("service", new Service())); Which parameter should be overridden? If you need to provide parameter values when resolving, I would suggest using factory delegates instead. Converting the sample above: container.

RegisterInstance>( number => { var dc = new DriveController(); dc. InitializeMe(number, container. Resolve("SpecialLogger")); return dc; }); var factory = container.

Resolve>(); var dc = factory(42); I have not tried this with Unity. I assume it will work, at least it shows that there should be alternatives to providing parameter values at "resolve time".

With Unity, in addition to the two types of injection you mentioned there is also 'Method Injection'. Msdn.microsoft. Com/en-us/library/… – Ian Warburton Aug 16 '10 at 18:40 @Ian - thanks!

One learns something new every day :) See my updated answer... – Peter Lillevold Aug 17 '10 at 9:21 "Using a ParameterOverride does not make much sense in this case since we already have provided explicit values for the resolver to use. " Couldn't the same argument be levelled at Constructor and Property parameter overrides which, of course, do exist? To put it another way, one can provide values for parameters when registering Constructors and Properties but these values can still be overridden at resolve time.

– Ian Warburton Aug 17 '10 at 16:42 @Ian - I'm sure the Unity team could've implemented the same support for methods, I'm only speculating in why they didn't. My theory is because when using method injection, you always provide values explicitly, and should thus never need to override those values. Constructor parameters, on the contrary, always have default values which is whatever type is matched in the container.

ParameterOverride provides a means to override those defaults. I also have a more technical theory as to why they only support constructor parameters, see my updated answer in a minute :) – Peter Lillevold Aug 17 '10 at 17:02 1 I agree as well. The Overrides stuff was added due to overwhelming user request, but in my own code I would absolutely use a separate factory.In fact, I deliberately made the override syntax messy / ugly, just to remind users that maybe this isn't a good idea.

;-) – Chris Tavares Aug 17 '10 at 21:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions