1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Data.Entity;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Linq.Expressions;
|
---|
7 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
8 | namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
|
---|
9 | public class BillingDao {
|
---|
10 |
|
---|
11 | #region Context Creation and Execution
|
---|
12 |
|
---|
13 | private BillingContext CreateContext() {
|
---|
14 | BillingContext context = new BillingContext("name=BillingContext");
|
---|
15 | return context;
|
---|
16 | }
|
---|
17 |
|
---|
18 | private delegate T ExecuteWithContextDelegate<T>(BillingContext context);
|
---|
19 | private delegate void ExecuteWithContextDelegate(BillingContext context);
|
---|
20 |
|
---|
21 | private void ExecuteWithContext(ExecuteWithContextDelegate call) {
|
---|
22 | BillingContext context = null;
|
---|
23 | try {
|
---|
24 | using (context = CreateContext()) {
|
---|
25 | call(context);
|
---|
26 | }
|
---|
27 | }
|
---|
28 | finally {
|
---|
29 | if (context.Database.Connection.State == System.Data.ConnectionState.Open) {
|
---|
30 | context.Database.Connection.Close();
|
---|
31 | }
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | private T ExecuteWithContext<T>(ExecuteWithContextDelegate<T> call) {
|
---|
36 | BillingContext context = null;
|
---|
37 | try {
|
---|
38 | using (context = CreateContext()) {
|
---|
39 | T result = call(context);
|
---|
40 | return result;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | finally {
|
---|
44 | if (context.Database.Connection.State == System.Data.ConnectionState.Open) {
|
---|
45 | context.Database.Connection.Close();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region Product Methods
|
---|
53 |
|
---|
54 | public Product FindProductById(long id) {
|
---|
55 | return ExecuteWithContext<Product>((context) => {
|
---|
56 | return context.Products.FirstOrDefault(prod => prod.ProductId == id);
|
---|
57 | });
|
---|
58 | }
|
---|
59 |
|
---|
60 | public IEnumerable<Product> FindProdcutBy(Expression<Func<Product, bool>> predicate) {
|
---|
61 | using (var context = CreateContext()) {
|
---|
62 | var products = context.Products.Where(predicate).ToList();
|
---|
63 | return products;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IEnumerable<Product> FindAllProducts() {
|
---|
68 | using (var context = CreateContext()) {
|
---|
69 | var products = context.Products.ToList();
|
---|
70 | return products;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void AddProduct(Product entity) {
|
---|
75 | ExecuteWithContext((context) => {
|
---|
76 | context.Products.Add(entity);
|
---|
77 | context.SaveChanges();
|
---|
78 | });
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void UpdateProduct(Product entity) {
|
---|
82 | ExecuteWithContext((context) => {
|
---|
83 | context.Products.Attach(entity);
|
---|
84 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
85 | context.SaveChanges();
|
---|
86 | });
|
---|
87 | }
|
---|
88 |
|
---|
89 | void DeleteProduct(Product entity) {
|
---|
90 | ExecuteWithContext((context) => {
|
---|
91 | context.Products.Remove(entity);
|
---|
92 | });
|
---|
93 | }
|
---|
94 |
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Order Methods
|
---|
98 |
|
---|
99 | public void AddOrder(Order order) {
|
---|
100 | ExecuteWithContext((context) => {
|
---|
101 | context.Orders.Add(order);
|
---|
102 | context.SaveChanges();
|
---|
103 | });
|
---|
104 | }
|
---|
105 |
|
---|
106 | public IEnumerable<Order> FindOrderBy(Expression<Func<Order, bool>> predicate) {
|
---|
107 | using (var context = CreateContext()) {
|
---|
108 | var orders = context.Orders
|
---|
109 | .Include(o => o.OrderLines)
|
---|
110 | .Include(o => o.User)
|
---|
111 | .Include(o => o.Invoices)
|
---|
112 | .Include(o => o.OrderLines.Select(ol => ol.Product))
|
---|
113 | .Where(predicate).ToList();
|
---|
114 | return orders;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region OrderLine Methods
|
---|
121 |
|
---|
122 | public OrderLine FindOrderLineById(long id) {
|
---|
123 | return ExecuteWithContext<OrderLine>((context) => {
|
---|
124 | return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id);
|
---|
125 | });
|
---|
126 | }
|
---|
127 |
|
---|
128 | public IEnumerable<OrderLine> FindOrderLineBy(Expression<Func<OrderLine, bool>> predicate) {
|
---|
129 | using (var context = CreateContext()) {
|
---|
130 | var orderLines = context.OrderLines.Where(predicate).ToList();
|
---|
131 | return orderLines;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public IEnumerable<OrderLine> FindAllOrderLines() {
|
---|
136 | using (var context = CreateContext()) {
|
---|
137 | var orderLines = context.OrderLines.ToList();
|
---|
138 | return orderLines;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void AddOrderLine(OrderLine entity) {
|
---|
143 | ExecuteWithContext((context) => {
|
---|
144 | context.OrderLines.Add(entity);
|
---|
145 | context.SaveChanges();
|
---|
146 | });
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void UpdateOrderLine(OrderLine entity) {
|
---|
150 | ExecuteWithContext((context) => {
|
---|
151 | context.OrderLines.Attach(entity);
|
---|
152 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
153 | context.SaveChanges();
|
---|
154 | });
|
---|
155 | }
|
---|
156 |
|
---|
157 | void DeleteOrderLine(OrderLine entity) {
|
---|
158 | ExecuteWithContext((context) => {
|
---|
159 | context.OrderLines.Remove(entity);
|
---|
160 | });
|
---|
161 | }
|
---|
162 |
|
---|
163 | #endregion
|
---|
164 |
|
---|
165 | #region User Methods
|
---|
166 |
|
---|
167 | public User FindUserById(long id) {
|
---|
168 | return ExecuteWithContext<User>((context) => {
|
---|
169 | return context.Users.FirstOrDefault(user => user.UserId == id);
|
---|
170 | });
|
---|
171 | }
|
---|
172 |
|
---|
173 | public IEnumerable<User> FindUserBy(Expression<Func<User, bool>> predicate) {
|
---|
174 | using (var context = CreateContext()) {
|
---|
175 | var users = context.Users.Where(predicate).ToList();
|
---|
176 | return users;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public IEnumerable<User> FindAllUsers() {
|
---|
181 | using (var context = CreateContext()) {
|
---|
182 | var users = context.Users.ToList();
|
---|
183 | return users;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | public void AddUser(User entity) {
|
---|
188 | ExecuteWithContext((context) => {
|
---|
189 | context.Users.Add(entity);
|
---|
190 | context.SaveChanges();
|
---|
191 | });
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void UpdateUser(User entity) {
|
---|
195 | ExecuteWithContext((context) => {
|
---|
196 | context.Users.Attach(entity);
|
---|
197 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
198 | context.SaveChanges();
|
---|
199 | });
|
---|
200 | }
|
---|
201 |
|
---|
202 | void DeleteUser(User entity) {
|
---|
203 | ExecuteWithContext((context) => {
|
---|
204 | context.Users.Remove(entity);
|
---|
205 | });
|
---|
206 | }
|
---|
207 |
|
---|
208 | #endregion
|
---|
209 |
|
---|
210 | #region UsageRecords Methods
|
---|
211 |
|
---|
212 | public UsageRecord FindUsageRecordById(long id) {
|
---|
213 | return ExecuteWithContext<UsageRecord>((context) => {
|
---|
214 | return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id);
|
---|
215 | });
|
---|
216 | }
|
---|
217 |
|
---|
218 | public IEnumerable<UsageRecord> FindUsageRecordsBy(Expression<Func<UsageRecord, bool>> predicate) {
|
---|
219 | using (var context = CreateContext()) {
|
---|
220 | var records = context.UsageRecords.Where(predicate).ToList();
|
---|
221 | return records;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | public IEnumerable<UsageRecord> FindAllUsageRecords() {
|
---|
226 | using (var context = CreateContext()) {
|
---|
227 | var records = context.UsageRecords.ToList();
|
---|
228 | return records;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void AddUsageRecord(UsageRecord entity) {
|
---|
233 | ExecuteWithContext((context) => {
|
---|
234 | context.UsageRecords.Add(entity);
|
---|
235 | context.SaveChanges();
|
---|
236 | });
|
---|
237 | }
|
---|
238 |
|
---|
239 | public void UpdateUsageRecord(UsageRecord entity) {
|
---|
240 | ExecuteWithContext((context) => {
|
---|
241 | context.UsageRecords.Attach(entity);
|
---|
242 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
243 | context.SaveChanges();
|
---|
244 | });
|
---|
245 | }
|
---|
246 |
|
---|
247 | void DeleteUsageRecord(UsageRecord entity) {
|
---|
248 | ExecuteWithContext((context) => {
|
---|
249 | context.UsageRecords.Remove(entity);
|
---|
250 | });
|
---|
251 | }
|
---|
252 |
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | #region Invoice Methods
|
---|
256 |
|
---|
257 | public void AddInvoice(Invoice entity) {
|
---|
258 | ExecuteWithContext((context) => {
|
---|
259 | context.Invoices.Add(entity);
|
---|
260 | context.SaveChanges();
|
---|
261 | });
|
---|
262 | }
|
---|
263 |
|
---|
264 | public IEnumerable<Invoice> FindInvoiceBy(Expression<Func<Invoice, bool>> predicate) {
|
---|
265 | using (var context = CreateContext()) {
|
---|
266 | var invoices = context.Invoices.Where(predicate).ToList();
|
---|
267 | return invoices;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | #endregion
|
---|
272 | }
|
---|
273 | }
|
---|