Search code examples
c#automapperautomapper-2automapper-3

How to configure Auto mapper in class library project?


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.

enter image description here

I want to write transfer process in the service project.

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


Solution

  • 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();
        }