StructureMap 3的TypeInterceptor替换

时间:2016-04-23 19:01:28

标签: c# structuremap3

在从StructureMap 2升级到版本3时,似乎无法找到有关如何重现我的代码库中TypeInterceptor当前提供的功能的任何有用指南(因为我们不使用.NET 4.6而无法升级到v4)尚未)。

基本上拦截器的作用是:

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;
    }
}

所以我们基本上维护一个字典,其中键是我们想要拦截的类型,值是实现已知接口的特定类型,我们想要在截取的对象的属性上设置。

FWIW我没有写原始代码,我只是无法弄清楚在StructureMap 3中镜像这种行为的正确方法是什么。我觉得这可以在没有拦截器的情况下完成,但我相信它是这样实现的,这样就可以在多个站点(它在共享库中)使用这种行为,而不需要每个站点都必须明确地处理拦截行为,所以,如果可能的话,我想保留这种用法。

1 个答案:

答案 0 :(得分:1)

所以我最终通过反复试验来解决这个问题。你需要的是一个ActivatorInterceptor,并使用Action委托来执行以前在TypeInterceptor的Process方法中的逻辑。因此,从我上面的代码段开始,它变为:

 Cannot complete the install because one or more required items could not be found.
  Software being installed: Visual Editor 1.5.0.R20101202-1328 (org.eclipse.ve.feature.group 1.5.0.R20101202-1328)
  Missing requirement: Visual Editor Common Data Editor 1.5.0.R20101202-1328 (org.eclipse.ve.cde 1.5.0.R20101202-1328) requires 'bundle com.ibm.icu [3.4.4.1,4.3.0)' but it could not be found
  Cannot satisfy dependency:
    From: Visual Editor 1.5.0.R20101202-1328 (org.eclipse.ve.feature.group 1.5.0.R20101202-1328)
    To: org.eclipse.ve.cde [1.3.0,2.0.0)