我正在使用Ninject.Extensions.Interception (更具体地说,是InterceptAttribute)和Ninject.Extensions.Interception.Linfu代理来实现InterceptAttribute应用程序中的日志机制,但是当代理类实现多个接口时,我面临一些问题。
我有一个实现接口并从抽象类继承的类。
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(){ ... }
} 当我试图从MyClass获取一个ServiceLocator实例时,Loggable属性会使它返回一个代理。
var proxy = _serviceLocator.GetInstance<IMyClass>();问题是返回的代理只识别AbstractClass接口,公开了SomeMethod()。因此,当我试图访问不存在的ArgumentException时,我会收到一个SomeProperty。
//ArgumentException
proxy.SomeProperty = "Hi";在这种情况下,是否有一种方法可以使用mixin或其他一些技术来创建一个暴露多个接口的代理?
谢谢
保罗
发布于 2013-01-10 00:02:16
我遇到了一个类似的问题,我没有找到一个优雅的解决方案,只有九种方法。所以我用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
}
}https://stackoverflow.com/questions/10302750
复制相似问题