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

分享

.net5 使用AutoMapper

 實(shí)力決定地位 2021-04-01

步驟1:項(xiàng)目引用

AutoMapper

AutoMapper.Extensions.Microsoft.DependencyInjection

步驟2:建立文件 DevProCommonFile

IProfile:是一個(gè)空接口,主要用來動(dòng)態(tài)加載文件時(shí)查找相關(guān)文件,也就時(shí)說DevProCommonFile這種文件可以建立多個(gè),只要繼承
Profile, IProfile就會(huì)被框架加入
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:設(shè)置加載文件規(guī)則

建立Mappings 定一個(gè)擴(kuò)展方法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()//獲取默認(rèn)程序集
             .GetReferencedAssemblies()//獲取所有引用程序集             .Select(Assembly.Load)
             .SelectMany(y => y.DefinedTypes)
             .Where(type => typeof(IProfile).GetTypeInfo().IsAssignableFrom(type.AsType()));
            service.AddAutoMapper(allType.ToArray());

            

        }
    }
}

步驟4:Startup 的ConfigureServices進(jìn)行注冊

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

步驟5:應(yīng)用 在控制器注入 IMapper 

private IMapper _IMapper { get; set; }

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

這樣就完成了AutoMapper使用

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多