ASP.NET Core 项目实战(AutoMapper)

1.AutoMapper的使用

1、引入Nuget包 AutoMapper、AutoMapper.Extensions.Microsoft.DependencyInjection
2、新建Config文件夹,以及AutoMapperConfigs文件,引用Profile
3、在构造函数中管理映射关系:CreateMap();
4、在Program中注册
builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
5、使用
return _mapper.Map(user);

2 创建输入输出Dto

image.png

1、MenuAdd

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebModel.Dto.Menu
{
    public class MenuAdd
    {
        public string Name { get; set; }
        public string Index { get; set; }
        public string FilePath { get; set; }
        public long ParentId { get; set; }
        public int Order { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }


    }
}

2、MenuEdit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebModel.Dto.Menu
{
    public class MenuEdit
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Index { get; set; }
        public string FilePath { get; set; }
        public long ParentId { get; set; }
        public int Order { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }
    }
}

3、MenuReq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebModel.Dto.Menu
{
    public class MenuReq
    {
        public string Name { get; set; }
        public string Index { get; set; }
        public string FilePath { get; set; }
        public long ParentId { get; set; }
        public string Description { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
    }
}

4、MenuRes

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebModel.Dto.Menu
{
    public class MenuRes
    {
        [SugarColumn(IsTreeKey = true)]
        public long Id { get; set; }
        public string Name { get; set; }
        public string Index { get; set; }
        public string FilePath { get; set; }
        public long ParentId { get; set; }
        public string ParentName { get; set; }
        public int Order { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }
        public DateTime CreateDate { get; set; }
        /// 
        /// 子级
        /// 
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public List Children { get; set; }
    }
}

  1. RoleAdd
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.Role
{
   public class RoleAdd
   {
       public string Name { get; set; }
       public int Order { get; set; }
       public bool IsEnable { get; set; }
       public string Description { get; set; }
   }
}

6.RoleEdit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.Role
{
    public class RoleEdit
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Order { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }
    }
}

7.RoleReq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.Role
{
    public class RoleReq
    {
        public string Name { get; set; } 
        public bool IsEnable { get; set; }
        public string Description { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
    }
}

8.RoleRes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.Role
{
   public class RoleRes
   {
       public long Id { get; set; }
       /// 
       /// 名称
       ///  
       public string Name { get; set; }
       /// 
       /// 排序
       ///  
       public int Order { get; set; }
       /// 
       /// 是否启用(0=未启用,1=启用)
       ///  
       public bool IsEnable { get; set; } 
       /// 
       /// 描述
       ///  
       public string Description { get; set; }
       /// 
       /// 创建人Id
       ///  
       public string CreateUserId { get; set; }
       /// 
       /// 创建日期
       ///  
       public DateTime CreateDate { get; set; }
       /// 
       /// 修改人Id
       ///  
       public string ModifyUserId { get; set; }
       /// 
       /// 修改日期
       ///  
       public DateTime? ModifyDate { get; set; }
       /// 
       /// 是否删除
       ///  
       public int IsDeleted { get; set; }
   }
}

9.UserAdd

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.User
{
    public class UserAdd
    {
        public string Name { get; set; }
        public string NickName { get; set; }
        public string Password { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.User
{
    public class UserEdit
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string NickName { get; set; }
        public string Password { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }
    }
}

11.UserReq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.User
{
    public class UserReq
    {
        public string Name { get; set; }
        public string NickName { get; set; } 
        public int UserType { get; set; }
        public bool IsEnable { get; set; }
        public string Description { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
    }
}

  1. UserRes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Dto.User
{
    public class UserRes
    {
        /// 
        /// 主键
        /// 
        public long Id { get; set; }
        /// 
        /// 用户名
        ///  
        public string Name { get; set; }
        /// 
        /// 昵称
        ///  
        public string NickName { get; set; }
        /// 
        /// 密码
        /// 
        public string Password { get; set; }
        /// 
        /// 用户类型
        /// 
        public int UserType { get; set; }
        /// 
        /// 角色名
        /// 
        public string RoleName { get; set; }
        /// 
        /// 创建时间
        /// 
        public DateTime CreateDate { get; set; }
        /// 
        /// 是否启用
        /// 
        public bool IsEnable { get; set; }
        /// 
        /// 描述
        /// 
        public string Description { get; set; }
    }
}

在WebApi => Config => AutoMapperConfigs 配置关系映射
using AutoMapper;
using Model.Dto.Role;
using Model.Dto.User;
using WebModel.Dto.Menu;
using WebModel.Entitys;

namespace WebApi.Config
{
    /// 
    /// Dto 映射配置
    /// 
    public class AutoMapperConfigs:Profile
    {
        /// 
        /// 映射配置
        /// 
        public AutoMapperConfigs()
        {

            //角色
            CreateMap();
            CreateMap();
            CreateMap();
            // 用户
            CreateMap();
            CreateMap();
            CreateMap();
            // 菜单
            CreateMap();
            CreateMap();
            CreateMap();
        }
    }
}

最后在注入到配置文件中(HostBuilderExtend.cs)
using Autofac;
using Autofac.Extensions.DependencyInjection;
using SqlSugar;
using System.Runtime.CompilerServices;

namespace WebApi.Config
{
    /// 
    /// 注册类-扩展
    /// 
    public static class HostBuilderExtend
    {
        public static void Register(this WebApplicationBuilder app)
        {
            // 1. nuGet 安装 autofac
            app.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
            app.Host.ConfigureContainer(builder =>
            {
                #region 注册 sqlsugar
                builder.Register(context =>
                {
                    SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                    {
                        //数据库配置地址
                        ConnectionString = "Server=;Database=zxAdminDb;Uid=sa;Pwd=root",
                        DbType = DbType.SqlServer,
                        IsAutoCloseConnection = true
                    });
                    //支持SQL 语句的输出,方便排查问题
                    db.Aop.OnLogExecuted = (sql, par) =>
                    {
                        Console.WriteLine("\r\n");
                        Console.WriteLine($"{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")},Sql语句:{sql}");
                        Console.WriteLine("===================================================");
                    };
                    return db; 
                });
                #endregion

                // 注册接口和实现层
                builder.RegisterModule(new AutofacModuleRegister());
            });
            //Automapper映射
            app.Services.AddAutoMapper(typeof(AutoMapperConfigs));
        }

    }
}

你可能感兴趣的:(ASP.NET Core 项目实战(AutoMapper))