Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper - why use Mapper.Initialize?

Tags:

c#

automapper

I wouldn't normally ask this kind of question on here, but unfortunately whilst AutoMapper seems to be a good mapping library, its documentation is woefully bad - there is no XML documentation for the library's methods, and the most official online documentation I could find was this, which is very brisk. If anyone has any better documentation, please let me know.

That said, here's the question: why use Mapper.Initialize? It doesn't seem to be required as you can just use Mapper.CreateMap immediately, and as there is no documentation I have no clue what Initialize is meant to do.

like image 217
Jez Avatar asked Oct 19 '12 15:10

Jez


People also ask

Should I use Auto Mapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

What is the use of Mapper in C#?

The AutoMapper in C# is a mapper between two objects. That is AutoMapper is an object-object mapper. It maps the properties of two different objects by transforming the input object of one type to the output object of another type.

Should AutoMapper be a singleton?

Your configuration (e.g. Automapper Profiles) are singletons. That is, they are only ever loaded once when your project runs. This makes sense since your configurations will not change while the application is running.

Where do I configure AutoMapper?

Create a MapperConfiguration instance and initialize configuration via the constructor: var config = new MapperConfiguration(cfg => { cfg. CreateMap<Foo, Bar>(); cfg. AddProfile<FooProfile>(); });


1 Answers

I asked on the AutoMapper users list, and this answer basically says why:

https://groups.google.com/forum/?fromgroups=#!topic/automapper-users/0RgIjrKi28U

It's something to do with allowing AutoMapper to do deterministic (stochastic) optimization. Performance-wise, it's better to get all your mappings created in the Initialize call.

like image 113
Jez Avatar answered Oct 02 '22 17:10

Jez