Click here to Skip to main content
13,054,615 members (68,360 online)
Rate this:
Please Sign up or sign in to vote.
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 3-Sep-12 21:49pm

1 solution

Rate this: bad
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:[^]

Not all DI containers support dynamic interception. Castle Windsor, and Unity (probably others too) support it. In the book there are examples for all tree DI containers andthe use with interception.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 4 Sep 2012
Copyright © CodeProject, 1999-2017
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