Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9645 was 9645, checked in by spimming, 11 years ago

#1888:

  • Model classes implement new interface to track current state of entity
  • Mark EntityState property as 'not mapped'
  • Set hook to initialize entity as unchanged
  • Extension method for DbContext to apply changes only on modified entity
File size: 5.8 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Data;
5using System.Data.Entity;
6using System.Linq;
7using System.Linq.Expressions;
8using HeuristicLab.Services.Optimization.Billing.Model;
9using HeuristicLab.Services.Optimization.Billing.Utils;
10namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
11  public class BillingDao {
12
13    #region Context Creation and Execution
14
15    private BillingContext CreateContext() {
16      BillingContext context = new BillingContext("name=BillingContext");
17      return context;
18    }
19
20    #endregion
21
22    #region Product Methods
23
24    public Product FindProductById(long id) {
25      using (var context = CreateContext()) {
26        return context.Products.FirstOrDefault(prod => prod.ProductId == id);
27      }
28    }
29
30    public IEnumerable<Product> FindProdcutBy(Expression<Func<Product, bool>> predicate) {
31      using (var context = CreateContext()) {
32        var products = context.Products.Where(predicate).ToList();
33        return products;
34      }
35    }
36
37    public IEnumerable<Product> FindAllProducts() {
38      using (var context = CreateContext()) {
39        var products = context.Products.ToList();
40        return products;
41      }
42    }
43
44    public void InsertOrUpdateProduct(Product entity) {
45      using (var context = CreateContext()) {
46        throw new NotImplementedException();
47      }
48    }
49
50    void DeleteProduct(Product entity) {
51      using (var context = CreateContext()) {
52        throw new NotImplementedException();
53      }
54    }
55
56    #endregion
57
58    #region Order Methods
59
60    public void InsertOrUpdateOrder(Order entity) {
61      using (var context = CreateContext()) {
62        context.Orders.Add(entity);
63        context.ApplyStateChanges();
64        context.SaveChanges();
65      }
66    }
67
68    public IEnumerable<Order> FindOrderBy(Expression<Func<Order, bool>> predicate) {
69      using (var context = CreateContext()) {
70        var orders = context.Orders
71          .Include(o => o.OrderLines)
72          .Include(o => o.User)
73          .Include(o => o.User.ContactInformation)
74          .Include(o => o.Invoices)
75          .Include(o => o.OrderLines.Select(ol => ol.Product))
76          .Where(predicate).ToList();
77        return orders;
78      }
79    }
80
81    #endregion
82
83    #region OrderLine Methods
84
85    public OrderLine FindOrderLineById(long id) {
86      using (var context = CreateContext()) {
87        return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id);
88      }
89    }
90
91    public IEnumerable<OrderLine> FindOrderLineBy(Expression<Func<OrderLine, bool>> predicate) {
92      using (var context = CreateContext()) {
93        var orderLines = context.OrderLines.Where(predicate).ToList();
94        return orderLines;
95      }
96    }
97
98    public IEnumerable<OrderLine> FindAllOrderLines() {
99      using (var context = CreateContext()) {
100        var orderLines = context.OrderLines.ToList();
101        return orderLines;
102      }
103    }
104
105    public void InsertOrUpdateOrderLine(OrderLine entity) {
106      using (var context = CreateContext()) {
107        throw new NotImplementedException();
108      }
109    }
110
111    void DeleteOrderLine(OrderLine entity) {
112      using (var context = CreateContext()) {
113        throw new NotImplementedException();
114      }
115    }
116
117    #endregion
118
119    #region User Methods
120
121    public User FindUserById(long id) {
122      using (var context = CreateContext()) {
123        return context.Users.FirstOrDefault(user => user.UserId == id);
124      }
125    }
126
127    public IEnumerable<User> FindUserBy(Expression<Func<User, bool>> predicate) {
128      using (var context = CreateContext()) {
129        var users = context.Users.Where(predicate).ToList();
130        return users;
131      }
132    }
133
134    public IEnumerable<User> FindAllUsers() {
135      using (var context = CreateContext()) {
136        var users = context.Users.ToList();
137        return users;
138      }
139    }
140
141    public void InsertOrUpdateUser(User entity) {
142      using (var context = CreateContext()) {
143        throw new NotImplementedException();
144      }
145    }
146
147    void DeleteUser(User entity) {
148      using (var context = CreateContext()) {
149        throw new NotImplementedException();
150      }
151    }
152
153    #endregion
154
155    #region UsageRecords Methods
156
157    public UsageRecord FindUsageRecordById(long id) {
158      using (var context = CreateContext()) {
159        return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id);
160      }
161    }
162
163    public IEnumerable<UsageRecord> FindUsageRecordsBy(Expression<Func<UsageRecord, bool>> predicate) {
164      using (var context = CreateContext()) {
165        var records = context.UsageRecords.Where(predicate).ToList();
166        return records;
167      }
168    }
169
170    public IEnumerable<UsageRecord> FindAllUsageRecords() {
171      using (var context = CreateContext()) {
172        var records = context.UsageRecords.ToList();
173        return records;
174      }
175    }
176
177    public void InsertOrUpdateUsageRecord(UsageRecord entity) {
178      using (var context = CreateContext()) {
179        throw new NotImplementedException();
180      }
181    }
182
183    void DeleteUsageRecord(UsageRecord entity) {
184      using (var context = CreateContext()) {
185        throw new NotImplementedException();
186      }
187    }
188
189    #endregion
190
191    #region Invoice Methods
192
193    public void InsertOrUpdateInvoice(Invoice entity) {
194      using (var context = CreateContext()) {
195        context.Invoices.Add(entity);
196        context.ApplyStateChanges();
197        context.SaveChanges();
198      }
199    }
200
201    public IEnumerable<Invoice> FindInvoiceBy(Expression<Func<Invoice, bool>> predicate) {
202      using (var context = CreateContext()) {
203        var invoices = context.Invoices.Where(predicate).ToList();
204        return invoices;
205      }
206    }
207
208    #endregion
209  }
210}
Note: See TracBrowser for help on using the repository browser.