2012-07-26 58 views
0

比如我想調用下面的代碼註冊了一整套,我想注入的政策服務:如何爲繼承標記接口的接口註冊攔截注入?

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

其中:

ISomeServiceA : IBusinessService 
ISomeServiceB : IBusinessService etc 

我覺得我讀了你cant從ISomeServceX獲取到IMarkerInterface的某個地方...... 可以證實這一點。

+0

我可以使用反射和通用擴展方法..... – brumScouse 2012-07-26 15:15:38

回答

0

總之,看看加載的程序集(增加額外的濾波如果需要的話 - 如果沒有緩存是在引導完成)添加默認攔截器實現指定的標記接口類型。

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

編輯:

上面可以使用流利API完成,並且意味着我們沒有關於AppDomain.CurrentDomain.GetAssemblies)的naiive依賴性((其包含難道不在流利API配置應用的濾波

.Include(If.Implements<IBusinessService>, (x, y) => 
                  { 
                   if (x.IsClass) 
                    y.Configure<Interception>(). 
                     SetDefaultInterceptorFor(x, 
                           new VirtualMethodInterceptor 
                            ()); 
                  }) 
+0

將您知道如何在vb.net寫的嗎? – 2013-01-14 21:25:01

+0

沒關係,我知道了。 – 2013-01-14 21:47:22