日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

.net5 使用AutoMapper

 實力決定地位 2021-04-01

步驟1:項目引用

AutoMapper

AutoMapper.Extensions.Microsoft.DependencyInjection

步驟2:建立文件 DevProCommonFile

IProfile:是一個空接口,主要用來動態(tài)加載文件時查找相關文件,也就時說DevProCommonFile這種文件可以建立多個,只要繼承
Profile, IProfile就會被框架加入
using AutoMapper;using Dev.WooNet.Model.DevDTO;using Dev.WooNet.Model.Models;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Dev.WooNet.AutoMapper.ProFiles
{    /// <summary>
    /// 公共映射文件    /// </summary>
    public class DevProCommonFile : Profile, IProfile
    {        public DevProCommonFile()
        {
            CreateMap<DepartData, DevDepartment>();          //.ForMember(a => a.ContId, opt => opt.Ignore())          //.ForMember(a => a.CreateUserId, opt => opt.Ignore())          //.ForMember(a => a.CreateDateTime, opt => opt.Ignore())          //.ForMember(a => a.ModifyUserId, opt => opt.Ignore())          //.ForMember(a => a.ModifyDateTime, opt => opt.Ignore());        }
       
    }
}

步驟3:設置加載文件規(guī)則

建立Mappings 定一個擴展方法AddDevMapperFiles

using AutoMapper;using Microsoft.Extensions.DependencyInjection;using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace Dev.WooNet.AutoMapper.Extend
{   public static class Mappings
    {      

        /// <summary>
        ///映射文件        /// </summary>
        /// <param name="service"></param>
        public static void AddDevMapperFiles(this IServiceCollection service)
        {            var allType =
          Assembly
             .GetEntryAssembly()//獲取默認程序集
             .GetReferencedAssemblies()//獲取所有引用程序集             .Select(Assembly.Load)
             .SelectMany(y => y.DefinedTypes)
             .Where(type => typeof(IProfile).GetTypeInfo().IsAssignableFrom(type.AsType()));
            service.AddAutoMapper(allType.ToArray());

            

        }
    }
}

步驟4:Startup 的ConfigureServices進行注冊

services.AddDevMapperFiles();(上面擴展方法)

步驟5:應用 在控制器注入 IMapper 

private IMapper _IMapper { get; set; }

然后 var deptinfo = _IMapper.Map<DevDepartment>(departData);

這樣就完成了AutoMapper使用

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約