public int Count() { using (var db = new CommandModelDatabase()) { return (from s in db.Subscribers select s).Count(); } }
public bool Add(Subscriber aggregate) { using (var db = new CommandModelDatabase()) { db.Subscribers.Add(aggregate); return db.SaveChanges() > 0; } }
public IList<Product> FindAll() { using (var db = new CommandModelDatabase()) { var products = (from p in db.Products select p).ToList(); return products; } }
public IList<Order> FindAll() { using (var db = new CommandModelDatabase()) { var orders = (from o in db.Orders select o).ToList(); return orders; } }
public AdHocCatalog GetCustomerAdHocCatalog(string customerId) { var catalog = new AdHocCatalog(); using (var db = new CommandModelDatabase()) { catalog.Products = (from p in db.Products select p).ToList(); catalog.Customer = (from c in db.Customers where c.CustomerId == customerId select c).Single(); } return catalog; }
public ICollection<Product> GetFeaturedProducts(int count = 3) { // Featured() is just a business-oriented way of writing a WHERE clause using (var db = new CommandModelDatabase()) { var products = (from p in db.Products where p.Featured select p).Take(count).ToList(); if (!products.Any()) products = (from p in db.Products orderby p.StockLevel descending select p).Take(1).ToList(); return products; } }
public int AddAndReturnKey(Order aggregate) { using (var db = new CommandModelDatabase()) { db.Entry(aggregate.Buyer).State = EntityState.Unchanged; db.Orders.Add(aggregate); if (db.SaveChanges() > 0) { return aggregate.OrderId; } return 0; } }
public IEnumerable<Product> FindProductBelowStockLevel(IEnumerable<Product> products, int threshold) { var productIds = (from p in products.ToArray() select p.Id).ToList(); using (var db = new CommandModelDatabase()) { var list = (from p in db.Products where p.StockLevel < threshold && productIds.Contains(p.Id) select p).ToList(); return list; } }
public Customer FindById(string id) { using (var db = new CommandModelDatabase()) { try { var customer = (from c in db.Customers where c.CustomerId == id select c).Single(); return customer; } catch (InvalidOperationException) { return new MissingCustomer(); } } }
public bool Save(Customer aggregate) { using (var db = new CommandModelDatabase()) { try { var customer = (from c in db.Customers where c.CustomerId == aggregate.CustomerId select c).Single(); customer.SetAddress(aggregate.Address); customer.SetAvatar(aggregate.Avatar); customer.SetPaymentDetails(aggregate.Payment); customer.SetPasswordHash(aggregate.PasswordHash); var changes = db.SaveChanges(); return changes >0; } catch (InvalidOperationException) { return false; } } }
//public Order FindById(int id) //{ // using (var db = new DomainModelFacade()) // { // try // { // // Need to load the entire graph of objects // var order = (from o in db.Orders // .Include("Items") // .Include("Items.Product") // where o.OrderId == id // select o).Single(); // return order; // } // catch (InvalidOperationException) // { // return new MissingOrder(); // } // } //} public Order FindLastByCustomer(string customerId) { using (var db = new CommandModelDatabase()) { try { // Need to load the entire graph of objects var order = (from o in db.Orders .Include("Buyer") .Include("Items") .Include("Items.Product") where o.Buyer.CustomerId == customerId orderby o.OrderId descending select o).First(); return order; } catch (InvalidOperationException) { return new MissingOrder(); } } }