What is the best implementation for AOP in .Net? [closed]
Asked Answered
E

5

90

There is a lot of AOP implementation in C#, VB.net. this is some of AOP Implementations:

What is the best implementation for AOP in .Net? What I should use?

Essentialism answered 11/3, 2009 at 8:14 Comment(5)
It would be helpful if you provided links to all the AOP, so as to saver the reader a bit of time with Google. I am hoping this question/answers will become a great summary of the different AOP options in .NETTinney
52 people voted it's a constructive question. 5 voted it's not constructive. Who decided? At least moderator should change or reformulate the question, but they'd better to consider most of people's opinion.Queenhood
@Queenhood Totally agree !Libel
It is amazing that SO and people downvote questions like this as OT. Such a useful question and technology to discuss and help community gets simply voted down and closed but bunch of old fashioned, 80ies style people that are stuck in the past. SO, the world has changed. It is understandable completely that you want to help asnwer specific questions but at least provide a link in your "closed" tag where to post such questions. That would help a lot and avoid this kind of stupidity.Quenna
9 years later, I think it's clear why this question is not very useful. Many links are dead.Head
C
48

I think that Castle Dynamic Proxy is the solution of choice if dynamic interception can handle your needs. This framework is used internally by a lot of other frameworks that want to offer AOP capabilities. Typically, most of existing IoC containers now provide some dynamic interception mechanisms (Spring.NET, Castle Windsor, StructureMap, etc.) If you already work with an IoC container, maybe it could be easier to look at what it proposes.

If dynamic interception can't address your needs (weaving sealed class, intercepting non-virtual call, etc.), then you certainly want static weaving. PostSharp is the reference in this domain.

Note that it also exists Linfu, that can be used to leverage both AOP fashions.

Current answered 11/3, 2009 at 9:12 Comment(5)
+1 on that if you want to do runtime AOP. +1 on PostSharp if you want post-compile time AOPDebrahdebrecen
Any update on this answer? Or is this still valid? I was specially wondering how Spring.Aop compares to Castle and PostSharpSteiermark
unfortunately, PostSharp is a commercial productQuenna
Postsharp is commercial product, not free. Please advice anything else for static weaving.Homozygous
@ShivamSachan Only because something is free does not make it good. Most free AOP in .net died with last updates 2-5 years ago, PostSharp is still best in class.Ashby
S
15

"Best" is subjective.

First, draw up a list of the features you need, your architecture, etc. Then look for options that do what you need, without introducing unnecessary complexity. For example, several are interface oriented: is your code currently interface oriented? If not, PostSharp might be a better choice (being weaved into the original classes). But of course, PostSharp can't be configured at runtime... horses for courses.

Silvertongued answered 11/3, 2009 at 8:20 Comment(1)
you could reformulate what you said as follows: "Best is subjective, I'll draw up a list of the pro and contros for some of this framework".Queenhood
F
11

"The best way," of course, is subjective but, to me, the best way to do aspect-oriented programming in .NET is by using well known design techniques. For instance, by applying the SOLID principles you can achieve the flexibility and modularity you need to allow adding cross-cutting concerns. If you have the design right, you will even be able to apply most cross-cutting concerns without any framework. It is a fallacy to think that OOP is unsuited for doing AOP.

Here are some pointers:

  • Don't depend on concrete instances, but depend on abstractions.
  • Don't mix cross-cutting concerns and business logic in the same class.
  • Adding cross-cutting concerns by wrapping classes with business logic in classes that implement those concerns (decorators).
  • Find common artifacts in your design and model them equally, preferably using the same type of abstractions. Take a look at this and this for instance.

When you've got the right abstractions in place, adding new cross-cutting concerns to the system is just a matter of writing a new decorator class and wrapping it around the right implementations. If abstractions are generic, you can wrap a single decorator around a large group of classes (which is exactly what AOP is about).

Although techniques such as dynamic proxies and code weaving could make it easier to work with a badly designed application, there is truly no alternative for good design. Sooner or later you will get burned. This doesn't mean that dynamic proxy generation and code weaving should not be used though. But without a proper application design even those techniques will just be marginally helpful.

Ferrante answered 27/12, 2012 at 23:54 Comment(6)
AOP is the next level of abstraction. In fact, it's the limitation of inheritance and composition that lead to AOP. Have you seen Entlib exception block? Aspect is a lot cleaner than invoking that damn block for every single call into the database, just to try-catch-log-throw.Solley
If you wrap every call to your database with an exception block, you're doing it wrong anyway. It comes back to good design. Always.Ferrante
So instead of catching db exceptions what is the right way?Colonize
@OutOFTouch: Take a look at the answer of this SO question.Ferrante
@SleeperSmith: although I'm not sure what "AOP is the next level of abstraction" means, I do think that you can't keep big systems maintainable without use of AOP. I believe in applying AOP. However, AOP is a paradigm; not a tool. I agree with the limitation of inheritance, but it's not a limitation of composition that leads to AOP. It's the lack of proper design that leads to using code weaving and dynamic proxy tools. I apply cross-cutting concerns using decorators. This is my preferred way of applying AOP.Ferrante
sometimes we need to use AOP may be because of some third-party code which is out of our control, it's not our own code.Haas
G
5

I don't know about best, there are a lot of frameworks and not enough hours in the day to try them all.

I did use PostSharp and was pleasantly surprised how easy it is to get started with it.

I also looked into AOP with Castle Windsor and Spring.Net, the approach is different (runtime vs compile time). Mixing AOP and IoC seems to make sense. When you're not using one of these frameworks yet it's a lot more work to get started but don't let that stop you.

For new projects now I'd probably use Castle Windsor, but that's mostly because I'd also want to use IoC. If i had to quickly implement AOP into an existing code base I'd use PostSharp.

Gasworks answered 11/3, 2009 at 8:23 Comment(0)
C
4

There's also Policy Injection and Unity Interception from Microsoft.

Cheryl answered 9/4, 2011 at 20:8 Comment(1)
Unity Interception is no longer supported: github.com/unitycontainer/unityNeely

© 2022 - 2024 — McMap. All rights reserved.