How to boostrap AutoMapper to work with ASP.NET (not Core) and EF to use ProjectTo

46 views
Skip to first unread message

José Manuel Nieto Sánchez

unread,
Jan 31, 2018, 8:57:10 AM1/31/18
to AutoMapper-users

I would like to use AutoMapper in a ASP.NET 4.5 Application that currently uses Unity to resolve dependencies.

My goal is to be able to use the ProjectTo extension method to make projections of my entities (from the AutoMapper.EF6 NuGet package).

Could you, please, explain how do I boostrap it?

In ASP.NET Core this is done adding the NuGet package:

AutoMapper.Extensions.Microsoft.DependencyInjection

And adding this:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAutoMapper(typeof(Startup).Assembly);      
    ...
}

But, that's Core. How do it achieve the same in classic ASP.NET with Unity?

Big thanks! 

Jimmy Bogard

unread,
Jan 31, 2018, 9:12:01 AM1/31/18
to automapper-users

--
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-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Manuel Nieto Sánchez

unread,
Jan 31, 2018, 10:54:42 AM1/31/18
to AutoMapper-users
Thanks Jimmy! However, I still don't know how to proceed.

In a few words, what I really don't understand is how to use the Queryable extensions for AutoMapper and EF6.

For instance, inside this Controller

public MyController : ApiController
{

   
private MyContext _context;

   
public MyController(MyDbContext context)
   
{


         _context
= context;
   
}

   
[HttpGet]
   
public Task<SomeDto> Get(int id)
   
{
         _context
.Users
             
.Where(x => x.Id == id)
             
.ProjectTo<UserDto>()
             
.FirstOrDefault();
   
}
}

How does the ProjectTo know what instance of IMapper to use? I mean the ProjectTo doesn't accept an instance of IMapper, so, how does it work? Does it use the static façade (like using Mapper.Map<...>?

Big thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.

Jimmy Bogard

unread,
Jan 31, 2018, 11:27:48 AM1/31/18
to automapper-users
It accepts the configuration object instead, which you can register statically. The IMapper object is really only for in-memory mappings. The LINQ projections only need to look at configuration to do mapping.

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.

José Manuel Nieto Sánchez

unread,
Jan 31, 2018, 12:28:52 PM1/31/18
to AutoMapper-users
OK! I think now I get it! I'm going to do some tests to see if I'm getting it. Thank you, master!
Reply all
Reply to author
Forward
0 new messages