Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在StructureMap注册表中使用具有依赖项的DynamicProxy拦截器_C#_.net_Dependency Injection_Structuremap_Castle Dynamicproxy - Fatal编程技术网

C# 在StructureMap注册表中使用具有依赖项的DynamicProxy拦截器

C# 在StructureMap注册表中使用具有依赖项的DynamicProxy拦截器,c#,.net,dependency-injection,structuremap,castle-dynamicproxy,C#,.net,Dependency Injection,Structuremap,Castle Dynamicproxy,我有以下代码: _container = new Container(x => x.AddRegistry<ManagerRegistry>()); 它在StructureMap中创建依赖项,并使用DynamicProxy对其进行装饰 现在这很好,因为拦截器本身没有依赖项 但鉴于以下情况: public class LoggingInterceptor : IInterceptor { public LoggingInterceptor(ILogger logger)

我有以下代码:

_container = new Container(x => x.AddRegistry<ManagerRegistry>());
在StructureMap中创建依赖项,并使用DynamicProxy对其进行装饰
现在这很好,因为拦截器本身没有依赖项

但鉴于以下情况:

public class LoggingInterceptor : IInterceptor
{
    public LoggingInterceptor(ILogger logger)
    {

我将如何进行连接结构图中的

() .EnrichAllWith(t=>proxyGenerator.CreateInterfaceProxyWithTarget( t、 _container.GetInstance()) .使用();
这就是我想到的:

_container.RegisterInterceptor<IPersonManager, LoggingInterceptor>();
\u container.RegisterInterceptor();
-

公共静态类ContainerExtensions
{
公共静态无效注册表接收器(此IContainer容器)
其中TDependency:class
其中,TInterceptor:IInterceptor
{
IInterceptor拦截器=container.GetInstance();
if(拦截器==null)
抛出新的NullReferenceException(“拦截器”);
类型拦截器类型拦截器
=新的GenericTypeInterceptor(拦截器);
Configure(c=>c.RegisterInterceptor(typeInterceptor));
}
}
-

公共类GenericTypeInterceptor:TypeInterceptor
其中TDependency:class
{
专用只读侦听器(interceptor);;
私有只读ProxyGenerator _ProxyGenerator=新ProxyGenerator();
公共通用类型拦截器(IIInterceptor拦截器)
{
if(拦截器==null)
抛出新的异常(“拦截器”);
_拦截器=拦截器;
}
公共对象进程(对象目标、IContext上下文)
{
返回_proxyGenerator.CreateInterfaceProxyWithTarget(目标为TDependency,_拦截器);
}
公共布尔匹配类型(类型)
{
返回类型(TDependency)。IsAssignableFrom(类型);
}
}

我对结果非常满意。

您在注册表中没有对容器的引用。这样做也会违反RRR模式。啊。。我懂了。你真的需要注册吗?您不必使用StructureMap发布实例,因此没有RRR,只有RR:-)这不适用于StructureMap 3+。看:如果你遇到这个问题,你可能实际上一直在寻找这个:
public class LoggingInterceptor : IInterceptor
{
    public LoggingInterceptor(ILogger logger)
    {
_container.RegisterInterceptor<IPersonManager, LoggingInterceptor>();
public static class ContainerExtensions
{
    public static void RegisterInterceptor<TDependency, TInterceptor>(this IContainer container)
        where TDependency : class
        where TInterceptor : IInterceptor 
    {
        IInterceptor interceptor = container.GetInstance<TInterceptor>();

        if (interceptor == null)
            throw new NullReferenceException("interceptor");

        TypeInterceptor typeInterceptor 
            = new GenericTypeInterceptor<TDependency>(interceptor);

        container.Configure(c => c.RegisterInterceptor(typeInterceptor));
    }
}
public class GenericTypeInterceptor<TDependency> : TypeInterceptor
    where TDependency : class
{
    private readonly IInterceptor _interceptor;
    private readonly ProxyGenerator _proxyGenerator = new ProxyGenerator();

    public GenericTypeInterceptor(IInterceptor interceptor)
    {
        if (interceptor == null)
            throw new ArgumentNullException("interceptor");

        _interceptor = interceptor;
    }

    public object Process(object target, IContext context)
    {
        return _proxyGenerator.CreateInterfaceProxyWithTarget(target as TDependency, _interceptor);
    }

    public bool MatchesType(Type type)
    {
        return typeof(TDependency).IsAssignableFrom(type);
    }
}