如何使用Castle Core或其他库(仅免费库)编写Interceptor(AOP),以解决横切问题 [英] How can I write Interceptor (AOP) with Castle Core or Other libraries (just free libraries) for Cross Cutting Concerns
问题描述
我想为诸如日志记录",异常","..."之类的横切关注点"具有这样的属性.
I want to have an attribute like this for Cross Cutting Concerns like Logging , Exception , ...
public class MyService
{
[Log] // Interception (AOP)
[ExceptionHandler] // Interception (AOP)
public void DoSomething()
{
}
}
我知道我可以用postharp编写这些代码,但是我想用免费的库(例如Castle Core和...
I know that I can write these codes with postsharp but I want to write these interceptions with free libraries like Castle Core and ...
任何人都可以帮助我并为此目的编写示例吗??? 我需要一个非常简单的示例来学习概念
Can anyone help me and write a sample for these purpose ??? I need a very simple sample for learning concepts
推荐答案
Autofac是一个免费的IoC容器.我将Autofac与Autofac.Extras.DynamicProxy2 nuget ,
Autofac is a free IoC container. I use Autofac with Autofac.Extras.DynamicProxy2 nuget, docs.
假设您知道为什么(何时)(不使用)拦截器,并且想拦截某些功能:
Assuming you know why and when to (and not to) use interceptors, and you want to intercept some functionality:
public class FooService : IFooService
{
public void MoreFoo()
{
DoSomething();
}
public void LessFoo()
{
DoSomethingElse();
}
}
它需要连接".我喜欢属性,因为您无需在IoC容器接线时显式指定拦截器.您只需指定要注意的属性即可:
It needs to be "wired". I like attributes as you don't need to explicitly specify the interceptor at IoC container wiring. You just specify an attribute to watch out for:
[Intercept(typeof(Logger)]
public class FooService : IFooService { ... }
并连接:
var builder = new ContainerBuilder();
builder.RegisterType<FooService>()
.EnableClassInterceptors();
然后在另一个文件中创建Logger拦截器:
Then create your Logger interceptor in another file:
class Logger : IInterceptor
{
public void Intercept(IInvocation invocation) // implements the IInterceptor interface
{
_loggerService.Log("calling " + invocation.Method.Name);
invocation.Proceed();
_loggerService.Log("finished " + invocation.Method.Name);
}
}
如您所见,您可以创建计时器,try-catch块等.数据库上下文和其他可抛弃资源是一个有趣的资源:
As you can see, you can create timers, try-catch blocks, and much more. Database context and other disposable resources is an interesting one:
class Logger : IInterceptor
{
public void Intercept(IInvocation invocation) // implements the IInterceptor interface
{
using (var someThing = new SomeResource())
{
invocation.Proceed();
}
}
}
通常使用这种资源,您需要在方法内部使用someThing.这是另一个问题的话题! (请参阅invocation.SetArgumentValue或invocation.TargetType.GetProperties()以与封闭类进行通信.我对此不是100%满意,因此其他人的一些评论会有所帮助)
Usually with such a resource you need to use someThing inside your method. That's a topic for another question! (see invocation.SetArgumentValue or invocation.TargetType.GetProperties() to communicate to the enclosing class. I'm not 100% comfortable with this, so some comments from others would be helpful)
然后,以日志记录为例:
Then, take a logging as an example:
void ManageFoo()
{
// sorry for the messy code, what else can I do?!
_logger("more foo please");
_fooService.MoreFoo();
_logger("less foo please");
_fooService.LessFoo();
_logger("enough foo");
}
ManageFoo方法的实际问题在所有混乱的日志中都丢失了(增加安全性和其他问题,最终可能会造成很大的麻烦).
The actual concern of the ManageFoo method is lost in all the mess of logging (add security and other concerns and you can end up with a big mess).
现在您可以像这样重写它:
Now you can rewrite it like this:
void ManageFoo()
{
_fooService.MoreFoo();
_fooService.LessFoo();
}
这篇关于如何使用Castle Core或其他库(仅免费库)编写Interceptor(AOP),以解决横切问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!