簡體   English   中英

代理使用Ninject.Extensions.Interception.Linfu公開多個接口

[英]Proxy exposing multiple interfaces with Ninject.Extensions.Interception.Linfu

我正在使用Ninject.Extensions.Interception (更具體地說是InterceptAttribute )和Ninject.Extensions.Interception.Linfu代理在我的C#應用​​程序中實現日志記錄機制,但是當代理類實現多個接口時,我會遇到一些問題。

我有一個實現接口並從抽象類繼承的類。

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

問題是返回的代理只能識別AbstractClass接口,從而暴露SomeMethod() 因此,當我嘗試訪問不存在的SomeProperty時,我收到ArgumentException

//ArgumentException
proxy.SomeProperty = "Hi";

在這種情況下,是否可以使用mixin或其他技術來創建公開多個接口的代理?

謝謝

保羅

我遇到了類似的問題,但僅使用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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM