为通用接口和类对指定默认的 Unity 类型映射
我们使用基于构造函数的依赖注入,AutoMapper 和 AutoMapper 。 codeplex.com/unity" rel="nofollow noreferrer">代码库上的 Unity。
我们用通用接口包装了 AutoMapper...
public interface IMapper<TSource, TDestination>
{
TDestination Map(TSource source);
}
以及实现该接口的类...
public class AutomaticMapper<TSource, TDestination> : IMapper<TSource, TDestination>
{
public TDestination Map(TSource source)
{
return AutoMapper.Mapper.Map<TSource, TDestination>(source);
}
}
这效果很好,但这意味着对于我们在 AutoMapper 配置中定义的每个映射,我们需要执行额外的 UnityContainer.RegisterType 。
这些类型映射几乎总是采用以下形式:
container.RegisterType<IMapper<ClassA, ClassB>, AutomaticMapper<ClassA, ClassB>>();
有什么方法可以告诉 Unity 使用默认类型映射,使用相同的 从
和 IMapper
映射到 AutomaticMapper
它们各自的 TSourceTDestination
?
We're using constructor-based dependency injection, AutoMapper and Unity on a codebase.
We have wrapped AutoMapper with a generic interface...
public interface IMapper<TSource, TDestination>
{
TDestination Map(TSource source);
}
And a class that implements this interface...
public class AutomaticMapper<TSource, TDestination> : IMapper<TSource, TDestination>
{
public TDestination Map(TSource source)
{
return AutoMapper.Mapper.Map<TSource, TDestination>(source);
}
}
This works well, but it means that for every mapping we define in the AutoMapper configuration we need to perform an additional UnityContainer.RegisterType
.
These type mappings are almost always of the form
container.RegisterType<IMapper<ClassA, ClassB>, AutomaticMapper<ClassA, ClassB>>();
Is there any way that we can tell unity to use a default type mapping that maps from IMapper
to AutomaticMapper
using the same TSource
and TDestination
for each of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们实际上做了几乎完全相同的事情。在Unity中,你可以说:
We actually do almost the same exact thing. In Unity, you can say:
Unity 有一个自动注册插件,可能可以满足您的需求。查看 http://unity.codeplex.com/Thread/View。 aspx?ThreadId=59418
There's an auto registration add-in for Unity that probably does what you want. Have a look at http://unity.codeplex.com/Thread/View.aspx?ThreadId=59418