//Approach 1 - Domain Model DTO Projection public List <CustomerPurchaseHistoryDto> GetAllCustomerPurchaseHistoryV1() { IEnumerable <Guid> customersThatHavePurhcasedSomething = this.purchaseRepository.Find(new PurchasedNProductsSpec(1)) .Select(purchase => purchase.CustomerId) .Distinct(); IEnumerable <Customer> customers = this.customerRepository.Find(new CustomerBulkIdFindSpec(customersThatHavePurhcasedSomething)); List <CustomerPurchaseHistoryDto> customersPurchaseHistory = new List <CustomerPurchaseHistoryDto>(); foreach (Customer customer in customers) { IEnumerable <Purchase> customerPurchases = this.purchaseRepository.Find(new CustomerPurchasesSpec(customer.Id)); CustomerPurchaseHistoryDto customerPurchaseHistory = new CustomerPurchaseHistoryDto(); customerPurchaseHistory.CustomerId = customer.Id; customerPurchaseHistory.FirstName = customer.FirstName; customerPurchaseHistory.LastName = customer.LastName; customerPurchaseHistory.Email = customer.Email; customerPurchaseHistory.TotalPurchases = customerPurchases.Count(); customerPurchaseHistory.TotalProductsPurchased = customerPurchases.Sum(purchase => purchase.Products.Sum(product => product.Quantity)); customerPurchaseHistory.TotalCost = customerPurchases.Sum(purchase => purchase.TotalCost); customersPurchaseHistory.Add(customerPurchaseHistory); } return(customersPurchaseHistory); }
//Approach 1 - Domain Model DTO Projection public List <CustomerPurchaseHistoryDto> GetAllCustomerPurchaseHistoryV1() { IEnumerable <Customer> customers = this.customerRepository.Find(new CustomerPurchasedNProductsSpec(1)); List <CustomerPurchaseHistoryDto> customersPurchaseHistory = new List <CustomerPurchaseHistoryDto>(); foreach (Customer customer in customers) { CustomerPurchaseHistoryDto customerPurchaseHistory = new CustomerPurchaseHistoryDto(); customerPurchaseHistory.CustomerId = customer.Id; customerPurchaseHistory.FirstName = customer.FirstName; customerPurchaseHistory.LastName = customer.LastName; customerPurchaseHistory.Email = customer.Email; customerPurchaseHistory.TotalPurchases = customer.Purchases.Count; customerPurchaseHistory.TotalProductsPurchased = customer.Purchases.Sum(purchase => purchase.Products.Sum(product => product.Quantity)); customerPurchaseHistory.TotalCost = customer.Purchases.Sum(purchase => purchase.TotalCost); customersPurchaseHistory.Add(customerPurchaseHistory); } return(customersPurchaseHistory); }