.Net Core 3.x Api开发笔记 -- 使用AutoMapper

1,安装AutoMapper包2,准备实体类和映射类 1 public class Users2 {3 public int Userid { get; set; }4 public string Username { get; set; }5 public string Password { get; s...

 1,安装AutoMapper包

2,准备实体类和映射类

 1     public class Users
 2     {
 3         public int Userid { get; set; }
 4         public string Username { get; set; }
 5         public string Password { get; set; }
 6         public string TrueName { get; set; }
 7         public int Sex { get; set; }
 8         public string Email { get; set; }
 9         public DateTime Birthday { get; set; }
10         public decimal bankprice { get; set; }
11 
12         .......
13     }
14 
15 
16     public class UsersDto
17     {
18         public int Userid { get; set; }
19         public string UserName { get; set; }
20         public string Sex { get; set; }
21         public string Birthday { get; set; }
22         public string Status { get; set; }
23         public string Regtime { get; set; }
24         public string Province { get; set; }
25         public string City { get; set; }
26         public string Area { get; set; }
27     }

3,配置映射类,分别创建类:CustomProfile、AutoMapperConfig

 1     /// <summary>
 2     /// AutoMapper映射类 实体类--->Dto扩展类
 3     /// </summary>
 4     public class CustomProfile : Profile
 5     {
 6         public CustomProfile()
 7         {
 8             CreateMap<Users, UsersDto>()
 9                 .ForMember(d => d.UserName, o => o.MapFrom(s => s.Username))
10                 //性别映射   0女  1男  2保密
11                 .ForMember(d => d.Sex, o => o.MapFrom(s => s.Sex == 0 ? "女" : (s.Sex == 1 ? "男" : "保密")))
12                 //状态映射   0停用  1启用
13                 .ForMember(d => d.Status, o => o.MapFrom(s => s.status == 0 ? "停用" : "启用"))
14                 //生日  时间格式映射  yyyy-MM-dd
15                 .ForMember(d => d.Birthday, o => o.MapFrom(s => s.Birthday.ToString("yyyy-MM-dd")))
16                 //注册时间  时间格式映射  yyyy-MM-dd HH:mm:ss
17                 .ForMember(d => d.Regtime, o => o.MapFrom(s => s.regtime.ToString("yyyy-MM-dd HH:mm:ss")))
18                 //省份   100
19                 .ForMember(d => d.Province, o => o.MapFrom(s => s.ProvinceId == 100 ? "河北省" : "北京市"))
20                 //城市   1001
21                 .ForMember(d => d.City, o => o.MapFrom(s => s.CityId == 1001 ? "石家庄市" : "北京市"))
22                 //地区   10011
23                 .ForMember(d => d.Area, o => o.MapFrom(s => s.AreaId == 10011 ? "正定区" : "北京市"));
24         }
25     }
26 
27 
28     /// <summary>
29     /// 配置AutoMapper映射类
30     /// </summary>
31     public class AutoMapperConfig
32     {
33         public static MapperConfiguration RegisterMappings()
34         {
35             return new MapperConfiguration(option =>
36             {
37                 option.AddProfile(new CustomProfile());
38             });
39         }
40     }

4,注册AutoMapper,注意下边红色部分

 1         public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddControllersWithViews();
 4 
 5             //注册AddAutoMapper
 6             services.AddAutoMapper(typeof(AutoMapperConfig));
 7             AutoMapperConfig.RegisterMappings();
 8 
 9             //注册接口
10             services.AddScoped<IUsersServices, UsersServices>();
11         }

5,在业务类中的构造函数中调用,使用 IMapper 进行调用。。。

 1     public class UsersServices : IUsersServices
 2     {
 3         private readonly IMapper mapper;
 4         public UsersServices(IMapper _mapper)
 5         {
 6             mapper = _mapper;
 7         }
 8         public List<UsersDto> GetUserList()
 9         {
10             return mapper.Map<List<UsersDto>>(this.GetUserDataList());  //调用数据库访问层获取数据
11         }
。。。。。。。。。

6,在控制器中使用

 1     public class UsersController : Controller
 2     {
 3         private readonly IUsersServices usersServices;
 4 
 5         public UsersController(IUsersServices _usersServices)
 6         {
 7             usersServices = _usersServices;
 8         }
 9 
10         public IActionResult Index()
11         {
12             var getmodel = usersServices.GetUserList();  //正常调用
13             return View(getmodel);
14         }
15     }

7,前端简单的用Html展示

 1 <div class="eblist-div" id="listDiv">
 2         <table id="list-table" class="datalist table table-bordered">
 3             <thead class="bg-info">
 4                 <tr>
 5                     <th>序号</th>
 6                     <th>用户名称</th>
 7                     <th>性别</th>
 8                     <th>生日</th>
 9                     <th>状态</th>
10                     <th>注册时间</th>
11                     <th>所在省</th>
12                     <th>所在市</th>
13                     <th>所在地区</th>
14                 </tr>
15             </thead>
16             <tbody>
17                 @foreach (var item in Model)
18                 {
19                     <tr>
20                         <td style="text-align: center;">@item.Userid</td>
21                         <td style="text-align: center;">@item.UserName</td>
22                         <td style="text-align: center;">@item.Sex</td>
23                         <td style="text-align: center;">@item.Birthday</td>
24                         <td style="text-align: center;">@item.Status</td>
25                         <td style="text-align: center;">@item.Regtime</td>
26                         <td style="text-align: center;">@item.Province</td>
27                         <td style="text-align: center;">@item.City</td>
28                         <td style="text-align: center;">@item.Area</td>
29                     </tr>
30                 }
31             </tbody>
32         </table>
33     </div>

8,最后测试效果 

 

本文标题为:.Net Core 3.x Api开发笔记 -- 使用AutoMapper

基础教程推荐