我正在学习C#.NET Core,并试图在不使用AutoMapper的情况下创建DTO映射,因为我仅在一个小项目上工作,并且想在使用额外的程序包之前了解基本知识,所以我无法在stackoverflow上轻易找到答案。 com或我可能使用了错误的关键字搜索。
顺便说一句,下面是我的代码,我已将其成功映射到GetEmployee方法下的EmployeeForShortDto。不幸的是,我不仅仅因为返回数据是一个集合,而不是单个记录,就无法在GetAllEmployee下映射它。请指教。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreWebApplication1.Dto;
using NetCoreWebApplication1.Repository;
using NetCoreWebApplication1.Other;
namespace NetCoreWebApplication1.Controller
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private readonly IMasterRepository _repo;
public EmployeeController(IMasterRepository repo)
{
_repo = repo;
}
[HttpGet("{id}")]
public async Task<IActionResult> GetEmployee(int id)
{
var data = await _repo.GetEmployee(id);
if (data == null) return NotFound();
var dataDto = new EmployeeForShortDto()
{
Id = data.Id,
EmpCode = data.EmpCode,
Fname = data.Fname,
Lname = data.Lname,
Age = NetCoreWebApplication1.Other.Extension.CalcAge(data.DateBirth)
};
return Ok(dataDto);
}
[HttpGet]
public async Task<IActionResult> GetAllEmployee()
{
var data = await _repo.GetAllEmployee();
return Ok(data);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NetCoreWebApplication1.Models;
namespace NetCoreWebApplication1.Repository
{
public class MasterRepository : IMasterRepository
{
private readonly PrDbContext _context;
public MasterRepository(PrDbContext context)
{
_context = context;
}
// Employee
public async Task<List<Employee>> GetAllEmployee()
{
var data = await _context.Employee.ToListAsync();
return data;
}
public async Task<Employee> GetEmployee(int id)
{
var data = await _context.Employee.FirstOrDefaultAsync(x => x.Id == id);
return data;
}
// Generic methods
public void Add<T>(T entity) where T : class
{
_context.Add(entity);
}
public void Delete<T>(T entity) where T : class
{
_context.Remove(entity);
}
public async Task<bool> SaveAll()
{
return await _context.SaveChangesAsync() > 0;
}
}
}
答案 0 :(得分:0)
好的,您的问题的直接答案是“执行返回值”;
List<EmployeeForShortDto> result = new List<EmployeeForShortDto>();
foreach(Employee dbEmployee in data )
{
result.add(new EmployeeForShortDto()
{
Id = dbEmployee .Id,
EmpCode = dbEmployee .EmpCode,
Fname = dbEmployee .Fname,
Lname = dbEmployee .Lname,
Age = NetCoreWebApplication1.Other.Extension.CalcAge(dbEmployee .DateBirth)
});
}
这是您商品的特定类型。为什么不创建一个使用反射通过附加属性或直接通过属性名称映射对象的通用方法? 完成后,只要遵守属性名称的内部规则或通过属性设置映射,就可以将任何对象转移到DTO。
答案 1 :(得分:0)
针对您的问题,以新方法提取实现。
EmployeeForShortDto ConvertToDto(Employee data)
{
var dataDto = new EmployeeForShortDto()
{
Id = data.Id,
EmpCode = data.EmpCode,
Fname = data.Fname,
Lname = data.Lname,
Age = NetCoreWebApplication1.Other.Extension.CalcAge(data.DateBirth)
};
}
然后最终将其循环调用
foreach(Employee e in EmployeeList)
{
dtoList.Add(ConvertToDto(e));
}
对于一般实现,请通过反射生成Model和Dto的属性列表。然后匹配它们的类型。
class AdapterHelper<T1, T2>
{
public T1 Adapt(T2 source)
{
T1 targetItem = Activator.CreateInstance<T1>();
var props = typeof(T1).GetProperties();
var targetProps = typeof(T2).GetProperties();
foreach (var prop in props)
{
foreach (var targetProp in targetProps)
{
if (prop.Name == targetProp.Name)
{
targetProp.SetValue(targetItem, prop.GetValue(source));
//assign
}
}
}
return targetItem;
}
}
这是我原始答案的link。
答案 2 :(得分:0)
您可以使用扩展方法将实体类型映射为DTO类型。
public static EmployeeForShortDto ToDto(this Employee employee)
{
if (employee != null)
{
return new EmployeeForShortDto
{
Id = employee.Id,
EmpCode = employee.EmpCode,
Fname = employee.Fname,
Lname = employee.Lname,
Age = NetCoreWebApplication1.Other.Extension.CalcAge(employee.DateBirth)
};
}
return null;
}
然后在需要的地方使用。
[HttpGet("{id}")]
public async Task<IActionResult> GetEmployee(int id)
{
var data = await _repo.GetEmployee(id);
if (data == null)
{
return NotFound();
}
return Ok(data.ToDto());
}
[HttpGet]
public async Task<IActionResult> GetAllEmployee()
{
var data = await _repo.GetAllEmployee();
return Ok(data.Select(x => x.ToDto()));
}
答案 3 :(得分:0)
感谢您的所有回复,所有这些对我都很有用。最后,我得到@Brad的解决方案。我还学习了如何在将记录添加到数据库之前进行从DTO到类的反向映射。
我将我的代码放在下面,以防有人想要。任何意见/建议都非常欢迎。谢谢。
Extension.cs
using NetCoreWebApplication1.Dto;
using NetCoreWebApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NetCoreWebApplication1.Other
{
public static class Extension
{
public static EmployeeForShortDto MapToEmployeeForShortDto(this Employee emp)
{
if (emp != null)
{
return new EmployeeForShortDto
{
Id = emp.Id,
EmpCode = emp.EmpCode,
Fname = emp.Fname,
Lname = emp.Lname,
Age = emp.DateBirth.CalcAge()
};
}
return null;
}
public static EmployeeForListDto MapToEmployeeForListDto(this Employee emp)
{
if (emp != null)
{
return new EmployeeForListDto
{
Id = emp.Id,
EmpCode = emp.EmpCode,
Fname = emp.Fname,
Lname = emp.Lname,
Age = emp.DateBirth.CalcAge(),
EntityCode = emp.EntityCode,
IsActive = emp.IsActive
};
}
return null;
}
public static Employee MapFromEmployeeForAddDto(this EmployeeForAddDto emp)
{
if (emp != null)
{
return new Employee
{
EmpCode = emp.EmpCode,
Fname = emp.Fname,
Lname = emp.Lname,
IdCard = emp.IdCard,
IsActive = 1
};
}
return null;
}
public static int CalcAge(this DateTime? dateBirth)
{
if (dateBirth.HasValue)
{
var age = DateTime.Today.Year - dateBirth.Value.Year;
if (dateBirth.Value.AddYears(age) > DateTime.Today) age--;
return age;
}
else
{
return 0;
}
}
}
}
MasterRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NetCoreWebApplication1.Dto;
using NetCoreWebApplication1.Models;
namespace NetCoreWebApplication1.Repository
{
public class MasterRepository : IMasterRepository
{
private readonly PrDbContext _context;
public MasterRepository(PrDbContext context)
{
_context = context;
}
// Employee
public async Task<List<Employee>> GetAllEmployee()
{
var data = await _context.Employee.ToListAsync();
return data;
}
public async Task<Employee> GetEmployee(int id)
{
var data = await _context.Employee.FirstOrDefaultAsync(x => x.Id == id);
return data;
}
public async Task<Employee> AddEmployee(Employee data)
{
await _context.Employee.AddAsync(data);
await _context.SaveChangesAsync();
return data;
}
public async Task<bool> EmployeeExists(string entityCode, string empCode)
{
if (await _context.Employee.AnyAsync(x =>
x.EntityCode == entityCode &&
x.EmpCode == empCode))
return true;
return false;
}
// Generic methods
public void Add<T>(T entity) where T : class
{
_context.Add(entity);
}
public void Delete<T>(T entity) where T : class
{
_context.Remove(entity);
}
public async Task<bool> SaveAll()
{
return await _context.SaveChangesAsync() > 0;
}
}
}
EmployeeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreWebApplication1.Dto;
using NetCoreWebApplication1.Repository;
using NetCoreWebApplication1.Other;
using NetCoreWebApplication1.Models;
namespace NetCoreWebApplication1.Controller
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private readonly IMasterRepository _repo;
public EmployeeController(IMasterRepository repo)
{
_repo = repo;
}
[HttpPost("add")]
public async Task<IActionResult> AddEmployee(EmployeeForAddDto emp)
{
if (await _repo.EmployeeExists(emp.EntityCode, emp.EmpCode))
ModelState.AddModelError("Employee", "Employee is duplicate (EntityCode + EmpCode)");
if (!ModelState.IsValid)
return BadRequest(ModelState);
Employee employeeToAdd = emp.MapFromEmployeeForAddDto();
await _repo.AddEmployee(employeeToAdd);
return StatusCode(201);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetEmployee(int id)
{
var data = await _repo.GetEmployee(id);
if (data == null) return NotFound();
return Ok(data.MapToEmployeeForShortDto());
}
[HttpGet]
public async Task<IActionResult> GetAllEmployee()
{
var data = await _repo.GetAllEmployee();
//var dataDto = data.Select(x => x.MapToEmployeeForShortDto());
var dataDto = data.Select(x => x.MapToEmployeeForListDto());
return Ok(dataDto);
}
}
}