打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# – Mapper已初始化

我有一个3层架构Web Api解决方案,里面有3个项目:数据,业务和表示层.我需要在两个业务和表示层中初始化两个不同的映射器.

我已经创建了一个静态类和方法来初始化业务逻辑中的一个映射器:

using AutoMapper;using Shop.BLL.DTOModels;using Shop.DAL.Models;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Shop.BLL.InitMapper{    public static class InitializeMapperBLL    {        public static void RegisterMappings()        {            Mapper.Initialize(cfg => cfg.CreateMap<Category, DTOCategoryModel>());        }    }}

并称之为:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Shop.DAL.Repositories;using AutoMapper;using Shop.BLL.DTOModels;using Shop.DAL.Models;using Shop.BLL.Interfaces;using Shop.DAL.Interfaces;using Shop.BLL.InitMapper;namespace Shop.BLL.Services{    public class CategoryService : ICategoryService    {        IUnitOfWork Database { get; set; }        public CategoryService(IUnitOfWork uow)        {            Database = uow;        }        public IEnumerable<DTOCategoryModel> GetCategories()        {//I call it here            InitializeMapperBLL.RegisterMappings();            return Mapper.Map<IEnumerable<Category>, List<DTOCategoryModel>>(Database.Categories.GetAll());        }        public void Dispose()        {            Database.Dispose();        }    }}

在表示层我做同样的事情:

using AutoMapper;using Shop.API.ViewModels;using Shop.BLL.DTOModels;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Shop.API.MapperInit{    public static class InitializeMapperAPI    {        public static void RegisterMappings()        {            Mapper.Initialize(cfg => cfg.CreateMap<DTOCategoryModel, CategoryViewModel>());        }    }}

并调用Global.asax

protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            GlobalConfiguration.Configure(WebApiConfig.Register);            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            RouteConfig.RegisterRoutes(RouteTable.Routes);            BundleConfig.RegisterBundles(BundleTable.Bundles);      //here I call it            InitializeMapperAPI.RegisterMappings();            CreateKernel();        }

我已经初始化了错误Mapper.您必须为每个应用程序域/进程调用一次Initialize.

如何解决这个问题呢?

解决方法:

您可以使用反射和自动播放器配置文件的一种方法.这在我用过的项目中非常有效.

在每个项目/图层中创建一个automapper配置文件类.每个配置文件类应仅包含它自己需要的映射.以下是其中一个的示例:

  //Profile here is of type AutoMapper.Profile  public class BusinessLayerMapperConfig : Profile  {    public BusinessLayerMapperConfig()    {      //create layer specific maps      CreateMap<MyObjectDTO, MyObjectViewModel>();    }    public override string ProfileName    {      get { return this.GetType().ToString(); }    }  }

然后真正靠近应用程序入口点的地方(我从Global.asax.cs中的ApplicationStart方法调用以下方法),初始化所有的配置文件,如下所示:

public static void RegisterMaps()    {      //get all projects' AutoMapper profiles using reflection      var assembliesToScan = System.AppDomain.CurrentDomain.GetAssemblies();      var allTypes = assembliesToScan.SelectMany(a => a.ExportedTypes).ToArray();      var profiles =          allTypes              .Where(t => typeof(Profile).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))              .Where(t => !t.GetTypeInfo().IsAbstract);      //add each profile to our static AutoMapper      Mapper.Initialize(cfg =>      {        foreach (var profile in profiles)        {          cfg.AddProfile(profile);        }      });    }

这将允许您按照它们使用的图层逻辑分隔您的地图,并确保您只初始化它们一次.

来源:https://www.icode9.com/content-1-315301.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
对象到对象映射-AutoMapper
【C#】AutoMapper 使用手册
对象映射工具AutoMapper介绍
ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射
java获取系统信息
ASP.NET Core MVC 从入门到精通之自动映射(一)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服