DI: Injecting ActionFilterAttribute implementation (ASP.NET MVC)
- by Sosh
I was wondering if it is possible to inject a particular ActionFilterAttribute implementation using a IoC container.
For example, imagine you create a TransactionAttribute class [Transaction]
You use this to decorate action which should be wrapped in a transaction in the persistence layer. But implementation details of the attribute will be tied to the persistence tech you are using, but strictly speaking, your controller should not know about this, and you might want to swap this out.
What I would like to do, is define some kind of TransactionAttribute interface, and then have my IoC inject the correct implantation. So on my actions I only need specify:
[Transaction]
public ActionResult SomeAction()
{
....
}
...and the IoC will inject the correct implementation depending on config (eg. something like NHibernateTransactionAttribute, or SomeOtherORMTransactionAttribute).
Is this possible? Has anyone done it?