Can Automapper be used in a console application?

Viewed 13490

Is it possible to use automapper in a console application?

Its Getting Started Page suggests the bootstrapper class be called from Application start up, but there are no further details about a class to add and call from Main().

How do I go about using this in a simple console app?

3 Answers

I know that this is an old question, but if you found this I want to add an update: Automaper does not allow static initialization anymore.

You can check more here

Below, I'm providing a full example of how to use it on a console app. Hope this might be helpful for someone in the future.

class Program
{
    static void Main(string[] args)
    {
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<MyClass, MyClassDTO>();
        });
        IMapper mapper = config.CreateMapper();

        var myClass = new MyClass(){
            Id = 10,
            Name = "Test"
        };
        var dst = mapper.Map<MyClass, MyClassDTO>(myClass);

        Console.WriteLine(dst.Id);
    }
}

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

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

Do not forget to include using AutoMapper;

Related