首页 > 解决方案 > 将 Unity 升级到 5.11.1

问题描述

我正在尝试将 Unity 从版本 3.0.1304.1 升级到最新版本 5.11.1。不出所料,有一些问题(3.0.1304.1 已经很老了)。下面的代码完成了两件事:

  1. 对从某个基类 X 派生的所有类使用 ContainerControlledLifetimeManager,而不为从 X 派生的每个类指定。
  2. 解析从基类 Y 派生的所有类时使用默认构造函数,而不为从 Y 派生的每个类指定默认构造函数。

Unity 文档现在很稀缺,并且缺乏示例。我已经阅读了升级说明和常规文档,但我无法使用新版本进行这项工作。

到目前为止,我有:

但是怎么办LifetimeManagerFactory?我应该从(根据 ISelect 接口)派生IOCConstructorSelectorPolicyISelect<ConstructorInfo>替换IOCConstructorSelectorPolicy.SelectConstructorSelect(根据 ISelect 接口)以及如何实现此 Select 函数?

我知道这是一个很长的镜头,我也会将其发布在统一 github 上(作为各种文档的请求),但希望有人可以给我一些指示。

    /// <summary>
    /// This class merely exists for readability: avoid having to write the class it derives from.
    /// </summary>
    public class IOCConfigLifetimeExtension : DefaultLifetimeManagerExtension<ContainerControlledLifetimeManager, X>
    {}

    /// <summary>
    /// An IOC strategy to use a specific LifetimeManager (<typeparamref name="TLifetimeManager"/>) for subclasses of <typeparamref name="TBaseClass"/>.
    /// </summary>
    public class DefaultLifetimeManagerExtension<TLifetimeManager, TBaseClass> : UnityContainerExtension where TLifetimeManager : LifetimeManager, new()
    {
        protected override void Initialize()
        {
            var theFactory = new LifetimeManagerFactory(Context, typeof(TLifetimeManager));
            Context.Strategies.Add(new DefaultLifetimeManagerStrategy(theFactory, TypePredicate), UnityBuildStage.TypeMapping);
        }

         private bool TypePredicate(Type type)
         {
             return typeof (TBaseClass).IsAssignableFrom(type);
         }
    }

    public class DefaultLifetimeManagerStrategy : BuilderStrategy
    {
        public DefaultLifetimeManagerStrategy(LifetimeManagerFactory factory, Predicate<Type> typePredicate)
        {
            mFactory = factory;
            mTypePredicate = typePredicate;
        }

        public override void PreBuildUp(IBuilderContext context)
        {
            if (context.Existing == null) {
                var theLifetime = context.Policies.GetNoDefault<ILifetimePolicy>(context.BuildKey, false);

                if (theLifetime == null && mTypePredicate(context.BuildKey.Type)) {
                    theLifetime = mFactory.CreateLifetimePolicy();
                    context.PersistentPolicies.Set(theLifetime, context.BuildKey);
                }
            }
        }
        private readonly LifetimeManagerFactory mFactory;
        private readonly Predicate<Type> mTypePredicate;
    }

    /// <summary>
    /// A Unity extension that prioritizes the default constructor for classes derived of Y when available, otherwise the default resolve method is used.
    /// </summary>
    public class IOCExtension : UnityContainerExtension
    {
        protected override void Initialize()
        {
            var theDefaultConstructorSelectorPolicy = Context.Policies.Get<IConstructorSelectorPolicy>(null);
            Context.Policies.SetDefault<IConstructorSelectorPolicy>(new IOCConstructorSelectorPolicy(theDefaultConstructorSelectorPolicy));
        }

        public class IOCConstructorSelectorPolicy : IConstructorSelectorPolicy
        {
            public IOCConstructorSelectorPolicy(IConstructorSelectorPolicy defaultConstructorSelectorPolicy)
            {
                mDefaultConstructorSelectorPolicy = defaultConstructorSelectorPolicy;
            }

            public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination)
            {
                Type theType = context.BuildKey.Type;
                if (typeof(DataNode).IsAssignableFrom(theType)) {
                    ConstructorInfo theDefaultConstructorInfo = theType.GetConstructor(Type.EmptyTypes);
                    if (theDefaultConstructorInfo != null && theDefaultConstructorInfo.IsPublic) {
                        return new SelectedConstructor(theDefaultConstructorInfo);
                    }
                }
                return mDefaultConstructorSelectorPolicy.SelectConstructor(context, resolverPolicyDestination);
            }

            private readonly IConstructorSelectorPolicy mDefaultConstructorSelectorPolicy;
        }
    }

标签: unity-container

解决方案


推荐阅读