Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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?
Posted

1 solution

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.
 
Share this answer
 
Comments
jim lahey 4-Sep-12 8:09am    
Hi Martijn,

Thanks for the reply, but it's not quite what I'm after. We've already tried using dynamic interception but in order to be able to enforce security fully on our classes the interception needs to be done at the IL level so you can't get round it. Even with dynamic interception somebody else can take the assemblies and wire up the dependencies themselves or simply use a DI container that doesn't apply the interception, and therefore doesn't apply the security.
Martijn Kok 4-Sep-12 8:22am    
I haven't look at it that way. Dynamic interception won't solve this. Hopefully someoneelse knows how to use DI in this case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900