How to configure Automapper in a Windowsform project?

Asked

Viewed 362 times

2

How do I set up Automapper in a Windows Forms app, being:

  • Which file and should I configure to load together with the application and make it available?
  • How to use with a simple example?
  • @still an example!

1 answer

1


To configure the Automapper in an application Windows Forms, follow the following steps:

Install the package via NUGET

PM> Install-Package AutoMapper

Create the templates that should be used in your project:

public class ClassA
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ClassB
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ClassC
{
    public int Id { get; set; }
    public string Name { get; set; }
    public System.DateTime? DateCreated { get; set; }
}

public class ClassD
{
    public int Id { get; set; }
    public string Name { get; set; }
    public System.DateTime? DateCreated { get; set; }
}

and after installation of the package and the classes that will be used open the file Program.cs and create two methods:

public static void InitializeAutoMapper()
{
    AutoMapper.Mapper.Initialize(Load());
}

public static Action<AutoMapper.IMapperConfigurationExpression> Load()
{
    return _ =>
    {
        _.CreateMap<ClassA, ClassB>();
        _.CreateMap<ClassC, ClassD>();
        //_.CreateMap add mais configuração.
    };
}

All classes to be configured do in the method Load(), as in the example and in the InitializeAutoMapper will be called settings, it is a form of configuration that can undergo changes (this is an example).

To work this on your application, go on the method Main of Program.cs and add the line InitializeAutoMapper(); as follows:

[STAThread]
static void Main()
{
    InitializeAutoMapper(); // adicionando para ficar global na sua app.
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);            
    Application.Run(new Form1());            
}

and with it becomes available in your project. One observation is that this method should be called before Application.Run(new Form1()); if this setting will not be loaded correctly.

Complete code:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        InitializeAutoMapper(); // adicionando para ficar global na sua app.
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);            
        Application.Run(new Form1());            
    }

    public static void InitializeAutoMapper()
    {
        AutoMapper.Mapper.Initialize(Load());
    }

    public static Action<AutoMapper.IMapperConfigurationExpression> Load()
    {
        return _ =>
        {
            _.CreateMap<ClassA, ClassB>();
            _.CreateMap<ClassC, ClassD>();
            //_.CreateMap add mais configuração.
        };
    }
}

To use is easy a very simple example would:

var b = new ClassB
{
    Id = 10,
    Name = Guid.NewGuid().ToString()
};
var c = new ClassC
{
    Id = 10,
    Name = Guid.NewGuid().ToString(),
    DateCreated = DateTime.Now
};

ClassA result0 = AutoMapper.Mapper.Map<ClassA>(b);
ClassD result1 = AutoMapper.Mapper.Map<ClassD>(c);

Note: this is a very simple example to exemplify the tutorial.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.