- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在将相同的具体类注册到不同的未命名实例时,我们看到 Unity 出现意外行为。在这种情况下,注册似乎以意想不到的方式相互干扰。
独立注册是我们所期望的,但不起作用。似乎 mapTo 类型不是根据注册类型进行管理,而是仅根据 mapTo 类型和注册名称进行管理。我们想了解预期的行为是什么,当前的行为是否符合预期,以及是否有一种干净的方法来实现独立注册。请注意,我们已经看到了命名注册的建议,但我们不想更改我们的框架,因为它正在使用中,而且通常我们不想无缘无故地强制应用程序符合命名实例。附加的代码演示了意外行为。
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace TestUnity
{
public interface IBaseInterface
{
string BaseString { set; get; }
}
public interface IChildInterface : IBaseInterface
{
string ChildString { set; get; }
}
public class ChildClass : IChildInterface
{
public string BaseString { set; get; }
public string ChildString { set; get; }
}
public class ContainerClass
{
public IBaseInterface impl { set; get; }
}
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.LoadConfiguration();
// the expected result BaseString ="IBaseInterface_BaseString" & ChildString ="IBaseInterface_ChildString"
// the result is BaseString ="IChildInterface_BaseString" & ChildString ="IChildInterface_ChildString"
var iBaseInterface = container.Resolve<IBaseInterface>();
// the expected result BaseString ="IChildInterface_BaseString" & ChildString ="IChildInterface_ChildString"
var iChildInterface = container.Resolve<IChildInterface>();
//We expect test class will be initialize with BaseString ="IBaseInterface_BaseString" & ChildString ="IBaseInterface_ChildString"
//but the result is the expected result BaseString ="IChildInterface_BaseString" & ChildString ="IChildInterface_ChildString"
var testClass = container.Resolve<ContainerClass>("Test");
//The container.Registrations include both regestered types(IBaseInterface & IChildInterface)
foreach (var registration in container.Registrations)
{
Console.WriteLine(@"RegisteredType :{0} ,MappedToType :{1}", registration.RegisteredType, registration.MappedToType);
}
Console.ReadLine();
}
}
}
应用程序配置
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IBaseInterface" type="TestUnity.IBaseInterface, TestUnity" />
<alias alias="IChildInterface" type="TestUnity.IChildInterface, TestUnity" />
<alias alias="transient" type="Microsoft.Practices.Unity.TransientLifetimeManager, Microsoft.Practices.Unity"/>
<container>
<register type="IBaseInterface" mapTo="TestUnity.ChildClass, TestUnity">
<lifetime type="transient"/>
<property name="BaseString" value="IBaseInterface_BaseString" />
<property name="ChildString" value="IBaseInterface_ChildString" />
</register>
<register type="IChildInterface" mapTo="TestUnity.ChildClass, TestUnity">
<lifetime type="transient" />
<property name="BaseString" value="IChildInterface_BaseString" />
<property name="ChildString" value="IChildInterface_ChildString" />
</register>
<register name="Test" type="TestUnity.ContainerClass, TestUnity" mapTo="TestUnity.ContainerClass, TestUnity">
<lifetime type="transient" />
<property name="impl" dependencyType="IBaseInterface" />
</register>
</container>
</unity>
</configuration>
最佳答案
TL;DR - 抱歉,这似乎是 Unity 的一个限制。
我不是构建 Unity 的团队的成员,因此我只能推测预期的行为,因为没有针对此特定场景明确记录。但是优先考虑 Unity 的其他行为,我预计它会覆盖之前注册的 InjectionProperty
。
您是正确的,对象构建器在查找用于构建请求解析的对象实例的策略时不考虑已注册的类型。对象生成器键由 mapTo 类型和名称组成(参见 NamedTypeBuildKey )。创建此键的方式是 Unity 对象生成器内部不可或缺的一部分。尝试将已注册的类型添加到管道中的此键将比您想要处理的自定义更多。而且我想不出另一种方法来引入您希望的基于注册类型的独立注册行为(除了不同的具体类或多个命名注册)。
关于c# - 固件 : Unexpected result upon unnamed registration of the same mapTo with different mapFrom with injectedProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949079/
尝试建立一些东西,但卡住了,错过了这里的任何错误。我想跟踪按钮点击,但从 #zip 输入中获取值,所以这就是我选择 mapTo 的原因。当我删除 mapTo 时,我可以跟踪点击并更改我在订阅函数中的语
我正在阅读 Akka Futures Guide我看到这句话: Also note that the Future returned by an Actor is a Future[Any] sinc
我要映射 Map至Map但我无法在 map 函数中返回 Map.Entry: itemsWithQuantity.mapTo(mutableMapOf(), { it.key.toString() t
之前我使用的是这段代码: private val mItems = ArrayList() (1..item_count).mapTo(mItems) { it } /* mItems will b
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
背景 我正在尝试使用 go-fed 的 apcore构建联合应用程序的框架。我已经实现了 apcore.Application 接口(interface),但在加载配置时卡住了。 当尝试将从 conf
我发现了一段我看不懂的代码。 我正在将 JSONArray 转换为 List。 Kotlin 在其 stdlib( link ) 中提供了函数 mapTo mapTo inline fun > Ite
试图了解 kotlin 中 map 和 mapTo 之间的区别。谁能帮我解释一下使用一些例子的区别 最佳答案 map 在内部创建一个新列表,并将其结果放入该列表,然后返回该列表: val mapRes
function c() { return Math.random(); } source$.pipe( map(a => c()) ).subscribe(v => c
我最近开始使用 Akka/Scala 进行编码,遇到了以下问题: 在范围内进行隐式转换,例如: implicit def convertTypeAtoTypeX(a: TypeA): TypeX =
在将相同的具体类注册到不同的未命名实例时,我们看到 Unity 出现意外行为。在这种情况下,注册似乎以意想不到的方式相互干扰。 我们为我们想要注入(inject)的其他应用程序提供了一个框架不同的界面
我们如何转换: Rx.Observable.timer(3000).mapTo({ id: 1 }) 到 RxJS 6? 例如,如果我们: import { Observable, timer }
我是一名优秀的程序员,十分优秀!