Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/3.2/DataAdapterBase.cs @ 2021

Last change on this file since 2021 was 2004, checked in by svonolfe, 15 years ago

Added paging to DAL (#372)

File size: 7.8 KB
Line 
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
22using System;
23using System.Data;
24using System.Collections.Generic;
25using System.Text;
26using System.Linq;
27using System.Threading;
28using HeuristicLab.DataAccess.Interfaces;
29
30namespace 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 ITableAdapterWrapper<AdapterT, ObjT, RowT> dataAdapter;
39
40    protected DataAdapterBase(
41      ITableAdapterWrapper<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 ITableAdapterWrapper<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    private ICollection<ObjT> FindMultiple(Selector selector,
141      int from, int size) {
142      ITransaction trans =
143       session.GetCurrentTransaction();
144      bool transactionExists = trans != null;
145      if (!transactionExists) {
146        trans = session.BeginTransaction();
147      }
148
149      try {
150        IEnumerable<RowT> found =
151          selector();
152
153        if (from > 0 && size > 0)
154          found = found.Skip<RowT>(from).Take<RowT>(size);
155
156        IList<ObjT> result =
157          new List<ObjT>();
158
159        foreach (RowT row in found) {
160          ObjT obj = new ObjT();
161          obj = Convert(row, obj);
162          if (obj != null)
163            result.Add(obj);
164        }
165
166        return result;
167      }
168      finally {
169        if (!transactionExists && trans != null) {
170          trans.Commit();
171        }
172      }     
173    }
174
175    protected ICollection<ObjT> FindMultiple(Selector selector) {
176      return FindMultiple(
177        selector, 0, -1);
178    }
179
180    protected virtual RowT GetRowById(Guid id) {
181      return FindSingleRow(
182        delegate() {
183          return dataAdapter.FindById(id);
184        });
185    }
186
187    protected virtual void doUpdate(ObjT obj) {
188      if (obj != null) {
189        RowT row = null;
190
191        if (obj.Id != Guid.Empty) {
192          row = GetRowById(obj.Id);
193        } else {
194          obj.Id = Guid.NewGuid();
195        }
196
197        if (row == null) {
198          row = dataAdapter.InsertNewRow(obj);
199        }
200
201        ConvertObj(obj, row);
202        dataAdapter.UpdateRow(row);
203      }
204    }
205
206    public void Update(ObjT obj) {
207      ITransaction trans =
208        session.GetCurrentTransaction();
209      bool transactionExists = trans != null;
210      if (!transactionExists) {
211        trans = session.BeginTransaction();
212      }
213
214      try {
215        doUpdate(obj);
216      }
217      catch (DBConcurrencyException ex) {
218        DataRow row = ex.Row;
219
220        RowT current = GetRowById(obj.Id);
221        if (current != null) {
222          //find out changes
223          for (int i = 0; i < row.ItemArray.Length; i++) {           
224            if (!row[i, DataRowVersion.Current].Equals(
225                 row[i, DataRowVersion.Original])) {
226              current[i] = row[i];
227            }
228          }
229
230          ConvertRow(current, obj);
231          //try updating again
232          Update(obj);
233        }
234        //otherwise: row was deleted in the meantime - nothing to do
235      }
236      finally {
237        if (!transactionExists && trans != null) {
238          trans.Commit();
239        }
240      }
241    }
242
243    public virtual ObjT GetById(Guid id) {
244      return FindSingle(delegate() {
245        return dataAdapter.FindById(id);
246      });
247    }
248
249    public virtual ICollection<ObjT> GetAll() {
250      return new List<ObjT>(
251        FindMultiple(
252          new Selector(dataAdapter.FindAll)));
253    }
254
255    public virtual ICollection<ObjT> GetAll(int from, int size) {
256      //note - this base implementation is inefficient,
257      //consider overriding the implementation
258      //in derived adapters (based on a query (SQL LIMIT))
259      return new List<ObjT>(
260        FindMultiple(
261          new Selector(dataAdapter.FindAll),
262          from, size));
263    }
264
265    protected virtual bool doDelete(ObjT obj) {
266      bool success = false;
267
268      if (obj != null) {
269        RowT row =
270          GetRowById(obj.Id);
271
272        if (row != null) {
273          row.Delete();
274          dataAdapter.UpdateRow(row);
275
276          success = true;
277        }
278      }
279
280      return success;
281    }
282
283    public bool Delete(ObjT obj) {
284      ITransaction trans =
285        session.GetCurrentTransaction();
286      bool transactionExists = trans != null;
287      if (!transactionExists) {
288        trans = session.BeginTransaction();
289      }
290
291      try {
292        return doDelete(obj);
293      }
294      catch (DBConcurrencyException) {
295        RowT current = GetRowById(obj.Id);
296        if (current != null) {
297          ConvertRow(current, obj);
298          //try deleting again
299          return Delete(obj);
300        } else {
301          //row has already been deleted
302          return false;
303        }
304      }
305      finally {
306        if (!transactionExists && trans != null) {
307          trans.Commit();
308        }
309      } 
310    }
311  }
312}
313
Note: See TracBrowser for help on using the repository browser.