2011-11-14 9 views
1

[HandlerAttribute]ベースのインターセプトを私のプロジェクトで使用したいと思います(これは、新しい開発者にとっては少しはっきりしているからです)。しかし、new InterceptionBehavior<PolicyInjectionBehavior>()RegisterTypeに明示的に指定しない限り、動作させることはできません。Unityで[HandlerAttribute]ベースの傍受作業をデフォルトですべて行う方法は?

[HandlerAttribute]を検出する簡単な方法はありますか?RegisterTypeは汚染されずに検出されますか?

私は次はそうのような UnityContainerExtensionを定義し、あなたが

後にしているものを達成すべきだと思う

答えて

1

public class InterceptionExtension : UnityContainerExtension 
{ 
    protected override void Initialize() 
    { 
     Context.Registering += OnRegister; 
     Context.RegisteringInstance += OnRegisterInstance; 
    } 

    public override void Remove() 
    { 
     Context.Registering -= OnRegister; 
     Context.RegisteringInstance -= OnRegisterInstance; 
    } 

    private void OnRegister(object sender, RegisterEventArgs e) 
    { 
     Container.Configure<Interception>() 
      .SetInterceptorFor(e.TypeTo, new VirtualMethodInterceptor()); 
    } 

    private void OnRegisterInstance(object sender, RegisterInstanceEventArgs e) 
    { 
     Container.Configure<Interception>() 
      .SetInterceptorFor(e.RegisteredType, new VirtualMethodInterceptor()); 
    } 
} 

は、コンテナにこれを追加します。すべての登録されたタイプのその後

_container.AddNewExtension<InterceptionExtension>(); 

を仮想メンバーに適用するにはInterceptionを設定する必要があります。これは、適用された[HandlerAttribute]でピックアップする必要があります。

+0

私の主な問題は 'Interceptor'ではなく' PolicyInjectionBehavior'であるので、私が理解している解決策は '[HandlerAttribute]'を取り上げません。しかし、アイデア自体は正しいと思いますが、私は 'SetInterceptorFor'をポリシー動作を追加するだけで置き換えます。 –

関連する問題