Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingDao.cs @ 9582

Last change on this file since 9582 was 9577, checked in by spimming, 12 years ago

#1888:

  • Added dal and service interfaces and implementations
  • Added dbcontext for billing
File size: 3.1 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Linq.Expressions;
6using HeuristicLab.Services.Optimization.Billing.Model;
7namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
8  public class BillingDao {
9
10    #region Context Creation and Execution
11
12    private BillingContext CreateContext() {
13      BillingContext context = new BillingContext("name=BillingContext");
14      return context;
15    }
16
17    private delegate T ExecuteWithContextDelegate<T>(BillingContext context);
18    private delegate void ExecuteWithContextDelegate(BillingContext context);
19
20    private void ExecuteWithContext(ExecuteWithContextDelegate call) {
21      BillingContext context = null;
22      try {
23        using (context = CreateContext()) {
24          call(context);
25        }
26      }
27      finally {
28        if (context.Database.Connection.State == System.Data.ConnectionState.Open) {
29          context.Database.Connection.Close();
30        }
31      }
32    }
33
34    private T ExecuteWithContext<T>(ExecuteWithContextDelegate<T> call) {
35      BillingContext context = null;
36      try {
37        using (context = CreateContext()) {
38          T result = call(context);
39          return result;
40        }
41      }
42      finally {
43        if (context.Database.Connection.State == System.Data.ConnectionState.Open) {
44          context.Database.Connection.Close();
45        }
46      }
47    }
48
49    #endregion
50
51    #region Product Methods
52
53    public Product FindProductById(long id) {
54      return ExecuteWithContext<Product>((context) => {
55        return context.Products.FirstOrDefault(prod => prod.ProductId == id);
56      });
57    }
58
59    public IEnumerable<Product> FindProdcutBy(Expression<Func<Product, bool>> predicate) {
60      using (var context = CreateContext()) {
61        var products = context.Products.Where(predicate).ToList();
62        return products;
63      }
64    }
65
66    public IEnumerable<Product> FindAllProducts() {
67      using (var context = CreateContext()) {
68        var products = context.Products.ToList();
69        return products;
70      }
71    }
72
73    public void AddProduct(Product entity) {
74      ExecuteWithContext((context) => {
75        context.Products.Add(entity);
76        context.SaveChanges();
77      });
78    }
79
80    public void UpdateProduct(Product entity) {
81      ExecuteWithContext((context) => {
82        context.Products.Attach(entity);
83        context.Entry(entity).State = System.Data.EntityState.Modified;
84        context.SaveChanges();
85      });
86    }
87
88    void DeleteProduct(Product entity) {
89      ExecuteWithContext((context) => {
90        context.Products.Remove(entity);
91      });
92    }
93
94    #endregion
95
96    #region Order Methods
97
98    public void AddOrder(Order order) {
99      ExecuteWithContext((context) => {
100        context.Orders.Add(order);
101        context.SaveChanges();
102      });
103    }
104
105    public IEnumerable<Order> FindOrderBy(Expression<Func<Order, bool>> predicate) {
106      using (var context = CreateContext()) {
107        var orders = context.Orders.Where(predicate).ToList();
108        return orders;
109      }
110    }
111
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.