How to configure AutoMapper in a WindowsForm project?

2

How do I configure AutoMapper in a Windows Forms App, where:

  • Which file should I configure to load with the application and be available?
  • How to use with a simple example?
asked by anonymous 10.11.2017 / 14:39

1 answer

1

To configure AutoMapper in an application Windows Forms , follow these 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; }
}

After the 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 Load() method, as in the example and in the InitializeAutoMapper the settings will be called, it is a configuration form that can undergo changes ( this is an example ).

To work with this in your application, go to 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 that it becomes available in your project. One note is that this method should be called before Application.Run(new Form1()); otherwise this setting will not load properly.

Full 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 it's easy a simple example would be:

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);
This is a very simple example to exemplify the tutorial .

10.11.2017 / 14:52