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.