1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Data;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Text;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Threading;
|
---|
28 | using HeuristicLab.DataAccess.Interfaces;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataAccess.ADOHelper {
|
---|
31 | public abstract class DataAdapterBase<AdapterT, ObjT, RowT>
|
---|
32 | where AdapterT : new()
|
---|
33 | where RowT : System.Data.DataRow
|
---|
34 | where ObjT : IPersistableObject, new() {
|
---|
35 |
|
---|
36 | private ISession session;
|
---|
37 |
|
---|
38 | private IDataAdapterWrapper<AdapterT, ObjT, RowT> dataAdapter;
|
---|
39 |
|
---|
40 | protected DataAdapterBase(
|
---|
41 | IDataAdapterWrapper<AdapterT, ObjT, RowT> dataAdapter) {
|
---|
42 | this.dataAdapter = dataAdapter;
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected AdapterT Adapter {
|
---|
46 | get {
|
---|
47 | return dataAdapter.TransactionalAdapter;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected IDataAdapterWrapper<AdapterT, ObjT, RowT> DataAdapterWrapper {
|
---|
52 | get {
|
---|
53 | return dataAdapter;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public ISession Session {
|
---|
58 | get {
|
---|
59 | return this.session;
|
---|
60 | }
|
---|
61 |
|
---|
62 | set {
|
---|
63 | if (!(value is Session))
|
---|
64 | throw new Exception("Can only bind to ADO session");
|
---|
65 |
|
---|
66 | this.session = value;
|
---|
67 | this.dataAdapter.Session = value as Session;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public object InnerAdapter {
|
---|
72 | get {
|
---|
73 | return this.Adapter;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | #region Abstract methods
|
---|
78 | protected abstract RowT ConvertObj(ObjT obj, RowT row);
|
---|
79 |
|
---|
80 | protected abstract ObjT ConvertRow(RowT row, ObjT obj);
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | protected delegate IEnumerable<RowT> Selector();
|
---|
84 |
|
---|
85 | protected ObjT Convert(RowT row, ObjT obj) {
|
---|
86 | try {
|
---|
87 | obj = ConvertRow(row, obj);
|
---|
88 | return obj;
|
---|
89 | }
|
---|
90 | catch (DeletedRowInaccessibleException) {
|
---|
91 | return default(ObjT);
|
---|
92 | }
|
---|
93 | catch (RowNotInTableException) {
|
---|
94 | return default(ObjT);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected RowT FindSingleRow(Selector selector) {
|
---|
99 | RowT row = default(RowT);
|
---|
100 |
|
---|
101 | IEnumerable<RowT> found =
|
---|
102 | selector();
|
---|
103 |
|
---|
104 | if (found.Count<RowT>() == 1)
|
---|
105 | row = found.First<RowT>();
|
---|
106 |
|
---|
107 | return row;
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected ObjT FindSingle(Selector selector) {
|
---|
111 | ITransaction trans =
|
---|
112 | session.GetCurrentTransaction();
|
---|
113 | bool transactionExists = trans != null;
|
---|
114 | if (!transactionExists) {
|
---|
115 | trans = session.BeginTransaction();
|
---|
116 | }
|
---|
117 |
|
---|
118 | try {
|
---|
119 | RowT row = FindSingleRow(selector);
|
---|
120 |
|
---|
121 | ObjT result;
|
---|
122 | if (row != null) {
|
---|
123 | ObjT obj = new ObjT();
|
---|
124 | obj = Convert(row, obj);
|
---|
125 |
|
---|
126 | result = obj;
|
---|
127 | } else {
|
---|
128 | result = default(ObjT);
|
---|
129 | }
|
---|
130 |
|
---|
131 | return result;
|
---|
132 | }
|
---|
133 | finally {
|
---|
134 | if (!transactionExists && trans != null) {
|
---|
135 | trans.Commit();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected ICollection<ObjT> FindMultiple(Selector selector) {
|
---|
141 | ITransaction trans =
|
---|
142 | session.GetCurrentTransaction();
|
---|
143 | bool transactionExists = trans != null;
|
---|
144 | if (!transactionExists) {
|
---|
145 | trans = session.BeginTransaction();
|
---|
146 | }
|
---|
147 |
|
---|
148 | try {
|
---|
149 | IEnumerable<RowT> found =
|
---|
150 | selector();
|
---|
151 |
|
---|
152 | IList<ObjT> result =
|
---|
153 | new List<ObjT>();
|
---|
154 |
|
---|
155 | foreach (RowT row in found) {
|
---|
156 | ObjT obj = new ObjT();
|
---|
157 | obj = Convert(row, obj);
|
---|
158 | if (obj != null)
|
---|
159 | result.Add(obj);
|
---|
160 | }
|
---|
161 |
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 | finally {
|
---|
165 | if (!transactionExists && trans != null) {
|
---|
166 | trans.Commit();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | protected virtual RowT GetRowById(Guid id) {
|
---|
172 | return FindSingleRow(
|
---|
173 | delegate() {
|
---|
174 | return dataAdapter.FindById(id);
|
---|
175 | });
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected virtual void doUpdate(ObjT obj) {
|
---|
179 | if (obj != null) {
|
---|
180 | RowT row = null;
|
---|
181 |
|
---|
182 | if (obj.Id != Guid.Empty) {
|
---|
183 | row = GetRowById(obj.Id);
|
---|
184 | } else {
|
---|
185 | obj.Id = Guid.NewGuid();
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (row == null) {
|
---|
189 | row = dataAdapter.InsertNewRow(obj);
|
---|
190 | }
|
---|
191 |
|
---|
192 | ConvertObj(obj, row);
|
---|
193 | dataAdapter.UpdateRow(row);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void Update(ObjT obj) {
|
---|
198 | ITransaction trans =
|
---|
199 | session.GetCurrentTransaction();
|
---|
200 | bool transactionExists = trans != null;
|
---|
201 | if (!transactionExists) {
|
---|
202 | trans = session.BeginTransaction();
|
---|
203 | }
|
---|
204 |
|
---|
205 | try {
|
---|
206 | doUpdate(obj);
|
---|
207 | }
|
---|
208 | catch (DBConcurrencyException ex) {
|
---|
209 | DataRow row = ex.Row;
|
---|
210 |
|
---|
211 | RowT current = GetRowById(obj.Id);
|
---|
212 | if (current != null) {
|
---|
213 | //find out changes
|
---|
214 | for (int i = 0; i < row.ItemArray.Length; i++) {
|
---|
215 | if (!row[i, DataRowVersion.Current].Equals(
|
---|
216 | row[i, DataRowVersion.Original])) {
|
---|
217 | current[i] = row[i];
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | ConvertRow(current, obj);
|
---|
222 | //try updating again
|
---|
223 | Update(obj);
|
---|
224 | }
|
---|
225 | //otherwise: row was deleted in the meantime - nothing to do
|
---|
226 | }
|
---|
227 | finally {
|
---|
228 | if (!transactionExists && trans != null) {
|
---|
229 | trans.Commit();
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | public virtual ObjT GetById(Guid id) {
|
---|
235 | return FindSingle(delegate() {
|
---|
236 | return dataAdapter.FindById(id);
|
---|
237 | });
|
---|
238 | }
|
---|
239 |
|
---|
240 | public virtual ICollection<ObjT> GetAll() {
|
---|
241 | return new List<ObjT>(
|
---|
242 | FindMultiple(
|
---|
243 | new Selector(dataAdapter.FindAll)));
|
---|
244 | }
|
---|
245 |
|
---|
246 | protected virtual bool doDelete(ObjT obj) {
|
---|
247 | bool success = false;
|
---|
248 |
|
---|
249 | if (obj != null) {
|
---|
250 | RowT row =
|
---|
251 | GetRowById(obj.Id);
|
---|
252 |
|
---|
253 | if (row != null) {
|
---|
254 | row.Delete();
|
---|
255 | dataAdapter.UpdateRow(row);
|
---|
256 |
|
---|
257 | success = true;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | return success;
|
---|
262 | }
|
---|
263 |
|
---|
264 | public bool Delete(ObjT obj) {
|
---|
265 | ITransaction trans =
|
---|
266 | session.GetCurrentTransaction();
|
---|
267 | bool transactionExists = trans != null;
|
---|
268 | if (!transactionExists) {
|
---|
269 | trans = session.BeginTransaction();
|
---|
270 | }
|
---|
271 |
|
---|
272 | try {
|
---|
273 | return doDelete(obj);
|
---|
274 | }
|
---|
275 | catch (DBConcurrencyException) {
|
---|
276 | RowT current = GetRowById(obj.Id);
|
---|
277 | if (current != null) {
|
---|
278 | ConvertRow(current, obj);
|
---|
279 | //try deleting again
|
---|
280 | return Delete(obj);
|
---|
281 | } else {
|
---|
282 | //row has already been deleted
|
---|
283 | return false;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | finally {
|
---|
287 | if (!transactionExists && trans != null) {
|
---|
288 | trans.Commit();
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|