15

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?

StuperUser
  • 10,555
  • 13
  • 78
  • 137

3 Answers3

13

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>();
        }
    }
samy
  • 14,832
  • 2
  • 54
  • 82
  • I'm not sure which part of the answer you find confusing. `Application_Start` is more or less comparable to the `Main` method in your console application. You can initialize it there if all of your methods are static, or in the constructor for your application if you plan to use instance methods. – Roman Jul 18 '11 at 14:50
  • 1
    I'm getting `AutoMapper does not exist in the current context` errors where I have my `CreateMap` and `Map` calls despite having a reference to the AutoMapper.dll. If that mapping is all the initialisation needed, then I'll investigate why the reference isn't working. – StuperUser Jul 18 '11 at 15:00
  • @StuperUser: try to post the code for the initialization; the code i posted works (provided there's a TypeA to TypeF and a DoStuff method) – samy Jul 18 '11 at 15:04
  • @samy That's giving similar errors, the reference is playing silly buggers. It's in the References directory in the Solution Explorer, but the AutoMapper namespace disappears from the Object Browser even after project restarts/re-additions. – StuperUser Jul 18 '11 at 15:06
  • 1
    @samy, It seems like AutoMapper needs System.Web too. – StuperUser Jul 18 '11 at 15:13
  • 1
    @R0MANARMY The confusion came from question not containing any details as to what else needed to be configured, since my mapping is done on the fly, I didn't need to configure anything. My issues were caused by an apparent dependency on System.Web, which added by a reference fixes the issue. – StuperUser Jul 18 '11 at 15:16
12

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;

lwb
  • 379
  • 5
  • 17
0

Yes, but it appears to have a dependency on System.Web, which must be included too.

(See Mysterious disappearing reference for more details)

Community
  • 1
  • 1
StuperUser
  • 10,555
  • 13
  • 78
  • 137
  • That's really strange, you shouldn't have these problems since System.Web lives in the GAC and is found by the ... anyway, good catch :) For the record, i used NuGet to quickly test the solution i posted, but i can't find a hard reference to System.Web in the console project. – samy Jul 18 '11 at 15:18
  • 2
    @samy: It's probably related to [this](http://automapper.codeplex.com/workitem/5402). There's no direct dependency, but a dependency of a dependency. – Roman Jul 18 '11 at 18:42
  • @StuperUser Were you using the Client Profile Framework? If so, R0MANARMY may have found the reason with his link – samy Jul 19 '11 at 06:27
  • @samy @R0MANARMY That was what was in a new example console project by default, adding System.Web it updated the project's target framework to `.NET Framework 4`. @R0MANARMY cracked it. – StuperUser Jul 19 '11 at 09:30