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,最后測試效果