我正在使用存储库模式开发 .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; }

}

服务层中的映射(Domain 对象到 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>();

    }
}

最佳答案

您是否考虑过实现 Composite Pattern

您的 CustomerOrderDto 将包含一个 CustomerDto 和一个 OrderDto 类。像这样:

public class CustomerOrderDto
{
    public CustomerDto Customer {get; set;}
    public OrderDto Order {get; set;}
}

实现这种模式将带来以下优势:
  • future 对 CustomerDtoOrderDto 的任何更改都将自动成为 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() 的完整存储库代码。

    关于c# - 从服务层返回包含多个实体的 DTO 对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40980755/

    10-12 21:25