小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c# – Mapper已初始化

 印度阿三17 2019-07-10

我有一個(gè)3層架構(gòu)Web Api解決方案,里面有3個(gè)項(xiàng)目:數(shù)據(jù),業(yè)務(wù)和表示層.我需要在兩個(gè)業(yè)務(wù)和表示層中初始化兩個(gè)不同的映射器.

我已經(jīng)創(chuàng)建了一個(gè)靜態(tài)類(lèi)和方法來(lái)初始化業(yè)務(wù)邏輯中的一個(gè)映射器:

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>());
        }
    }
}

并稱(chēng)之為:

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>());
        }
    }
}

并調(diào)用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();
        }

我已經(jīng)初始化了錯(cuò)誤Mapper.您必須為每個(gè)應(yīng)用程序域/進(jìn)程調(diào)用一次Initialize.

如何解決這個(gè)問(wèn)題呢?

解決方法:

您可以使用反射和自動(dòng)播放器配置文件的一種方法.這在我用過(guò)的項(xiàng)目中非常有效.

在每個(gè)項(xiàng)目/圖層中創(chuàng)建一個(gè)automapper配置文件類(lèi).每個(gè)配置文件類(lèi)應(yīng)僅包含它自己需要的映射.以下是其中一個(gè)的示例:

  //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(); }
    }
  }

然后真正靠近應(yīng)用程序入口點(diǎn)的地方(我從Global.asax.cs中的ApplicationStart方法調(diào)用以下方法),初始化所有的配置文件,如下所示:

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);
        }
      });
    }

這將允許您按照它們使用的圖層邏輯分隔您的地圖,并確保您只初始化它們一次.

來(lái)源:https://www./content-1-315301.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多