2012-04-24 5 views
1

私はC#アプリケーションでログ機能を実装するのにNinject.Extensions.Interception(より具体的にはInterceptAttribute)とNinject.Extensions.Interception.Linfuプロキシを使用していますが、プロキシクラスいくつかのインターフェイス。Ninject.Extensions.Interception.Linfuで複数のインターフェイスを公開するプロキシ

私は、インターフェイスを実装し、抽象クラスから継承するクラスを持っています。

public class MyClass : AbstractClass, IMyClass { 
    public string SomeProperty { get; set; } 
} 


public class LoggableAttribute : InterceptAttribute { ... } 

public interface IMyClass { 
    public string SomeProperty { get; set; } 
} 

public abstract class AbstractClass { 

    [Loggable] 
    public virtual void SomeMethod(){ ... } 
}  

私はのServiceLocatorからのMyClassのインスタンスを取得しようとすると、のLoggable属性は、プロキシを返すようになります。

var proxy = _serviceLocator.GetInstance<IMyClass>(); 

問題は、プロキシのみのsomeMethod()を露出AbstractClassインターフェースを認識戻されます。結局のところ、SomePropertyにアクセスしようとすると、ArgumentExceptionが届きます。

//ArgumentException 
proxy.SomeProperty = "Hi"; 

この場合、mixinやその他の技術を使用して複数のインターフェイスを公開するプロキシを作成する方法はありますか?

パウロ

答えて

0

おかげで、私は同様の問題に走ったと私は唯一ninject手段とエレガントな解決策が見つかりませんでした。だから私はOOPからのより基本的なパターンで問題に取り組んだ:構成。

応用問​​題にこのようなものは、私の提案のようになります。

public interface IInterceptedMethods 
{ 
    void MethodA(); 
} 

public interface IMyClass 
{ 
    void MethodA(); 
    void MethodB(); 
} 

public class MyInterceptedMethods : IInterceptedMethods 
{ 
    [Loggable] 
    public virtual void MethodA() 
    { 
     //Do stuff 
    } 
} 

public class MyClass : IMyClass 
{ 
    private IInterceptedMethods _IInterceptedMethods; 
    public MyClass(IInterceptedMethods InterceptedMethods) 
    { 
     this._IInterceptedMethods = InterceptedMethods; 
    } 
    public MethodA() 
    { 
     this._IInterceptedMethods.MethodA(); 
    } 
    public Method() 
    { 
     //Do stuff, but don't get intercepted 
    } 
} 
関連する問題