How can I write Interceptor (AOP) with Castle Core or Other libraries (just free libraries) for Cross Cutting Concerns

瘦欲@ 提交于 2019-12-25 01:44:09

问题


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()
   {

   }
}

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


回答1:


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();
    }
}

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 { ... }

and wire it:

var builder = new ContainerBuilder();
builder.RegisterType<FooService>()
   .EnableClassInterceptors();

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);
    }
}

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();
        }
    }
}

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");
}

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();
}


来源:https://stackoverflow.com/questions/27473012/how-can-i-write-interceptor-aop-with-castle-core-or-other-libraries-just-free

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!