开发者

Where to define AutoMapper mappings?

Where in my ASP.NET MVC application should I define my AutoMapper mappings?

Mapper.CreateMap<User, UserVi开发者_JAVA百科ewModel>();

Presently I am defining these in the constructor of a BaseController, which all my Controlllers derive from. Is this the best place?


I think it's kind of late to answer the question, but maybe someone can use my answer.

I use Ninject to resolve dependencies, so I've created Ninject Module for AutoMapper. Here is the code:

public class AutoMapperModule : NinjectModule
{
    public override void Load()
    {
        Bind<IConfiguration>().ToMethod(context => Mapper.Configuration);
        Bind<IMappingEngine>().ToMethod(context => Mapper.Engine);

        SetupMappings(Kernel.Get<IConfiguration>());

        Mapper.AssertConfigurationIsValid();
    }

    private static void SetupMappings(IConfiguration configuration)
    {
        IEnumerable<IViewModelMapping> mappings = typeof(IViewModelMapping).Assembly
            .GetExportedTypes()
            .Where(x => !x.IsAbstract &&
                        typeof(IViewModelMapping).IsAssignableFrom(x))
            .Select(Activator.CreateInstance)
            .Cast<IViewModelMapping>();

        foreach (IViewModelMapping mapping in mappings)
            mapping.Create(configuration);
    }
}

As you can see on load it scans assembly for implementaion of IViewModelMapping and then runs Create method.

Here is the code of IViewModelMapping:

interface IViewModelMapping
{
    void Create(IConfiguration configuration);
}

Typical implementation of IViewModelMapping looks like this:

public class RestaurantMap : IViewModelMapping
{
    public void Create(IConfiguration configuration)
    {
        if (configuration == null)
            throw new ArgumentNullException("configuration");

        IMappingExpression<RestaurantViewModel, Restaurant> map =
            configuration.CreateMap<RestaurantViewModel, Restaurant>();
// some code to set up proper mapping

        map.ForMember(x => x.Categories, o => o.Ignore());
    }
}


As mentioned in this answer, AutoMapper has now introduced configuration profiles to organise your mapping configuration.

So for example, you could define a class to set up your mapping configuration:

public class ProfileToOrganiseMappings : Profile 
{
    protected override void Configure() 
    {
        Mapper.CreateMap<SourceModel, DestinationModel>();
        //other mappings could be defined here
    }
}

And then define a class to initialise the mappings:

public static class AutoMapperWebConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile(new ProfileToOrganiseMappings());
            //other profiles could be registered here
        });
    }
}

And then finally, call that class in your global.asax Application_Start() to configure those mappings:

protected void Application_Start()
{
    ...
    AutoMapperWebConfiguration.Configure();
}


The code you are referencing looks like AutoMapper, not StructureMap.

If you're using the static Mapper method, configuration only needs to happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications. Typically, the configuration bootstrapper class is in its own class, and this bootstrapper class is called from the startup method.

http://automapper.codeplex.com/wikipage?title=Getting%20Started&referringTitle=Home

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜