- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在使用 Unity 容器并注意到一个奇怪的行为。我有一个类,它实现了多个接口(interface)。我希望此类在具有不同生命周期的应用程序的不同位置使用。所以我已经将 IFooDerived1 映射到 Foo 作为 Singleton,并将 IFooDerived2 映射到 Foo 作为 Transient。但是 Foo 的任何注册都会破坏 Foo 之前的注册。
示例:
interface IFoo { }
interface IFooDerived1 { }
interface IFooDerived2 { }
class Foo : IFoo, IFooDerived1, IFooDerived2 { }
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType(typeof(IFoo), typeof(Foo), new ExternallyControlledLifetimeManager());
container.RegisterType(typeof(IFooDerived1), typeof(Foo), new ContainerControlledLifetimeManager());
container.RegisterType(typeof(IFooDerived2), typeof(Foo), new TransientLifetimeManager());
foreach(var r in container.Registrations)
{
Console.WriteLine("{0} -> {1} : {2}", r.RegisteredType.Name, r.MappedToType.Name, r.LifetimeManagerType.Name);
}
}
输出:
IFoo -> Foo : TransientLifetimeManager
IFooDerived1 -> Foo : TransientLifetimeManager
IFooDerived2 -> Foo : TransientLifetimeManager
这是正确的行为吗?任何人都可以给出一些合乎逻辑的解释吗?我可以很容易地使用其他一些方法,我只是想了解为什么会这样。谢谢。
最佳答案
如果您将注册更改为以下内容:
container.RegisterType(typeof(IFooDerived1), typeof(Foo), new ContainerControlledLifetimeManager());
container.RegisterType(typeof(IFooDerived2), typeof(Foo), new TransientLifetimeManager());
container.RegisterType(typeof(IFoo), typeof(Foo), new ExternallyControlledLifetimeManager());
您将获得以下输出:
IFoo -> Foo : ExternallyControlledLifetimeManager
IFooDerived1 -> Foo : ExternallyControlledLifetimeManager
IFooDerived2 -> Foo : ExternallyControlledLifetimeManager
什么?无论你检索什么抽象,你总是会得到相同的实例。因此,只需更改注册顺序,您就会获得完全不同的生活方式。
顺便说一句,ExternallyControlledLifetimeManager
太可怕了,因为它使用了 weak reference to the object并且可以在应用程序不再保留对它的引用后重新创建它。您几乎不应该使用这种生活方式。
我正在努力解决这个问题,但似乎在 Unity 中,当您进行注册时,您实际上是在以某种生活方式注册实现,而提供的抽象只是映射到该注册。因此,如果我们为 Unity 定义一个替代 API,注册将变成这样:
MakeEntry(typeof(Foo), new ExternallyControlledLifetimeManager());
MapToEntry(from: typeof(IFoo), to: typeof(Foo));
因此,使用这个“替代”API,可以更清楚地看到正在发生以下情况:
MakeEntry(typeof(Foo), new ExternallyControlledLifetimeManager());
MapToEntry(from: typeof(IFoo), to: typeof(Foo));
MakeEntry(typeof(Foo), new ExternallyControlledLifetimeManager());
MapToEntry(from: typeof(IFooDerived1), to: typeof(Foo));
MakeEntry(typeof(Foo), new TransientLifetimeManager());
MapToEntry(from: typeof(IFooDerived2), to: typeof(Foo));
这意味着同一个 Foo
有三个条目,显然 Unity 只是默默地接受它,最后一个调用获胜。你不讨厌这种含蓄的行为吗?
也许是时候切换到不同的库了?
关于c# - Unity RegisterType 与 LifetimeManager 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26025999/
在撰写本文时,Unity 有一个名为 Unity LTS 2017.4.12f1 的版本,Unity 的最新版本是 2018.2.11 我知道 Unity LTS 应该是稳定版本,但这是否意味着 Un
我需要 Unity 来捕获所有按键,即使 Unity 没有焦点。 我尝试过使用: Input.KeyPress() 但这似乎只有在 Unity 拥有用户输入焦点的情况下才有效。我需要它在没有焦点时工作
我正在尝试统一实现一个TCP服务器。我正在使用 unity pro 3.5,当我在场景中运行此代码时,unity 挂起,完全没有响应,直到我用任务管理器杀死它。 using UnityEngine;
我想问一下,使用新的unity ads 2.0与unity ads相比,收入有什么区别 Unity Ads Unity Ads 2.0 最佳答案 Unity 广告支持 Unity 4.2.2 或更高版
当我运行我的应用程序时,我希望 Unity 打开两个窗口。 window 将有不同的摄像头,但两者都会看到同一个世界。 这样的事情可能吗? (我还没有找到任何证据表明这一点) 我知道我可以通过两个 U
我使用Unity Hub下载了最新的Unity编辑器,它对于编辑器、文档和语言包运行良好,但无法下载android构建支持。刚刚告诉我这两天下载失败很多次。 所以我从网页下载了UnitySetup-A
我使用Unity Hub下载了最新的Unity编辑器,它对于编辑器、文档和语言包运行良好,但无法下载android构建支持。刚刚告诉我这两天下载失败很多次。 所以我从网页下载了UnitySetup-A
我今天已将我的项目升级到 Prism 6.3.0 和 Unity 5.3.1。在此之前,我有 Prism 5 和 Unity 4。 现在我遇到了 Prism.Unity.UnityBootstrapp
Unity 中是否有与 StructureMap 中的 Registry 类等效的内容? 我喜欢考虑一个层/组件/库来自行配置它 - 从而填充容器。所以“父”层只需要知道注册类。 最佳答案 不,没有。
我似乎无法在任何地方找到 Microsoft.Practices.Unity.StaticFactory.dll。 还有其他注册静态工厂的方法吗? 寻找这样的东西 container.Register
是否可以统一尝试所有已定义的构造函数,从参数最多的构造函数到最不具体的构造函数(默认构造函数)? 编辑 我的意思是说: foreach (var constructor in concrete.Get
我有一个正在运行且运行良好的 Unity 应用程序,但我们目前正在通过对所有编译警告采取行动来清理我们的代码。 由于过时的 Microsoft.Practices.Unity.Configuratio
我正在使用 Visual Studio Code 在 Unity 中编写脚本。在“编辑”-“首选项”-“外部工具”-“外部脚本编辑器”下,我也选择了 VS Code。 打开脚本工作正常,但是当我尝试通
因此,我很确定这不是提出此类问题的正确论坛,因此我非常感谢有人将我链接到针对此问题的更好论坛(如果需要)。 我的问题: 在 Unity Hub 中,我进行了设置,以便 Unity 编辑器应下载到我的硬
问题:即使在 Cardboard 相机范围内,我也无法在任何地方看到我的 UI 文本。 截图: 我使用的是Unity 5.4.0b21版本,有人说可以通过降级到Unity 5.3版本来修复。 但是,我
我正在开发一个 Unity 项目,我正在使用 Google VR SDK for Unity 和 FirebaseMessaging.unitypackage 适用于 Unity 的 Firebase
好吧,在谷歌、这里和几个 ASP/MVC 论坛上搜索之后,我一定要问我到底做错了什么。 我的应用程序有一个良好的开端,对 DI、IoC 有很好的理解,并且正在使用 Repository、Service
我们最近将项目中的 Microsoft Unity 从 3.5.1404 版本升级到 5.8.6。在我们的代码中只做了一些小的调整,这次升级似乎很容易。它毫无问题地解决了我们所有注册的实例。但是,我们
我正在弄清楚使用 Unity 应用程序 block 时的意外行为。我有项目 A 作为我的启动项目。 项目 A 具有对项目 B 的项目引用,而项目 B 具有对项目 C 的项目引用。 项目 A 使用 un
我将 Unity 与 MVC 和 NHibernate 结合使用。不幸的是,我们的 UnitOfWork 驻留在不同的 .dll 中,并且它没有默认的空 .ctor。这是我注册 NHibernate
我是一名优秀的程序员,十分优秀!