开发者

DI: Injecting ActionFilterAttribute implementation (ASP.NET MVC)

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, and 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 container inject the correct implementation. 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)开发者_Python百科.

Is this possible? Has anyone done it?


How do I use Windsor to inject dependencies into ActionFilterAttributes should give you an idea how to achieve this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜