Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Automapper be used in a console application?

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?

like image 461
StuperUser Avatar asked Jul 18 '11 14:07

StuperUser


1 Answers

You can initialize Automapper in the console startup, there's no limitations; the Application_start is the startup place for a web program in .net/iis, ie code that is called only once. Any configuration that you must call at the start of a web project goes in this method.

edit for comment: if you don't want to create your mappings on the fly, but would rather have a place to initialize all your mappings, just create a function called InitializeAutomapper and make the Mapper.Configure<X, Y> calls in here. Then in your Main() method, just call the function. There are lots of ways to handle configuration, but this is the simpler way to handle it.

code sample

class Program
    {
        static void Main(string[] args)
        {
            // the app is starting here
            InitializeAutomapper();
            // we're configured, let's go!
            DoStuff();
        }

        static void InitializeAutomapper()
        {
            AutoMapper.Mapper.CreateMap<TypeA, TypeB>();
            AutoMapper.Mapper.CreateMap<TypeC, TypeD>();
            AutoMapper.Mapper.CreateMap<TypeE, TypeF>();
        }
    }
like image 102
samy Avatar answered Oct 19 '22 07:10

samy