DI: Injecting ActionFilterAttribute implementation (ASP.NET MVC)

Posted by Sosh on Stack Overflow See other posts from Stack Overflow or by Sosh
Published on 2010-05-16T08:22:21Z Indexed on 2010/05/16 8:30 UTC
Read the original article Hit count: 192

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?

© Stack Overflow or respective owner

Related posts about attributes

Related posts about c#