【发布时间】:2017-04-20 05:30:58
【问题描述】:
我正在使用存储库模式开发一个 .net 多层应用程序。我的实现存储库模式的数据访问层具有将数据返回到服务层层的方法,服务层层又将数据返回到 Web api 层。目前我已经编写了返回客户信息或订单信息的方法。我还需要编写将返回 CustomerOrder 信息的方法。我认为最好的地方是把它写在服务层,但不知道如何去做。我在服务层中创建了一个 DTO 对象,其中包含来自 Customer 和 Order 类的字段。是服务层最好写这个逻辑的地方。我想我必须在服务层中填充 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>();
}
}
【问题讨论】:
-
理想情况下,您的服务层将仅返回 DTO,并且您将使用 Automapper 将数据库实体映射到 DTO。现在您可以独立于数据库层演化服务接口,并且可以将数据库实体组合成 DTO 从服务层返回。
-
除了复杂性之外,问题是,通过额外的存储库层您可以获得什么...
-
伙计,你真的需要 MediatR。所有这些图层对象对这一切都没有任何帮助。折叠这些层! vimeo.com/131633177
-
你还在为这个问题寻找解决方案吗?
标签: c# asp.net-mvc-5 automapper repository-pattern