Question

For example I would like to call the following code to register a whole set of services which I want to inject a policy for:

container
            .AddNewExtensionIfNotPresent<Interception>().Configure<Interception>().SetDefaultInterceptorFor<IBusinessService>(new InterfaceInterceptor());

Where:

ISomeServiceA  : IBusinessService
ISomeServiceB : IBusinessService etc 

I think I read that you cant get from ISomeServceX to IMarkerInterface somewhere.... Can this be confirmed.

Was it helpful?

Solution

In short, look at loaded assemblies (add extra filtering if required - no caching if this is done on bootstrap) Add default interceptors for the types implementing the designated marker interface.

        container.AddNewExtensionIfNotPresent<Interception>();
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        foreach (Assembly assembly in assemblies)
        {
            Type[] types = assembly.GetTypes().Where(x => x.IsClass && typeof(T).IsAssignableFrom(x) && x.GetType() != typeof(T)).ToArray();
            foreach (Type t in types)
            {
                container.Configure<Interception>().SetDefaultInterceptorFor(t, new VirtualMethodInterceptor());
            }
        }

EDIT:

The above can be done using the fluent API and means we dont have the naiive dependency on AppDomain.CurrentDomain.GetAssemblies() (which wouldnt contain the filtering applied in the fluent api config

.Include(If.Implements<IBusinessService>, (x, y) =>
                                                          {
                                                              if (x.IsClass)
                                                                  y.Configure<Interception>().
                                                                      SetDefaultInterceptorFor(x,
                                                                                               new VirtualMethodInterceptor
                                                                                                   ());
                                                          })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top