示例#1
0
 public int Count()
 {
     using (var db = new CommandModelDatabase())
     {
         return (from s in db.Subscribers select s).Count();
     }
 }
示例#2
0
 public bool Add(Subscriber aggregate)
 {
     using (var db = new CommandModelDatabase())
     {
         db.Subscribers.Add(aggregate);
         return db.SaveChanges() > 0;
     }
 }
示例#3
0
 public IList<Product> FindAll()
 {
     using (var db = new CommandModelDatabase())
     {
         var products = (from p in db.Products select p).ToList();
         return products;
     }
 }
示例#4
0
 public IList<Order> FindAll()
 {
     using (var db = new CommandModelDatabase())
     {
         var orders = (from o in db.Orders select o).ToList();
         return orders;
     }
 }
示例#5
0
 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;
 }
示例#6
0
 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;
     }
 }
示例#7
0
 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;
     }
 }
示例#8
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; 
            }
        }
示例#9
0
 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();
         }
     }
 }
示例#10
0
 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;
         }
     }
 }
示例#11
0
        //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();
                }
            }
        }