マーカーインタフェースを継承するインターフェイスに傍受注入を登録する方法
-
12-12-2019 - |
質問
例えば、次のコードを呼び出して、ポリシーを挿入したいサービス全体を登録します。
container
.AddNewExtensionIfNotPresent<Interception>().Configure<Interception>().SetDefaultInterceptorFor<IBusinessService>(new InterfaceInterceptor());
.
ここで:
ISomeServiceA : IBusinessService
ISomeServiceB : IBusinessService etc
.
私はあなたが cant からiMarkerInterfaceからImarkerInterfaceへの取得をどこかに読んだと思います.... これを確認できます。
解決
短い、ロードされたアセンブリを見てください(必要な場合は追加のフィルタリングを追加 - これがBootstrapで行われた場合はキャッシュなし)指定されたマーカーインターフェイスを実装する型のデフォルトのインターセプターを追加します。
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依存関係を持たないことを意味します(これはFluent API Config に適用されたフィルタリングを含みます。
.Include(If.Implements<IBusinessService>, (x, y) =>
{
if (x.IsClass)
y.Configure<Interception>().
SetDefaultInterceptorFor(x,
new VirtualMethodInterceptor
());
})
. 所属していません StackOverflow