1.AutoMapper的使用
1、引入Nuget包 AutoMapper、AutoMapper.Extensions.Microsoft.DependencyInjection
2、新建Config文件夹,以及AutoMapperConfigs文件,引用Profile
3、在构造函数中管理映射关系:CreateMap<Users, UserRes>();
4、在Program中注册
builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
5、使用
return _mapper.Map<UserRes>(user);
image.png2 创建输入输出Dto
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; }
/// <summary>
/// 子级
/// </summary>
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<MenuRes> Children { get; set; }
}
}
- 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; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 排序
/// </summary>
public int Order { get; set; }
/// <summary>
/// 是否启用(0=未启用,1=启用)
/// </summary>
public bool IsEnable { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 创建人Id
/// </summary>
public string CreateUserId { get; set; }
/// <summary>
/// 创建日期
/// </summary>
public DateTime CreateDate { get; set; }
/// <summary>
/// 修改人Id
/// </summary>
public string ModifyUserId { get; set; }
/// <summary>
/// 修改日期
/// </summary>
public DateTime? ModifyDate { get; set; }
/// <summary>
/// 是否删除
/// </summary>
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; }
}
}
- UserRes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.Dto.User
{
public class UserRes
{
/// <summary>
/// 主键
/// </summary>
public long Id { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 昵称
/// </summary>
public string NickName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 用户类型
/// </summary>
public int UserType { get; set; }
/// <summary>
/// 角色名
/// </summary>
public string RoleName { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateDate { get; set; }
/// <summary>
/// 是否启用
/// </summary>
public bool IsEnable { get; set; }
/// <summary>
/// 描述
/// </summary>
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
{
/// <summary>
/// Dto 映射配置
/// </summary>
public class AutoMapperConfigs:Profile
{
/// <summary>
/// 映射配置
/// </summary>
public AutoMapperConfigs()
{
//角色
CreateMap<Role, RoleRes>();
CreateMap<RoleAdd, Role>();
CreateMap<RoleEdit, Role>();
// 用户
CreateMap<Users, UserRes>();
CreateMap<UserAdd, Users>();
CreateMap<UserEdit, UserRes>();
// 菜单
CreateMap<Menu, MenuRes>();
CreateMap<MenuAdd, Menu>();
CreateMap<MenuEdit, Menu>();
}
}
}
最后在注入到配置文件中(HostBuilderExtend.cs)
using Autofac;
using Autofac.Extensions.DependencyInjection;
using SqlSugar;
using System.Runtime.CompilerServices;
namespace WebApi.Config
{
/// <summary>
/// 注册类-扩展
/// </summary>
public static class HostBuilderExtend
{
public static void Register(this WebApplicationBuilder app)
{
// 1. nuGet 安装 autofac
app.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
app.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
#region 注册 sqlsugar
builder.Register<ISqlSugarClient>(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));
}
}
}
网友评论