Intercept IEnumerable list of Interface types with Unity DI/IoC

378 views Asked by At

To All That Can Help,

I'm new to the Unity container and I'm embarking on a way to filter collections that contain a certain interface by use of interception. The application of interception is new to me, even with my experience with AutoFac and Castle. However, I know it can be done but I'm just having a hard time understanding why my interceptor will not fire.

Essentially, the goal is to filter a collection of objects based on the logged in user's security level. The interface has been named ISecurable and it requires its implementors to have a property of MinSecurityLevel. Unfortunately, majority of the code base is not programming against interfaces, so this interface is new. Due to that fact, there are several collections that will use implementors of this interface, i.e.

Customer[] Customers { get; set; }
List<Order> Orders { get; set; }

Where Orders and Customers implements ISecurable.

So, rather than finding each collection that contains objects that implement ISecurable and filtering those collections based on the logged in user's security level (which will become a monster to maintain and nasty to get the user's logged in security level). I figured interception ANY collection that contain objects of ISecurable would be a simple, elegant way of meeting this requirement while keeping that code tabbed to one central location.

In doing so, I have scoured the internet of articles and example code to get me a baseline to begin intercepting. Unfortunately, nothing has proven fruitful.

It seems like I am in the same boat as the poster here: Unity Interception - Custom Interception Behaviour

Unfortunately, I am unaware how he/she was able to wire up a CallHandler and/or InterceptionBehavior implementor to begin filtering their collection (plus, their code base is programming against an interface, i.e. IList, my codebase has dependencies on concrete types of Array and List).

Here is some a version of the code I have tried (with no lock of firing any property or method of the Handler or InterceptionBehavior class):

 container.AddNewExtension<Interception>();
 container.RegisterType<SecurityHandler>();

 container.RegisterType<IEnumerable<ISecurable>, Customer[]>(
      new Interceptor<TransparentProxyInterceptor>(),
      new InterceptionBehavior<SecurableInterceptionBehavior>());

 container.RegisterType<IEnumerable<ISecurable>, List<Order>>(
      new Interceptor<TransparentProxyInterceptor>(),
      new InterceptionBehavior<SecurableInterceptionBehavior>());

 container.Configure<Interception>)
      .AddPolicy("SecurityPolicy")
      .AddCallHandler(new SecurityHandler());

I have tried the code above with and without the SecurityHandler, I get the same result - no interception firing.

Thanks to all that help!

Here are reference(s) I have used so far:

https://msdn.microsoft.com/en-us/library/ff660851(v=pandp.20).aspx#interception_behavior_custom

P.S. Due to my lack of reputation I was unable to post more than 2 links that I have referenced. Sorry :(

EDIT

I am able to get my interceptor to fire if I resolve the type from the container.

Just as a test, this worked:

 container.RegisterType<ISecurable, Customer>(
      new Interceptor<InterfaceInterceptor>(),
      new InterceptionBehavior<SecurableInterceptionBehavior>());

 var securable = container.resolve<ISecurable>();
 securable.MinSecurityLevel = 1;
0

There are 0 answers