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