2012-01-03 6 views
3

assemblyAのタイプをコンソールで使用してAOPスタイルでログインできるようにアプリケーションを設定しようとしています。 JournalInterceptorはちょうどいくつかの種類のログファイルまたはデータストアへのメソッド呼び出し、入力および出力多分引数を書き出します。Windsorと一括登録クラスを使用するAOP

私は、一度に1つのタイプを登録することができますが、私は一度にすべてのタイプを登録したいと思います。いったん私が行くと、私はいくつかの登録されたタイプにフィルタリングを追加するかもしれませんが、私は何かが欠けている。

私はClasses.FromAssemblyContainingを使用しようとしているが、WindsorContainer::Register

任意の手がかりへの呼び出しのためにIRegistrationインスタンスで取得する方法がわからないのですか?

// otherAssembly.cs 
namespace assemblyA 
{ 
    public class Foo1 { public virtual void What(){} } 
    public class Foo2 { public virtual void Where(){} } 
} 
// program.cs 
namespace console 
{ 
    using assemblyA; 

    public class JournalInterceptor : IInterceptor {} 

    public class Program 
    { 
    public static void Main() 
    { 
     var container = new Castle.Windsor.WindsorContainer() 
      .Register(
       Component.For<JournalInterceptor>().LifeStyle.Transient, 
       // works but can't be the best way 
       Component.For<Foo1>().LifeStyle.Transient 
        .Interceptors<JournalInterceptor>(), 
       Component.For<Foo2>().LifeStyle.Transient, 
        .Interceptors<JournalInterceptor>(), 
       // how do I do it this way 
       Classes.FromAssemblyContaining<Foo1>() 
         .Pick() 
         .LifestyleTransient() 
         .Interceptors<JournalInterceptor>() 
        ); 

     Foo1 foo = container.Resolve<Foo1>(); 
    } 
    } 
} 

答えて

4

Pointcutを実装します。城ウィンザーでは、これはIModelInterceptorsSelectorインターフェイスを実装することによって行われます。コンテナでインターセプタとポイントカットを登録すると

public class JournalPointcut : IModelInterceptorsSelector 
{ 
    public bool HasInterceptors(ComponentModel model) 
    { 
     return true; // intercept everything - probably not a good idea, though 
    } 

    public InterceptorReference[] SelectInterceptors(
     ComponentModel model, InterceptorReference[] interceptors) 
    { 
     return new[] 
     { 
      InterceptorReference.ForType<JournalInterceptor>() 
     }.Concat(interceptors).ToArray(); 
    } 
} 

:それはこのような何かを行くだろう

の深い説明について

this.container.Register(Component.For<JounalInterceptor>()); 

this.container.Kernel.ProxyFactory.AddInterceptorSelector(new JournalPointcut()); 

、あなたはthis recordingを見てみたいことがあります。

+0

ありがとうございました。私は、どのサービスを(カスタム属性を介して)除外したのですか。私は時間を取るとすぐにビデオセッションを見ます。 –

関連する問題