I am working on a project where the Unity framework is used as the IoC container. My question relates to injecting an optional dependency (in this case a logger) into several classes using property- or setter injection.
I do not want to clutter the constructors of all my classes with these optional dependencies, but I cannot find a good way to handle this in Unity. The way you would do this is, according to the MSDN documentation, by adding an attribute to the property:
private ILogger logger;
[Dependency]
public ILogger Logger
{
get { return logger; }
set { logger = value; }
}
I find this very ugly. In StructureMap one could do the following to set all properties of a given type:
SetAllProperties(policy => policy.OfType<ILog>());
Does anyone know if it is possible to do something similar in Unity?
Edit:
Kim Major suggests using this approach which can also be achieved through code.
I would be interested in examples of how to do this automatically for all matching properties.
I don't like those attributes also
You can do all using the Configure method of the unity container:
First register the type
unityContainer.RegisterType<MyInterface,MyImpl>(
new ContainerControlledLifetimeManager());
If you have multiple constructors you'll have to do this so Unity invokes the parameterless constructor (if none set Unity will go for the fattest one)
unityContainer.Configure<InjectedMembers>()
.ConfigureInjectionFor<MyImpl>(
new InjectionConstructor());
Setting property dependency
unityContainer.Configure<InjectedMembers>()
.ConfigureInjectionFor<MyImpl>(
new InjectionProperty(
"SomePropertyName",
new ResolvedParameter<MyOtherInterface>()));
Configuring method dependency
unityContainer.Configure<InjectedMembers>()
.ConfigureInjectionFor<MyImpl>(
new InjectionMethod(
"SomeMethodName",
new ResolvedParameter<YetAnotherInterface>()));