Apr 8, 2012 at 9:12 AM
Edited Apr 8, 2012 at 9:13 PM
Yes, the dependency attribute is only applied against classes and not interfaces. I believe Unity mainly uses the interfaces for the type mappings and not for factories.
You can use a UnityContainerExtension to create the desired behavior. For the specific scenario above something like this should work:
public class InterfacePropertyContainerExtension : UnityContainerExtension
protected override void Initialize()
base.Context.Registering += new EventHandler<RegisterEventArgs>(this.OnRegister);
private void OnRegister(object sender, RegisterEventArgs e)
IUnityContainer container = sender as IUnityContainer;
foreach (PropertyInfo property in e.TypeFrom.GetProperties())
foreach (var attribute in property.GetCustomAttributes(typeof(DependencyAttribute), false))
var depAttr = attribute as DependencyAttribute;
InjectionProperty iProp =
Then you can resolve the CustomerRepository with the ConnectionString injected:
var container = new UnityContainer();
var repository = container.Resolve<IRepository>();
Now this is a simple example for the specific scenario above so probably doesn't cover every possible requirement but hopefully can help (if you want to go this route).
An alternative would be to create an abstract base class that implements the interface and have the concrete classes extend the abstract base class instead of implementing the interface.
Enterprise Library support engineer