How to configure Auto mapper in class library project?

扶醉桌前 提交于 2019-12-03 05:36:15

问题


I am using auto mapping first time.

I am working on c# application and I want to use auto mapper.

(I just want to know how to use it, so I don't have asp.net app neither MVC app.)

I have three class library projects.

I want to write transfer process in the service project.

So I want to know how and where should I configure the Auto Mapper ?


回答1:


You can place the configuration anywhere:

public class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(x =>
            {
                x.AddProfile<MyMappings>();              
            });
    }
}

 public class MyMappings : Profile
{
    public override string ProfileName
    {
        get { return "MyMappings"; }
    }

    protected override void Configure()
    {
    ......
    }

But it has to be called by the application using the libraries at some point:

void Application_Start()
    {               
        AutoMapperConfiguration.Configure();
    }



回答2:


So based on Bruno's answer here and John Skeet's post about singletons I came up with the following solution to have this run only once and be completely isolated in class library unlike the accepted answer which relies on the consumer of the library to configure the mappings in the parent project:

public static class Mapping
{
    private static readonly Lazy<IMapper> Lazy = new Lazy<IMapper>(() =>
    {
        var config = new MapperConfiguration(cfg => {
            // This line ensures that internal properties are also mapped over.
            cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
            cfg.AddProfile<MappingProfile>();
        });
        var mapper = config.CreateMapper();
        return mapper;
    });

    public static IMapper Mapper => Lazy.Value;
}

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Source, Destination>();
        // Additional mappings here...
    }
}

Then in your code where you need to map one object to another you can just do:

var destination = Mapping.Mapper.Map<Destination>(yourSourceInstance);

NOTE: This code is based on AutoMapper 6.2 and it might require some tweaking for older versions of AutoMapper.




回答3:


Nobody outside of your library has to configure AutoMapper

I recommend that you use the instance based approach using an IMapper. That way no one outside your library has to call any configuration method. You can define a MapperConfiguration and create the mapper from there all inside the class library.

var config = new MapperConfiguration(cfg => {
    cfg.AddProfile<AppProfile>();
    cfg.CreateMap<Source, Dest>();
});

IMapper mapper = config.CreateMapper();
// or
IMapper mapper = new Mapper(config);
var dest = mapper.Map<Source, Dest>(new Source());



回答4:


Marko's answer is correct.

We can also go by a below simple solution.

 public static class ObjectMapper
{
    public static IMapper Mapper
    {
        get
        {
            return AutoMapper.Mapper.Instance;
        }
    }
    static ObjectMapper()
    {
        CreateMap();
    }
    private static void CreateMap()
    {
        AutoMapper.Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<SourceClass, DestinationClass>();
        });
    }
}
And we can use it like.
public class SourceClass
{
    public string Name { get; set; }
}
public class DestinationClass
{
    public string Name { get; set; }
}
SourceClass c1 = new SourceClass() { Name = "Mr.Ram" };

DestinationClass c2 = ObjectMapper.Mapper.Map<DestinationClass>(c1);


来源:https://stackoverflow.com/questions/26458731/how-to-configure-auto-mapper-in-class-library-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!