Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 3-Sep-12 22:49pm
jim lahey24.9K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
jim lahey at 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 at 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)

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 /\jmot 175
4 Shweta N Mishra 161
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100