1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Linq.Expressions;
|
---|
6 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
7 | namespace 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 | #region OrderLine Methods
|
---|
115 |
|
---|
116 | public OrderLine FindOrderLineById(long id) {
|
---|
117 | return ExecuteWithContext<OrderLine>((context) => {
|
---|
118 | return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id);
|
---|
119 | });
|
---|
120 | }
|
---|
121 |
|
---|
122 | public IEnumerable<OrderLine> FindOrderLineBy(Expression<Func<OrderLine, bool>> predicate) {
|
---|
123 | using (var context = CreateContext()) {
|
---|
124 | var orderLines = context.OrderLines.Where(predicate).ToList();
|
---|
125 | return orderLines;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public IEnumerable<OrderLine> FindAllOrderLines() {
|
---|
130 | using (var context = CreateContext()) {
|
---|
131 | var orderLines = context.OrderLines.ToList();
|
---|
132 | return orderLines;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void AddOrderLine(OrderLine entity) {
|
---|
137 | ExecuteWithContext((context) => {
|
---|
138 | context.OrderLines.Add(entity);
|
---|
139 | context.SaveChanges();
|
---|
140 | });
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void UpdateOrderLine(OrderLine entity) {
|
---|
144 | ExecuteWithContext((context) => {
|
---|
145 | context.OrderLines.Attach(entity);
|
---|
146 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
147 | context.SaveChanges();
|
---|
148 | });
|
---|
149 | }
|
---|
150 |
|
---|
151 | void DeleteOrderLine(OrderLine entity) {
|
---|
152 | ExecuteWithContext((context) => {
|
---|
153 | context.OrderLines.Remove(entity);
|
---|
154 | });
|
---|
155 | }
|
---|
156 |
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | #region User Methods
|
---|
160 |
|
---|
161 | public User FindUserById(long id) {
|
---|
162 | return ExecuteWithContext<User>((context) => {
|
---|
163 | return context.Users.FirstOrDefault(user => user.UserId == id);
|
---|
164 | });
|
---|
165 | }
|
---|
166 |
|
---|
167 | public IEnumerable<User> FindUserBy(Expression<Func<User, bool>> predicate) {
|
---|
168 | using (var context = CreateContext()) {
|
---|
169 | var users = context.Users.Where(predicate).ToList();
|
---|
170 | return users;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | public IEnumerable<User> FindAllUsers() {
|
---|
175 | using (var context = CreateContext()) {
|
---|
176 | var users = context.Users.ToList();
|
---|
177 | return users;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | public void AddUser(User entity) {
|
---|
182 | ExecuteWithContext((context) => {
|
---|
183 | context.Users.Add(entity);
|
---|
184 | context.SaveChanges();
|
---|
185 | });
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void UpdateUser(User entity) {
|
---|
189 | ExecuteWithContext((context) => {
|
---|
190 | context.Users.Attach(entity);
|
---|
191 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
192 | context.SaveChanges();
|
---|
193 | });
|
---|
194 | }
|
---|
195 |
|
---|
196 | void DeleteUser(User entity) {
|
---|
197 | ExecuteWithContext((context) => {
|
---|
198 | context.Users.Remove(entity);
|
---|
199 | });
|
---|
200 | }
|
---|
201 |
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 | #region UsageRecords Methods
|
---|
205 |
|
---|
206 | public UsageRecord FindUsageRecordById(long id) {
|
---|
207 | return ExecuteWithContext<UsageRecord>((context) => {
|
---|
208 | return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id);
|
---|
209 | });
|
---|
210 | }
|
---|
211 |
|
---|
212 | public IEnumerable<UsageRecord> FindUsageRecordsBy(Expression<Func<UsageRecord, bool>> predicate) {
|
---|
213 | using (var context = CreateContext()) {
|
---|
214 | var records = context.UsageRecords.Where(predicate).ToList();
|
---|
215 | return records;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public IEnumerable<UsageRecord> FindAllUsageRecords() {
|
---|
220 | using (var context = CreateContext()) {
|
---|
221 | var records = context.UsageRecords.ToList();
|
---|
222 | return records;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void AddUsageRecord(UsageRecord entity) {
|
---|
227 | ExecuteWithContext((context) => {
|
---|
228 | context.UsageRecords.Add(entity);
|
---|
229 | context.SaveChanges();
|
---|
230 | });
|
---|
231 | }
|
---|
232 |
|
---|
233 | public void UpdateUsageRecord(UsageRecord entity) {
|
---|
234 | ExecuteWithContext((context) => {
|
---|
235 | context.UsageRecords.Attach(entity);
|
---|
236 | context.Entry(entity).State = System.Data.EntityState.Modified;
|
---|
237 | context.SaveChanges();
|
---|
238 | });
|
---|
239 | }
|
---|
240 |
|
---|
241 | void DeleteUsageRecord(UsageRecord entity) {
|
---|
242 | ExecuteWithContext((context) => {
|
---|
243 | context.UsageRecords.Remove(entity);
|
---|
244 | });
|
---|
245 | }
|
---|
246 |
|
---|
247 | #endregion
|
---|
248 |
|
---|
249 | #region Invoice Methods
|
---|
250 |
|
---|
251 | public void AddInvoice(Invoice entity) {
|
---|
252 | ExecuteWithContext((context) => {
|
---|
253 | context.Invoices.Add(entity);
|
---|
254 | context.SaveChanges();
|
---|
255 | });
|
---|
256 | }
|
---|
257 |
|
---|
258 | public IEnumerable<Invoice> FindInvoiceBy(Expression<Func<Invoice, bool>> predicate) {
|
---|
259 | using (var context = CreateContext()) {
|
---|
260 | var invoices = context.Invoices.Where(predicate).ToList();
|
---|
261 | return invoices;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | #endregion
|
---|
266 | }
|
---|
267 | }
|
---|