如何在ASP.NET MVC控制器中使用Automapper配置 [英] How to use Automapper configuration in ASP.NET MVC controllers

查看:262
本文介绍了如何在ASP.NET MVC控制器中使用Automapper配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AutoMapper将我的模型转换为视图模型。我已完成所有设置的配置,测试和工作。作为参考,这是我的configure方法的样子:

  public static MapperConfiguration Configure()
{
MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg => {
cfg.CreateMap< Ebill,EbillHarvesterTaskVM>()
cfg.CreateMap< Note,NoteVM>();
cfg.CreateMap< LoginItem ,LoginCredentialVM>()
cfg.CreateMap< Login,ProviderLoginVM>()
});

mapperConfiguration.CreateMapper();

return mapperConfiguration;
}

这是我的测试结果:

  public void ValidConfigurationTest()
{
var config = AutoMapperConfig.Configure();
config.AssertConfigurationIsValid();
}

我不了解的是如何访问它以实际映射一个对象从我的控制器中移到另一个。我知道我的应用程序启动时可以调用此配置方法,我有一个从global.asax调用的应用程序配置类,该类调用了我的automapper配置方法。我不确定如何从控制器内部访问所有这些。我已经读过一些有关依赖注入的内容,但是我对这意味着如何应用依赖并不十分熟悉。



我在其中使用了Automapper过去,但是我想我实现了现在不可用的静态API。配置方法如下:

  public static void RegisterMappings()
{
AutoMapper.Mapper .Initialize(cfg =>
{
cfg.CreateMap< ManagementCompany,ManagementCompanyViewModel>();
cfg.CreateMap< ManagementCompanyViewModel,ManagementCompany>();

});
}

在Global.asax中调用该配置

  AutoMapperConfig.RegisterMappings(); 

在哪里可以在控制器中调用以利用映射:

  AutoMapper.Mapper.Map(managementCompany,managementCompanyVM); 

这种方式不再起作用。当我键入AutoMapperMapper时,没有要调用的Map方法。为了能够访问我的映射并使用它们,我需要做些什么?

解决方案

 公共静态MapperConfiguration Configure(){
MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg => {
cfg.CreateMap< Ebill,EbillHarvesterTaskVM>()
cfg.CreateMap< Note,NoteVM>( );
cfg.CreateMap< LoginItem,LoginCredentialVM>()
cfg.CreateMap< Login,ProviderLoginVM>()
});

return mapperConfiguration;
}

构建映射器,并将其注册到应用程序使用的依赖容器中。 / p>

global.asax

  MapperConfiguration配置= AutoMapperConfig.Configure();; 

//构建映射器
IMapper mapper = config.CreateMapper();

// ..使用您的应用程序使用的依赖容器注册映射器。

myContainer.Register< IMapper>(映射器); // ...这只是一个过度简化的示例

更新您的控制器以通过以下方式显式依赖于映射器构造函数注入

 私有只读IMapper映射器; 

public MyController(IMapper mapper,...){
this.mapper = mapper;

// ...
}

然后调用

  // ... 

注意模型=映射器。 Map< Note>(noteVM);

// ...


I am using AutoMapper to convert my models into view models. I have the configuration all setup, tested, and working. For reference, this is what my configure method looks like:

    public static MapperConfiguration Configure()
    {
            MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg => {
                cfg.CreateMap<Ebill, EbillHarvesterTaskVM>()
                cfg.CreateMap<Note, NoteVM>();
                cfg.CreateMap<LoginItem, LoginCredentialVM>()
                cfg.CreateMap<Login, ProviderLoginVM>()
            });

            mapperConfiguration.CreateMapper();

            return mapperConfiguration;
     }

This is what my test looks like:

public void ValidConfigurationTest()
{
    var config = AutoMapperConfig.Configure();
    config.AssertConfigurationIsValid();
}

What I don't understand is how to access it to actually map one object to another from within my Controller. I know I can call this config method when my app starts up, I have an application configuration class that is called from global.asax that calls my automapper configuration method. I'm not sure how to access all of this from within the controller though. I've read things that say dependency injection, but I'm not familiar enough with what that means to know how to apply it.

I've used Automapper in the past, but I think I implemented the now unavailable static API. Where the config method looks like this:

public static void RegisterMappings()
    {
        AutoMapper.Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<ManagementCompany, ManagementCompanyViewModel>();
            cfg.CreateMap<ManagementCompanyViewModel, ManagementCompany>();

        });
    }

The configuration is called in Global.asax

AutoMapperConfig.RegisterMappings();

And where you can call this within a controller to utilize mapping:

AutoMapper.Mapper.Map(managementCompany, managementCompanyVM);

This way doesn't work anymore. When I type AutoMapperMapper there is no Map method to call. What do I need to do to be able to access my mappings and use them?

解决方案

public static MapperConfiguration Configure() {
        MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg => {
            cfg.CreateMap<Ebill, EbillHarvesterTaskVM>()
            cfg.CreateMap<Note, NoteVM>();
            cfg.CreateMap<LoginItem, LoginCredentialVM>()
            cfg.CreateMap<Login, ProviderLoginVM>()
        });

        return mapperConfiguration;
 }

build the mapper and register it with the dependency container used by your application.

global.asax

MapperConfiguration config = AutoMapperConfig.Configure();;

//build the mapper
IMapper mapper = config.CreateMapper();

//..register mapper with the dependency container used by your application.

myContainer.Register<IMapper>(mapper); //...this is just an oversimplified example

Update your controllers to explicitly depend on the mapper via constructor injection

private readonly IMapper mapper;

public MyController(IMapper mapper, ...) {
    this.mapper = mapper;

    //...
}

And call the mapper as needed in the controller actions.

//...

Note model = mapper.Map<Note>(noteVM);

//...

这篇关于如何在ASP.NET MVC控制器中使用Automapper配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆