In the following example shows a query to get an employ details. It has a int
parameter Id
. And there is a DTO class EmployeeDto
which also has a field Id
with type of string
.
I will need to create a value-object class EmployeeId
which contains some business logic for the employee Id. The Id
of the entity class Employee
will be changed to type of EmployeeId
instead of string
.
- Should the type of
Id
of the DTO/Vm classEmployeeDto
bestring
? And Automapper is used to map thestring
toEmployeeId
? So it will be easier for UI to renter the list. - Should the type of the property
Id
of the queryGetEmployeeDetailQuery
be primary type string/int? Where to check if the parameter is a valid employee ID (the value objectEmployeeId
has the validation logic)?
code:
public class GetEmployeeDetailQuery : IRequest<EmployeeDetailVm>
{
public int Id { get; set; } // Stay as primary type string/int?
// public EmployeeId Id { get; set; } // or EmployeeId?
public class GetEmployeeDetailQueryHandler : IRequestHandler<GetEmployeeDetailQuery, EmployeeDetailVm>
{
private readonly INorthwindDbContext _context;
private readonly IMapper _mapper;
public GetEmployeeDetailQueryHandler(INorthwindDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<EmployeeDetailVm> Handle(GetEmployeeDetailQuery request, CancellationToken cancellationToken)
{
var vm = await _context.Employees
.Where(e => e.EmployeeId == request.Id)
.ProjectTo<EmployeeDetailVm>(_mapper.ConfigurationProvider) // to be mapped to entity here
.SingleOrDefaultAsync(cancellationToken);
return vm;
}
}
}
namespace Northwind.Application.Employees.Queries.GetEmployeeDetail
{
public class EmployeeDetailVm : IMapFrom<Employee>
{
public int Id { get; set; } // int/string
// public EmployeeId Id { get; set; } // or EmployeeId?
public string Title { get; set; }