.Net 中 AOP 的最佳实现是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/633710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:18:19  来源:igfitidea点击:

What is the best implementation for AOP in .Net?

.netaop

提问by ecleel

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

在 C#、VB.net 中有很多 AOP 实现。这是一些 AOP 实现:

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

.Net 中 AOP 的最佳实现是什么?我应该使用什么?

采纳答案by Romain Verdier

I think that Castle Dynamic Proxyis 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.

如果动态拦截可以满足您的需求,我认为Castle Dynamic Proxy是首选的解决方案。许多其他想要提供 AOP 功能的框架在内部使用该框架。通常,大多数现有的 IoC 容器现在都提供了一些动态拦截机制(Spring.NET、Castle Windsor、StructureMap 等)。如果您已经使用过 IoC 容器,那么查看它的建议可能会更容易一些。

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

如果动态拦截不能满足你的需求(编织密封类,拦截非虚调用等),那你当然想要静态编织。PostSharp是该领域的参考。

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

请注意,它还存在Linfu,可用于利用两种 AOP 方式。

回答by Marc Gravell

"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 codecurrently 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.

首先,列出您需要的功能、您的架构等。然后寻找满足您需求的选项,而不会引入不必要的复杂性。例如,有几个是面向界面的:您的代码当前是否面向界面?如果没有,PostSharp 可能是更好的选择(被编织到原始类中)。但是当然,PostSharp 不能在运行时配置......课程的马匹。

回答by Steven

The best way to do aspect-oriented programming in .NET is by using well known design techniques. For instance, by applying the SOLID principlesyou 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.

在 .NET 中进行面向方面编程的最佳方法是使用众所周知的设计技术。例如,通过应用SOLID 原则,您可以获得允许添加横切关注点所需的灵活性和模块化。如果你有正确的设计,你甚至可以在没有任何框架的情况下应用大多数横切关注点。认为 OOP 不适合做 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 thisand thisfor 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).

当您拥有正确的抽象时,向系统添加新的横切关注点只是编写一个新的装饰器类并将其包装在正确的实现中的问题。如果抽象是通用的,您可以将单个装饰器包装在一大组类中(这正是 AOP 的意义所在)。

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.

尽管诸如动态代理和代码编织之类的技术可以使设计糟糕的应用程序更容易工作,但对于好的设计来说确实别无选择。迟早你会被烧死。这并不意味着不应使用动态代理生成和代码编织。但是,如果没有适当的应用程序设计,即使是这些技术也只是微不足道的。

回答by Mendelt

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.

我确实使用过 PostSharp 并且惊喜地开始使用它是多么容易。

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.

我还研究了 Castle Windsor 和 Spring.Net 的 AOP,方法是不同的(运行时与编译时)。混合 AOP 和 IoC 似乎是有道理的。如果您还没有使用这些框架之一,那么开始需要做更多的工作,但不要让这阻止您。

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.

对于现在的新项目,我可能会使用 Castle Windsor,但这主要是因为我也想使用 IoC。如果我必须快速将 AOP 实现到现有代码库中,我会使用 PostSharp。

回答by Grigori Melnik

There's also Policy Injection and Unity Interceptionfrom Microsoft.

还有来自 Microsoft 的Policy Injection 和Unity Interception