I started doing more and more work with Unity. I notice that Resolver method takes a params argument ResolverOverride.
Can someone give me an example how I can use ResolverOverride or point me some other source where I can get more clues.
Just to add my 2c. You can just add a ParameterOverrides like so:
Container.Resolve<IConcreteService>(new ParameterOverrides
{
{"val", 42}
});
Just in case someone is interested, I have made an extension method that makes the syntax for resolving using ParameterOverride
a little easier to read. The method is as follows:
public static class UnityExtensions
{
public static T Resolve<T>(this IUnityContainer container, object parameterOverrides)
{
var properties = parameterOverrides
.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var overridesArray = properties
.Select(p => new ParameterOverride(p.Name, p.GetValue(parameterOverrides, null)))
.Cast<ResolverOverride>()
.ToArray();
return container.Resolve<T>(null, overridesArray); //null needed to avoid a StackOverflow :)
}
}
With this, you can rewrite the ParameterOverride example as follows:
var service = container.Resolve<IConcreteService>(new {val=3});
I hope this is useful for someone...
As you have noticed this is a new (and really cool feature) of the Unity 2. This feature let you
There is
ParameterOverride : ResolverOverride
DependencyOverride : ResolverOverride
PropertyOverride : ResolverOverride
Example
Have no idea why does Google keeps silent about that.
Quotes are from Unity source code xml docs.