我正在开发一个使用仓储模式的 .net 多层应用程序。我的数据访问层实现了仓储模式,它有一些方法将数据返回给服务层,而服务层再将数据返回给 Web API 层。目前我编写了一些方法,可以返回客户信息或订单信息。我还需要编写能够返回客户订单信息的方法。我认为最好的地方是在服务层中编写该方法,但不确定应该如何操作。我在服务层中创建了一个 DTO 对象,它包含了客户和订单类的字段。服务层是否是编写这种逻辑的最佳位置?我认为我需要在服务层中填充 DTO 对象,并将该对象返回给呈现层。有人可以指点我吗?
实体层
在服务层中进行映射(将领域对象映射到Dto)
public class Customers
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Gender Gender { get; set; }
public string Email { get; set; }
public Address Address { get; set; }
public int AddressId { get; set; }
public ICollection<Orders> Orders { get; set; }
}
public class Orders
{
public int Id { get; set; }
public DateTime? OrderDate { get; set; }
public int? OrderNumber { get; set; }
public Customers Customers { get; set; }
public int CustomerId { get; set; }
public ICollection<ProductOrder> ProductOrders { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public State State { get; set; }
public int StateId { get; set; }
public ICollection<Customers> Customers { get; set; }
public string ZipCode { get; set; }
}
数据访问层
public class CustomerRepository : RepositoryBase<Customers>, ICustomerRepository
{
public CustomerRepository(IDbFactory dbFactory)
: base(dbFactory) { }
public IEnumerable<Customers> GetAllCustomers()
{
return (from customer in this.DbContext.Customers
select customer).ToList();
}
}
public interface ICustomerRepository : IRepository<Customers>
{
IEnumerable<Customers> GetAllCustomers();
}
public class OrderRepository : RepositoryBase<Orders>, IOrderRepository
{
public OrderRepository(IDbFactory dbFactory)
: base(dbFactory) {}
public IEnumerable<Orders> GetAllOrders()
{
return (from order in this.DbContext.Orders
select order).ToList();
}
}
public interface IOrderRepository : IRepository<Orders>
{
IEnumerable<Orders> GetAllOrders();
}
服务层
public interface ICustomerService
{
IEnumerable<Customers> GetCustomers();
}
public class CustomerService : ICustomerService
{
private readonly ICustomerRepository _customerRepository;
private readonly IUnitOfWork _unitOfWork;
public CustomerService(ICustomerRepository customerRepository, IUnitOfWork unitOfWork)
{
this._customerRepository = customerRepository;
this._unitOfWork = unitOfWork;
}
public IEnumerable<Customers> GetCustomers()
{
return _customerRepository.GetAll();
}
public interface IOrderService
{
IEnumerable<Orders> GetOrders();
}
public class OrderService : IOrderService
{
private readonly IOrderRepository _orderRepository;
private readonly IUnitOfWork _unitOfWork;
public OrderService(IOrderRepository orderRepository, IUnitOfWork unitOfWork)
{
this._orderRepository = orderRepository;
this._unitOfWork = unitOfWork;
}
}
public IEnumerable<Orders> GetOrders()
{
return _orderRepository.GetAll();
}
}
服务层中的Dto对象
public class CustomerDto
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
}
public class OrderDto
{
public int Id { get; set; }
public DateTime? OrderDate { get; set; }
public int? OrderNumber { get; set; }
}
public class CustomerOrderDto
{
public CustomerDto Customer { get; set; }
public OrderDto Order { get; set; }
}
在服务层中进行映射(将领域对象映射到Dto)
public class DomainToDtoMapping : Profile
{
public override string ProfileName
{
get { return "DomainToDtoMapping"; }
}
[Obsolete]
protected override void Configure()
{
CreateMap<Customers, CustomerDto>();
CreateMap<Address, AddressDto>();
CreateMap<Orders, OrderDto>();
CreateMap<OrdersDetails, OrderDetailsDto>();
CreateMap<State, StateDto>();
CreateMap<CustomerDto, CustomerOrderDto>();
CreateMap<AddressDto, CustomerOrderDto>();
CreateMap<OrderDto, CustomerOrderDto>();
CreateMap<OrderDetailsDto, CustomerOrderDto>();
CreateMap<State, CustomerOrderDto>();
}
}