tags:

views:

504

answers:

5

What are the best/most popular ways to do aspect-oriented programming (AOP) in C#/.Net?

A: 

I've played around with rolling my own, for several different types of things. I've had some luck. In general, I make an interface, implement it with a class, and then make a proxy which implements the interface, does whatever precondition steps I want, calls the real object's method, and then does whatever postcondition steps I want. One of the main annoyances of mine with this approach is that you can't have constructors in an interface, and you also can't have static methods in an interface, so there's no real obvious place to put that type of code. The hard part is the code generation - because you're either going to emit IL, or you're going to emit C# that you have to compile. But that's just been my approach. And it forced me to think about one aspect at a time, really - I hadn't gotten to the point where I could abstract out the "Aspect" and think in those terms. In short: roll your own or find a toolset you like, probably from Eric Bodden's list.

Matt Cruikshank
+3  A: 
  • DynamicProxy from Castle is probably the most used tool for doing AOP on the CLR.
  • Spring framework also offers AOP capabilities through its Spring.Aop namespace.
Romain Verdier
+1 For DynamicProxy-1 Spring because of the horrid XML!
Chris Canal
+2  A: 

Postsharp is another well-known one: "Bringing AOP to .NET!" I only have very little experience with it, but it looks nice and worth having a look at it.

Mudu
+1  A: 

PostSharp is good. I have been using it for about a year now. It's easy to install and has a fairly shallow learning curve considering the almost godlike power it enables. Additionally, there seem to be both an active community of developers and a responsive developer.

Check out the code samples on the PostSharp home page. Those are good examples of simper aspects done with PostSharp.

Jeffrey Sharp
A: 

I have been using Spring.Net AOP Framework for about 9 months now. It is pretty powerful and doesn't seem to impose a performance penalty in use even though weaving is done at run-time rather than during compilation. The only things to be aware of are that although objects you are applying advices to do not need to be aware of Spring.Net AOP, they must implement at least one interface. The documentation, which incidentally for Spring.Net in general is excellent, states that this restriction will be removed in a future version but doesn't give a roadmap.

Spring.Net AOP does not require you to use the rest of the Spring.Net framework and can be used in isolation.

Alfamale