ASP.NET MVC 4.8 in VS 2019

238 views
Skip to first unread message

Daniel Patfield

unread,
Dec 16, 2020, 4:19:18 PM12/16/20
to AutoMapper-users
I know there is documentation on automapper - I've perused it.
My question is so simple, and yet I cannot seem to get a response to it.

I'm using .NET 4.8 ASP/MVC/API in Visual Studio 2019.

I have a mapping profile after installing automapper.

in the constructor of this profile, I have CreateMap<source, dest>()

In older automapper versions, the next step would be to initialize in the global.asax.

That method is now obsolete.  Please tell me in the most SIMPLEST of terms where I would initialize the profile, and how.  I want to centralize so I can use in various controllers.

that is my question, no more, no less.  I am not using core so Core instructions do not help.

thanks in advance.

Jimmy Bogard

unread,
Dec 16, 2020, 4:49:03 PM12/16/20
to automapper-users
https://docs.automapper.org/en/stable/Getting-started.html#how-do-i-use-automapper

var config = new MapperConfiguration(cfg => cfg.AddMaps(/* list of assemblies to scan for profiles */);


Creating a mapper:

var mapper = config.CreateMapper();
mapper.Map<source, dest>();

That config object can be static, so most folks inject it into a DI container. Or make a static property somewhere that your app can get to it.
--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/automapper-users/a599b942-0ac2-44fa-bd00-6212dcd5e568n%40googlegroups.com.

Chad Richardson

unread,
Dec 16, 2020, 5:26:49 PM12/16/20
to AutoMapper-users
I have the same question. I'm still using the Global.asax method to register the profiles and I see that that method has been deprecated.
We don't use DI in our project.

So do you have to call this every place in the code we do a map?

var config = new MapperConfiguration(cfg => cfg.AddMaps(/* list of assemblies to scan for profiles */);  
var mapper = config.CreateMapper();
mapper.Map<source, dest>();

I thought it was advised to only a CreateMapper once on app startup as it was a "heavy" operation. If not using DI, how do you do this effectively?

I realize this may be more of a question due to my lack of general C# development, more than an explicit AutoMapper question, but I'd appreciate any pointers you could give so I can migrate to the non-deprecated way and continue to upgrade AutoMapper in our project.

Jimmy Bogard

unread,
Dec 16, 2020, 7:06:45 PM12/16/20
to automapp...@googlegroups.com
If you’re using Global.asax, then I also assume you’re using Mapper.Initialize.

Instead of doing that, just “new” up a mapper configuration mapper in the same spot you used to, and make those values into a static field somewhere everything in the app can get to.

Application.Mapper.Map etc.

You don’t initialize every time, you just manage the static fields yourself (instead of AutoMapper doing this for you).

On Dec 16, 2020, at 4:26 PM, Chad Richardson <chadrich...@gmail.com> wrote:

I have the same question. I'm still using the Global.asax method to register the profiles and I see that that method has been deprecated.

Chad Richardson

unread,
Dec 16, 2020, 7:52:54 PM12/16/20
to AutoMapper-users
So to spell it out for myself and others. 

Today I have this:

Configuration:
Global.asax ---------------------------------------
protected void Application_Start()
{
    AutoMapperConfiguration.Configure();
}

public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(am =>
{              
am.AddProfile<ViewModelMaps.AdminViewModelMaps>();                
...
...
...
});

Mapper.AssertConfigurationIsValid();
}    
}

Using AutoMapper:

For object mapping:

public Item ConvertViewModelToClass(ItemViewModel viewModel)
{
return Mapper.Map<Item>(viewModel);
}

For IQueryable Projection:

public IQueryable<ItemViewModel> ProjectToViewModelQuery(IQueryable<Item> entityQuery)
{
return entityQuery.ProjectTo<ItemViewModel>();
}


So instead, you're saying to do this:

Configuration:

public static class AutoMapperConfiguration
{
public static Mapper Configure()
{
Mapper mapper = GetMapper();
Mapper.AssertConfigurationIsValid();
}    
public static Mapper GetMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg => {
/* list of assemblies to scan for profiles */
cfg.AddProfile<ViewModelMaps.AdminViewModelMaps>();
cfg.AddProfile<ViewModelMaps.OrderViewModelMaps>();
cfg.AddProfile<ViewModelMaps.ITemViewModelMaps>();
...
...
});  
return config.CreateMapper();
}
}

Using It:

AutoMapperConfiguration  

Using AutoMapper:

For object mapping:

public Item ConvertViewModelToClass(ItemViewModel viewModel)
{
   return AutoMapperConfiguration.GetMapper().Map<Item>(viewModel);
}

For IQueryable Projection ??????????:

public IQueryable<ItemViewModel> ProjectToViewModelQuery(IQueryable<Item> entityQuery)
{
    return entityQuery.ProjectTo<ItemViewModel>();
}

1.) how to use this method for IQueryable Projection?
2.) is the rest of this correct?
3.) Does the way I have Mapper.AssertConfigurationIsValid();  in the Configure() method work to just call that at app startup via Global.asax? 

Thanks in advance!

Jimmy Bogard

unread,
Dec 17, 2020, 8:30:10 AM12/17/20
to automapper-users
Not quite. You still only want initialization to happen once at startup. In that code, you've got it executing every time you call GetMapper()

Something like:


Then when you use it for ProjectTo, pass in that Config property.

Reply all
Reply to author
Forward
0 new messages