C# Structuremap 3的TypeInterceptor替换
在从StructureMap 2升级到版本3时,似乎找不到关于如何在我的代码库中重现TypeInterceptor当前提供的功能的有用指南(无法升级到v4,因为我们还没有使用.NET 4.6) 拦截器的基本功能是:C# Structuremap 3的TypeInterceptor替换,c#,structuremap3,C#,Structuremap3,在从StructureMap 2升级到版本3时,似乎找不到关于如何在我的代码库中重现TypeInterceptor当前提供的功能的有用指南(无法升级到v4,因为我们还没有使用.NET 4.6) 拦截器的基本功能是: public class TheInterceptor : TypeInterceptor { private Dictionary<string, string> typesToIntercept; public TheInterceptor(IDic
public class TheInterceptor : TypeInterceptor
{
private Dictionary<string, string> typesToIntercept;
public TheInterceptor(IDictionary<string, string> typesToIntercept)
{
// Passed in on ctor, comes from XML configuration section.
this.typesToIntercept = typesToIntercept;
}
public object Process(object target, StructureMap.IContext ctx)
{
var typedTarget = target as BaseType;
var key = target.GetType().FullName;
if (typedTarget == null || !typesToIntercept.ContainsKey(key))
{
return target;
}
var propertyOverrideType = typesToIntercept[key];
typedTarget.BaseProperty = ctx.GetInstance<IBaseInterface>(propertyOverrideType);
return typedTarget;
}
}
public类interceptor:TypeInterceptor
{
私用词典;
公用侦听器(IDictionary类型可截取)
{
//在ctor上传入,来自XML配置部分。
this.typesToIntercept=typesToIntercept;
}
公共对象进程(对象目标,StructureMap.IContext ctx)
{
var typedTarget=作为基本类型的目标;
var key=target.GetType().FullName;
if(typedTarget==null | |!typesToIntercept.ContainsKey(键))
{
回报目标;
}
var-propertyOverrideType=typesToIntercept[key];
typedTarget.BaseProperty=ctx.GetInstance(propertyOverrideType);
返回类型目标;
}
}
因此,我们基本上是维护一个字典,其中键是我们想要截取的类型,值是实现已知接口的特定类型,我们想要在截取对象的属性上设置该接口
FWIW我没有编写原始代码,我只是不知道在StructureMap 3中镜像这种行为的正确方法是什么。我觉得这是一件没有拦截器也可以做到的事情,但我相信它是这样实现的,这样就可以跨多个站点(在共享库中)使用这种行为,而无需每个站点都明确处理拦截器行为,因此,如果可能的话,我希望保留这种用法。因此,我最终通过反复试验解决了这个问题。您需要的是一个ActivatorInterceptor,并使用Action委托来执行以前在TypeInterceptor的Process方法内部的逻辑。因此,从我上面的代码片段来看,它变成:
public class InterceptorPolicy : IInterceptorPolicy
{
private readonly IDictionary<string, string> typesToIntercept;
public InterceptorPolicy(IDictionary<string, string> types)
{
this.typesToIntercept = types;
}
public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance)
{
if (instance.ReturnedType.IsSubclassOf(typeof(BaseType)))
{
yield return new ActivatorInterceptor<BaseType>((ctx, x) => this.Activate(ctx, x));
}
}
private void Activate(IContext ctx, BaseType instance)
{
var key = instance.GetType().FullName;
if (this.typesToIntercept.ContainsKey(key))
{
var propertyOverrideType = this.typesToIntercept[key];
instance.BaseProperty = ctx.GetInstance<IBaseInterface>(propertyOverrideType);
}
}
}
公共类侦听器策略:IInterceptorPolicy
{
私有只读IDictionary类型拦截;
公共拦截策略(IDictionary类型)
{
this.typesToIntercept=类型;
}
public IEnumerable determinateInterceptors(类型pluginType,实例实例)
{
if(instance.ReturnedType.IsSubclassOf(typeof(BaseType)))
{
产生返回新的ActivatorInterceptor((ctx,x)=>this.Activate(ctx,x));
}
}
私有void激活(IContext ctx,BaseType实例)
{
var key=instance.GetType().FullName;
if(此.typesToIntercept.ContainsKey(键))
{
var propertyOverrideType=this.typesToIntercept[key];
instance.BaseProperty=ctx.GetInstance(propertyOverrideType);
}
}
}
所以我最终通过反复试验找到了答案。您需要的是一个ActivatorInterceptor,并使用Action委托来执行以前在TypeInterceptor的Process方法内部的逻辑。因此,从我上面的代码片段来看,它变成:
public class InterceptorPolicy : IInterceptorPolicy
{
private readonly IDictionary<string, string> typesToIntercept;
public InterceptorPolicy(IDictionary<string, string> types)
{
this.typesToIntercept = types;
}
public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance)
{
if (instance.ReturnedType.IsSubclassOf(typeof(BaseType)))
{
yield return new ActivatorInterceptor<BaseType>((ctx, x) => this.Activate(ctx, x));
}
}
private void Activate(IContext ctx, BaseType instance)
{
var key = instance.GetType().FullName;
if (this.typesToIntercept.ContainsKey(key))
{
var propertyOverrideType = this.typesToIntercept[key];
instance.BaseProperty = ctx.GetInstance<IBaseInterface>(propertyOverrideType);
}
}
}
公共类侦听器策略:IInterceptorPolicy
{
私有只读IDictionary类型拦截;
公共拦截策略(IDictionary类型)
{
this.typesToIntercept=类型;
}
public IEnumerable determinateInterceptors(类型pluginType,实例实例)
{
if(instance.ReturnedType.IsSubclassOf(typeof(BaseType)))
{
产生返回新的ActivatorInterceptor((ctx,x)=>this.Activate(ctx,x));
}
}
私有void激活(IContext ctx,BaseType实例)
{
var key=instance.GetType().FullName;
if(此.typesToIntercept.ContainsKey(键))
{
var propertyOverrideType=this.typesToIntercept[key];
instance.BaseProperty=ctx.GetInstance(propertyOverrideType);
}
}
}