Automapper与依赖注入

Automapper与依赖注入

本文介绍了Automapper与依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有以下映射:

  Mapper.CreateMap< Journal,JournalDto>(); 

现在,日记包含一个名为 RefTypeID ,其中相应的值存在于数据库中的另一个表中;要查找这个值,我有一个服务,它处理一个简单的 int - >字符串请求。 autoperperper配置目前在程序开始的静态类中发生。将映射代码移动到注入到我的DI容器中的类中还是有更好的方法吗?

解决方案

这是我如何解决它:



我定义了一个 IMappingCreator 界面:

  public interface IMappingCreator 
{
void CreateMappings();
}

我继续使用该界面实现了一个类(我正在使用MEF作为DI容器,这是属性来自的地方),它被放入DI容器中作为 IMappingCreator

  [Export(typeof(IMappingCreator))] 
public class Mapping:IMappingCreator
{
private readonly IRefTypesLookup iRefTypesLookup;


[ImportingConstructor]
public Mapping(IRefTypesLookup rtl)
{
iRefTypesLookup = rtl;
}

public void CreateMappings()
{
Mapper.CreateMap< Journal,DisplayJournal>()。AfterMap((j,dj)=> dj。 RefTypeName = iRefTypesLookup.Lookup(j.RefTypeID));
}
}

最后,在我的应用程序启动中,我抓取所有的实例的容器中的该接口,并调用 CreateMappings 方法:

  var mappings = container.GetExportedValues< IMappingCreator>(); 

foreach(映射中的IMappingCreator mc)
{
mc.CreateMappings();
}

这使得初始设置非常简单,因为所有的创建发生在一个地方,你可以拥有尽可能多的地图创建者(但是,你应该把它们保持在最低限度,也许每个项目一次,抓住所有需要的服务来映射该项目中的具体类型)。


I currently have the following mapping:

Mapper.CreateMap<Journal, JournalDto>();

Now, Journal contains a member named RefTypeID, which corresponding value exists in another table in the database; to look up this value, I have a service which handles a simple int -> string request. The automapper configuration currently happens in a static class at the start of the program. Is it okay to move the mapping code into a class which gets injected into my DI container or is there a better way?

解决方案

Here is how I solved it:

I defined an IMappingCreator interface:

public interface IMappingCreator
{
  void CreateMappings();
}

I went ahead and implemented a class with that interface (I'm using MEF as DI container, that's where the attributes are comming from) which is put into the DI container as IMappingCreator:

[Export(typeof(IMappingCreator))]
    public class Mapping : IMappingCreator
    {
        private readonly IRefTypesLookup iRefTypesLookup;


        [ImportingConstructor]
        public Mapping(IRefTypesLookup rtl)
        {
            iRefTypesLookup = rtl;
        }

        public void CreateMappings()
        {
            Mapper.CreateMap<Journal, DisplayJournal>().AfterMap((j, dj) => dj.RefTypeName = iRefTypesLookup.Lookup(j.RefTypeID));
        }
    }

Finally, in my application startup, I fetch all instances of that interface in the container and call the CreateMappings method on them:

    var mappings = container.GetExportedValues<IMappingCreator>();

    foreach (IMappingCreator mc in mappings)
    {
        mc.CreateMappings();
    }

This makes the initial setup quite easy, as all the creation happens in one place, and you can have as many mapping creators as you want (however, you should keep those to a minimum, maybe once per project or so, grabbing all needed services for mapping the specific types in that project).

这篇关于Automapper与依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:16