Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper Migrating from static API

https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static-API

this change breaks my system.

Before update, I use:

===> Startup.cs

public class Startup {     public Startup(IHostingEnvironment env)     {     ...         MyAutoMapperConfiguration.Configure();     } } 

===> MyAutoMapperConfiguration.cs

public class MyAutoMapperConfiguration {     public static void Configure()     {         Mapper.Initialize(a =>         {             a.AddProfile<AbcMappingProfile>();             a.AddProfile<XyzMappingProfile>();             a.AddProfile<QweMappingProfile>();         });     } } 

===> AbcMappingProfile.cs

public class AbcMappingProfile : Profile {     protected override void Configure()     {         Mapper.CreateMap<AbcEditViewModel, Abc>();         Mapper.CreateMap<Abc, AbcEditViewModel>();         ...     } } 

ERROR:

'Mapper.CreateMap()' is obsolete: 'The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instanace.'

I can use Mapper.Map. Now How can I use it

like image 419
Asp.net Avatar asked Feb 07 '16 16:02

Asp.net


People also ask

When should I use AutoMapper?

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.

Is AutoMapper bidirectional?

What is AutoMapper Reverse Mapping in C#? The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping.

How does AutoMapper work 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.

How to install automapper in ASP NET Core web API?

Then select ASP.NET Core Web Application and click on the Next button. Give the project name and click on the Create button. After that select API and click on Create button. So now your ASP.NET Core Web API project is setup. The above package will automatically install the AutoMapper package for us since it references the same package.

What happened to the static functions in automapper?

So back in January, Jimmy Bogard marked the static functions in AutoMapper 4.2 as obsolete with the intention of eventually removing them. As I work towards the 4.2 release of AutoMapper, I got a little inspiration.

How does automapper work with LINQ?

One of the features of AutoMapper is its support for a fluent API that works with LINQ expression chains. This feature requires the use of extension methods, which are always defined as static functions. The work around is to continue offering LINQ support, but in a way that doesn’t require the use of global state.

What is the history of automapper?

While originally based around the use of static functions, over time AutoMapper became more and more configurable. And with each new configuration option, more state had to be managed and more potential threading issues arose.


1 Answers

Instead of:

Mapper.CreateMap<AbcEditViewModel, Abc>(); 

The new syntax is:

var config = new MapperConfiguration(cfg => {   cfg.CreateMap<AbcEditViewModel, Abc>(); }); 

Then:

IMapper mapper = config.CreateMapper(); var source = new AbcEditViewModel(); var dest = mapper.Map<AbcEditViewModel, Abct>(source); 

(Source with more examples)

like image 89
NikolaiDante Avatar answered Sep 20 '22 19:09

NikolaiDante