unity-container - 将 Unity 升级到 5.11.1
问题描述
我正在尝试将 Unity 从版本 3.0.1304.1 升级到最新版本 5.11.1。不出所料,有一些问题(3.0.1304.1 已经很老了)。下面的代码完成了两件事:
- 对从某个基类 X 派生的所有类使用 ContainerControlledLifetimeManager,而不为从 X 派生的每个类指定。
- 解析从基类 Y 派生的所有类时使用默认构造函数,而不为从 Y 派生的每个类指定默认构造函数。
Unity 文档现在很稀缺,并且缺乏示例。我已经阅读了升级说明和常规文档,但我无法使用新版本进行这项工作。
到目前为止,我有:
- 替换
IConstructorSelectorPolicy
为ISelect<ConstructorInfo>
- 替换
IBuilderContext
为BuilderContext
(或在 PreBuildUp 函数中引用 BuilderContext)
但是怎么办LifetimeManagerFactory
?我应该从(根据 ISelect 接口)派生IOCConstructorSelectorPolicy
并ISelect<ConstructorInfo>
替换IOCConstructorSelectorPolicy.SelectConstructor
为Select
(根据 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;
}
}
解决方案
推荐阅读
- robotframework - 无法使用标头中的 API 密钥创建 Robot Framework API 自动化测试
- java - Jersey 2 with Maven Jetty Plugin - Class Not found Error
- wix - MSI API 不支持长文件名 - 如何缩短文件名?
- java - After changing compiler settings in Eclipse, every class in my project suddenly fails to be resolved every time I save a file
- ansible - 如何在角色的变量文件中使用 Ansible 事实变量
- c++ - GiNaC 库出错
- asp.net-core - EF Core (3.0) 中 DbContext 的远程使用和 DI
- javascript - 如何修复 Open Redirect Fortify 问题
- progressive-web-apps - 普瓦。在 deferredPrompt.prompt() DOMException
- java - 从没有 USB 和 WIFI 的设备调试应用程序