如何防止统一覆盖具有自动注册现有映射 [英] How to prevent Unity overwriting existing mappings with AutoRegistration
问题描述
container.RegisterTypes(
:统一3自动注册(由登记公约)等提供了新的功能, AllClasses.FromLoadedAssemblies(),//使用反射
WithMappings.FromMatchingInterface,//通过名称
WithName.Default匹配的接口来实现);
这代码将注册实现他们的名称相似的界面,对这些接口的所有类型。例如,类的MyService:IMyService 将自动好像你已经写了下面的注册:
container.RegisterType< IMyService,为MyService>();
我的问题:如果我想这大部分的时间,但我想选择一个不同的实现对于我的接口之一,虽然名称相似的实施存在?
请参阅: CodePlex上的模式和做法
阅读解释你为什么会想这样做是杰里米·米勒的
的约定优于配置文章
什么阻止你与配置加载自定义的设置覆盖自动映射(其中 - 如果是空的 - 也就是说,没有默认映射覆盖):
//有你的自动注册
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),//使用反射
WithMappings.FromMatchingInterface,//使用名称匹配的接口来实现
WithName.Default);
//并覆盖有必要时,
container.LoadConfiguration();
其中,配置是
<?XML版本=1.0编码=UTF-8>?;
<统一的xmlns =http://schemas.microsoft.com/practices/2010/unity>
<集装箱>
<注册类型=IMyServicemapTo =OverriddenServiceImpl/>
< /容器>
< /统一性GT;
或
<?XML版本=1.0编码=UTF-8>?;
<统一的xmlns =http://schemas.microsoft.com/practices/2010/unity>
<集装箱>
...什么都没有,不重写默认值...
< /容器>
< /统一性GT;
移动可选配置到XML文件具有这样的优点 - 你可以重新配置系统,而不需要重新编译它。
Unity 3 offers new capabilities for AutoRegistration (Registration by Convention) such as:
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(), //uses reflection
WithMappings.FromMatchingInterface, //Matches Interfaces to implementations by name
WithName.Default);
This code will register all types that implement their similarly named interfaces, against those interfaces. For example, class MyService : IMyService will be registered automatically as though you had written the following:
container.RegisterType<IMyService, MyService >();
My Question: What if I want this most of the time, but I want to choose a different implementation for one of my interfaces, even though a similarly named implementation exists?
See: Patterns and practices on CodePlex
An important article to read explaining why you would want to do this is Jeremy Miller's Convention Over Configuration article
What stops you from overriding the automated mapping with a custom set loaded from configuration (which - if empty - means that no default mapping is overridden):
// have your auto registration
container.RegisterTypes(
AllClasses.FromLoadedAssemblies(), //uses reflection
WithMappings.FromMatchingInterface, //Matches Interfaces to implementations by name
WithName.Default);
// and override it when necessary
container.LoadConfiguration();
where the configuration is
<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
<register type="IMyService" mapTo="OverriddenServiceImpl" />
</container>
</unity>
or
<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
...nothing, do not override defaults ...
</container>
</unity>
Moving the optional configuration to an XML file has the advantage - you can reconfigure the system without the need to recompile it.
这篇关于如何防止统一覆盖具有自动注册现有映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!