I use Simple Injector as my IoC container. SimpleInjector uses this simple technique to handle mixed life style for Per Thread and Per Web Request
container.RegisterPerWebRequest<IWebUnitOfWork, UnitOfWork>();
container.RegisterLifetimeScope<IThreadUnitOfWork, UnitOfWork>();
container.Register<IUnitOfWork>(() => container.GetInstance<UnitOfWork>());
// Register as hybrid PerWebRequest / PerLifetimeScope.
container.Register<UnitOfWork>(() =>
{
if (HttpContext.Current != null)
return container.GetInstance<IWebUnitOfWork>() as UnitOfWork;
else
return container.GetInstance<IThreadUnitOfWork>() as UnitOfWork;
});
I wish to decorate the interface IUnitOfWork
with classes such as UnitOfWorkAuthoriseDecorator
, UnitOfWorkExceptionDecorator
& UnitOfWorkTraceDecorator
.
What should I decorate?
IUnitOfWork
IWebUnitOfWork
& IThreadUnitOfWork
IUnitOfWork
& IWebUnitOfWork
& IThreadUnitOfWork
UnitOfWork
It seems most intuitive to me to decorate the IUnitOfWork
instance:
container.RegisterDecorator(typeof(IUnitOfWork),
typeof(UnitOfWorkAuthoriseDecorator));
container.RegisterDecorator(typeof(IUnitOfWork),
typeof(UnitOfWorkExceptionDecorator));
container.RegisterDecorator(typeof(IUnitOfWork),
typeof(UnitOfWorkTraceDecorator));
Decorating anything else would probably be rather awkward, since when decorating multiple interfaces, you need to have a decorator per interface (and in your case 3 decorators per interface).
Decorating the UnitOfWork
class is possible, but probably very inconvenient, since you will have to make all members of this class virtual and override them in the decorators, since those decorators need to inherit from UnitOfWork
.