Interception using NInject
NInject is a DI (Dependency Inversion) container. NInject can be used to inject classes and obtain instances of classes. Interception allows method invocations to be intercepted such as before a method call and after a method call. This is useful for logging, exception handling etc... There are several use cases. This blog post just shows the overall concept. The use cases can be handled if the concepts are understood.
The following NuGet packages need to be installed:
Ninject.Extensions.Interception.DynamicProxy
Ninject.Extensions.Interception
The IInterceptor interface can be implemented. This interface has only one method Intercept with the following signature:
Intercept(IInvocation invocation)
The parameter invocation has several properties with information regarding the target method, parameters being passed etc...
invocation.Proceed()
can be used for invoking the target method, even the result can be obtained. Wrapping this in a try..catch block, exceptions can be handled and logged, debug logs of method invocations, timing of method calls etc... can be logged.
I will create another blog post showing a sample code of the concept discussed above.
Until then, happy development!
Comments
Post a Comment
Chime in!