仅针对单例的动态代理生成?
Dynamic proxy generation only for singletons?
我正在使用 Castle Dynamic Proxy and StructureMap 实现日志拦截器,因此在我的依赖项注册表中,我告诉 StructureMap 使用 LoggingInterceptor 装饰 TrafficSourceRepository 的所有实例。
var proxyGenerator = new ProxyGenerator();
For<ITrafficSourceRepository>(Lifecycles.Singleton)
.DecorateAllWith(instance => proxyGenerator
.CreateInterfaceProxyWithTargetInterface(instance,
new LoggingInterceptor(LogManager.GetLogger("AgencyPlayground"))))
.Use<TrafficSourceRepository>();
一切似乎都很好,它有效,但是 TrafficSourceRepository 将被实例化为 Singleton
,而我不想这样做,所以我将已解析的 TrafficSourceRepositories 的生命周期更改为 Transient
:
var proxyGenerator = new ProxyGenerator();
For<ITrafficSourceRepository>(Lifecycles.Transient)
.DecorateAllWith(instance => proxyGenerator
.CreateInterfaceProxyWithTargetInterface(instance,
new LoggingInterceptor(LogManager.GetLogger("AgencyPlayground"))))
.Use<TrafficSourceRepository>();
它不再起作用了...
这是一个错误还是我做错了什么?
这不是答案,但我无法将其放入评论中。这是一个最小的例子,表明它与 Lifecycles.Transient
:
一起工作得很好
class Program {
public static void Main() {
var proxyGenerator = new ProxyGenerator();
var container = new Container(config => {
config.For<ITrafficSourceRepository>(Lifecycles.Transient)
.DecorateAllWith(instance => proxyGenerator
.CreateInterfaceProxyWithTargetInterface(instance,
new LoggingInterceptor()))
.Use<TrafficSourceRepository>();
});
var ts = container.GetInstance<ITrafficSourceRepository>();
ts.Call();
Console.ReadKey();
}
}
public interface ITrafficSourceRepository {
void Call();
}
public class TrafficSourceRepository : ITrafficSourceRepository {
public void Call() {
Console.WriteLine("Called");
throw new Exception("Ex");
}
}
public class LoggingInterceptor : IInterceptor {
public void Intercept(IInvocation invocation) {
try {
invocation.Proceed();
}
catch (Exception ex) {
Console.WriteLine("Intercepted: " + ex.Message);
}
}
}
输出:
Called
Intercepted: Ex
我正在使用 Castle Dynamic Proxy and StructureMap 实现日志拦截器,因此在我的依赖项注册表中,我告诉 StructureMap 使用 LoggingInterceptor 装饰 TrafficSourceRepository 的所有实例。
var proxyGenerator = new ProxyGenerator();
For<ITrafficSourceRepository>(Lifecycles.Singleton)
.DecorateAllWith(instance => proxyGenerator
.CreateInterfaceProxyWithTargetInterface(instance,
new LoggingInterceptor(LogManager.GetLogger("AgencyPlayground"))))
.Use<TrafficSourceRepository>();
一切似乎都很好,它有效,但是 TrafficSourceRepository 将被实例化为 Singleton
,而我不想这样做,所以我将已解析的 TrafficSourceRepositories 的生命周期更改为 Transient
:
var proxyGenerator = new ProxyGenerator();
For<ITrafficSourceRepository>(Lifecycles.Transient)
.DecorateAllWith(instance => proxyGenerator
.CreateInterfaceProxyWithTargetInterface(instance,
new LoggingInterceptor(LogManager.GetLogger("AgencyPlayground"))))
.Use<TrafficSourceRepository>();
它不再起作用了... 这是一个错误还是我做错了什么?
这不是答案,但我无法将其放入评论中。这是一个最小的例子,表明它与 Lifecycles.Transient
:
class Program {
public static void Main() {
var proxyGenerator = new ProxyGenerator();
var container = new Container(config => {
config.For<ITrafficSourceRepository>(Lifecycles.Transient)
.DecorateAllWith(instance => proxyGenerator
.CreateInterfaceProxyWithTargetInterface(instance,
new LoggingInterceptor()))
.Use<TrafficSourceRepository>();
});
var ts = container.GetInstance<ITrafficSourceRepository>();
ts.Call();
Console.ReadKey();
}
}
public interface ITrafficSourceRepository {
void Call();
}
public class TrafficSourceRepository : ITrafficSourceRepository {
public void Call() {
Console.WriteLine("Called");
throw new Exception("Ex");
}
}
public class LoggingInterceptor : IInterceptor {
public void Intercept(IInvocation invocation) {
try {
invocation.Proceed();
}
catch (Exception ex) {
Console.WriteLine("Intercepted: " + ex.Message);
}
}
}
输出:
Called
Intercepted: Ex