依赖注入和PostSharp [英] Dependency Injection and PostSharp

查看:98
本文介绍了依赖注入和PostSharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用PostSharp在我的应用程序中实现日志记录和安全方面. PostSharp使用属性装饰具有方面的方法,然后在编译时对其进行修改.

有谁知道我可以在这些方面使用依赖注入的方法吗?我目前正在使用静态服务定位器来解析来自容器的依赖关系,但它不像我想要的那么干净和分离.

有什么想法吗?

Hello everyone,

I''m using PostSharp to implement logging and security aspects in my application. PostSharp uses attributes to decorate methods with aspects which are then modified at compile time.

Does anyone know of a way that I can use dependency injection with these aspects? I''m currently using a static service locator to resolve the dependencies from the container but it''s not as clean and decoupled as I want.

Any ideas?

推荐答案

最近我在一本书中读到了有关依赖注入( Dependency Mark Seemann [ ^ ])在.NET中注入是您可能会感兴趣的一章(第9章).本章是关于拦截的.讨论了动态拦截,并与使用装饰器模式和PostSharp中的属性进行了比较.

动态拦截能够使用例如有关日志记录或异常处理的代码来动态包装装饰器的方法.装饰器上可以使用多个拦截器.我还没有进行足够的研究,以了解它对于应用程序的所有安全性方面是否足够.

可以在此处找到使用温莎城堡的示例: http://blog.ploeh.dk/2010/09/20/InstrumentationWithDecoratorsAndInterceptors.aspx [ ^ ]

并非所有的DI容器都支持动态拦截.温莎城堡(Castle Windsor),Spring.net和Unity(可能还有其他公司)都支持它.本书中有所有树形DI容器的示例以及拦截的用法.
In a book I recently read about dependency injection (Dependency Injection in .NET by Mark Seemann [^]) there is a chapter (chapter 9) that might interest you. The chapter is about interception. Dynamic interception is discussed and compared with using the decorator pattern and using attributes with PostSharp.

Dynamic interception is able to dynamically wrap the methods of a decorator with for instance code about logging or exception handling. Multiple interceptors can be used on a decorator. I haven''t studied it enough to know if it will be sufficient with all security aspects of an application.

An example using Castle Windsor can be found here: http://blog.ploeh.dk/2010/09/20/InstrumentationWithDecoratorsAndInterceptors.aspx[^]

Not all DI containers support dynamic interception. Castle Windsor, Spring.net and Unity (probably others too) support it. In the book there are examples for all tree DI containers andthe use with interception.


这篇关于依赖注入和PostSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆