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
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:
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:
and wire it:
Then create your Logger interceptor in another file:
As you can see, you can create timers, try-catch blocks, and much more. Database context and other disposable resources is an interesting one:
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:
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: