using Dectorators Chains with web.config

Jun 8, 2010 at 2:29 PM
Edited Jun 8, 2010 at 2:41 PM
I want to be able to use unity 2 with decorator chains and web.config, I have seen the microsft unity decorator example where everything is defined in the unity container within the code public interface IRepository<T, TID> { T Get(TID id); } public class RepositoryBase<T, TID> : IRepository<T, TID> { public T Get(TID id) { return default(T); } } public class LoggingRepositoryDecorator<T, TID> : IRepository<T, TID> { private IRepository<T, TID> _inner; public LoggingRepositoryDecorator(IRepository<T,TID> inner) { _inner = inner; } public T Get(TID id) { return _inner.Get(id); } } public void CreateADecoratedIRepository() { IUnityContainer container = new UnityContainer() .RegisterType(typeof(IRepository<,>), typeof(LoggingRepositoryDecorator<,>), "logging") .RegisterType(typeof(IRepository<,>), typeof(RepositoryBase<,>)); container.Resolve<IRepository<string, int>>("logging"); } but is there a way of doing it via a web.config (can you provide a working example please). So you can just ask unity to configure using the web.config and it will resolve the correct repository as specified in the web.config. Thus the container figures out how to build up the proper decorator chain based on any other repository decorators that have been registered. automatically. public void CreateADecoratedIRepository() { var section (UnityConfigurationSection).ConfigurationManage.GetSection("unity"); UnityConfigurationSection.CurrentSection.Configure(container); var respository = container.Resolve<IRepository<string, int>>("logging"); or var respository = container.Resolve<IRepository<Customer, int>>(); or other combination of generic type parameters }