0

Is Microsoft working on a solution for AOP in C#? What are the (real AOP) alternatives?

Bonus question: is Code Contracts a kind of AOP?

1

3 Answers 3

2

Most dependency injection frameworks in .net have the ability to use AOP techniques, I personally prefer Ninject and the Interceptor plugin as its simple to use and doesn't require any class pollution with 3rd party attributes or anything.

That being said Spring.net, Castle Windsor and lots of other DI frameworks support the same principle with slightly different syntax and approaches, some are xml driven some are c# driven so you can have compile time safety.

One thing to think about is that most DI frameworks do this via class proxying, so they require your methods to be virtual and then at runtime it proxies your class method and you can intercept at that point. There are some other approaches such as IL weaving which is what PostSharp does, which can be faster than proxying, although I dont have any measurements to back that up.

Didn't see the bonus question bit, for me I would say its not a clear cut answer, as it depends on how you use it, as generally you are using AOP as a way to separate your non functional concerns such as logging, transactions maybe validation from your source code, so you add this logic from the outside in, so ideally your source code has no knowledge of the AOP happening (although that's not always the case if you are using some 3rd party attributes which will couple your source code to the AOP framework).

As code contracts can be written in-line with existing classes or hooked in via attributes or I believe can be fully injected at runtime, so much like logging concerns you could put them all into your classes but when using an AOP approach you wouldn't. So for me Code Contracts are just a tool to enforce something, you can use it in an AOP fashion or you can just embed it in your code.

2

PostSharp is another alternative. Easy to get started with, although it is no longer free to use fully.

I don't know if Microsoft is still actively contributing to the Unity project. Maybe others can shed some light on this.

Code Contracts is not (a type of) Aspect Oriented Programming.

0

As alternative you can also use Spring AOP

Link : http://static.springsource.org/spring/docs/2.0.x/reference/aop.html

Nota : I prefer Unity

Not the answer you're looking for? Browse other questions tagged or ask your own question.