我正在使用存储库模式开发.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; }
}
DataAccess Layer
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>();
}
}
答案 0 :(得分:1)
您是否考虑过实施Composite Pattern?
您的CustomerOrderDto
将包含CustomerDto
和OrderDto
类。像这样:
public class CustomerOrderDto
{
public CustomerDto Customer {get; set;}
public OrderDto Order {get; set;}
}
实施此模式将带来以下好处:
CustomerDto
或OrderDto
的任何未来更改都将自动成为CustomerOrderDto
的一部分。dto
类的代码可以重复用于映射单个dto
实体以及`CustomerOrderDto'。将实体类映射到DTO类的代码应该在您的服务层中。
将Customer实体映射到CustomerDto实体:
internal static CustomerDto Map(Customer entity)
{
if (entity == null) throw new ArgumentNullException("entity");
CustomerDto dto = new CustomerDto();
try
{
dto.Id = entity.Id;
dto.FirstName = entity.FirstName;
dto.LastName = entity.LastName;
dto.Gender = entity.Gender;
dto.Email = entity.Email;
}
catch (Exception e)
{
string errMsg = String.Format("Map(entity). Error mapping a Customer entity to DTO. Id: {0}.", entity.Id);
//LOG ERROR
}
return dto;
}
您是否已完成存储库层以检索Composite CustomerOrder实体?我看到您已经完成了GetAllCustomers()
和GetAllOrders()
的完整存储库代码。
答案 1 :(得分:0)
试试这个:
public interface IOrderService
{
IEnumerable<CustomerOrderDto> GetOrders();
}
public IEnumerable<CustomerOrderDto> GetOrders()
{
List<CustomerOrderDto> customerOrderList = new List<CustomerOrderDto>();
foreach (var item in customerOrderList)
{
CustomerOrderDto customerOrder = new CustomerOrderDto();
//fields mapping
customerOrderList.Add(customerOrder);
}
return customerOrderList;
}
映射的最佳方法是linq或this.You不应该使用Automapper,因为它太慢了。