Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/DataAdapterBase.cs @ 1131

Last change on this file since 1131 was 1131, checked in by svonolfe, 16 years ago

fixed race condition issues, improved performance (#372)

File size: 4.2 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.Linq;
26using System.Text;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Runtime.CompilerServices;
29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  abstract class DataAdapterBase<AdapterT, ObjT, RowT>
32    where AdapterT : new()
33    where RowT : System.Data.DataRow
34    where ObjT : IHiveObject, new() {
35    protected AdapterT Adapter {
36      get {
37        return new AdapterT();
38      }
39    }
40
41    #region Abstract methods
42    protected abstract RowT ConvertObj(ObjT obj, RowT row);
43
44    protected abstract ObjT ConvertRow(RowT row, ObjT obj);
45
46    [MethodImpl(MethodImplOptions.Synchronized)]
47    protected abstract RowT InsertNewRow(ObjT obj);
48
49    [MethodImpl(MethodImplOptions.Synchronized)]
50    protected abstract void UpdateRow(RowT row);
51
52    protected abstract IEnumerable<RowT> FindById(long id);
53
54    protected abstract IEnumerable<RowT> FindAll();
55    #endregion
56
57    protected delegate IEnumerable<RowT> Selector();
58
59    protected ObjT Convert(RowT row, ObjT obj) {
60      try {
61        obj = ConvertRow(row, obj);
62        return obj;
63      }
64      catch (DeletedRowInaccessibleException) {
65        return default(ObjT);
66      }
67      catch (RowNotInTableException) {
68        return default(ObjT);
69      }
70    }
71
72    protected virtual RowT FindSingleRow(Selector selector) {
73      RowT row = default(RowT);
74
75      IEnumerable<RowT> found =
76        selector();
77
78      if (found.Count<RowT>() == 1)
79        row = found.First<RowT>();
80
81      return row;
82    }
83
84    protected virtual ObjT FindSingle(Selector selector) {
85      RowT row = FindSingleRow(selector);
86
87      if (row != null) {
88        ObjT obj = new ObjT();
89        obj = Convert(row, obj);
90
91        return obj;
92      } else {
93        return default(ObjT);
94      }
95    }
96
97    protected virtual ICollection<ObjT> FindMultiple(Selector selector) {
98      IEnumerable<RowT> found =
99        selector();
100
101      IList<ObjT> result =
102        new List<ObjT>();
103
104      foreach (RowT row in found) {
105        ObjT obj = new ObjT();
106        obj = Convert(row, obj);
107        if(obj != null)
108          result.Add(obj);
109      }
110
111      return result;
112    }
113
114    protected virtual RowT GetRowById(long id) {
115      return FindSingleRow(
116        delegate() {
117          return FindById(id);
118        });
119    }
120
121    [MethodImpl(MethodImplOptions.Synchronized)]
122    public virtual void Update(ObjT obj) {
123      if (obj != null) {
124        RowT row =
125          GetRowById(obj.Id);
126
127        if (row == null) {
128          row = InsertNewRow(obj);
129        }
130
131        ConvertObj(obj, row);
132
133        UpdateRow(row);
134
135        obj.Id = (long)row[row.Table.PrimaryKey[0]];
136      }
137    }
138
139    public virtual ObjT GetById(long id) {
140      return FindSingle(delegate() {
141        return FindById(id);
142      });
143    }
144
145    public virtual ICollection<ObjT> GetAll() {
146      return new List<ObjT>(
147        FindMultiple(
148          new Selector(FindAll)));
149    }
150
151    [MethodImpl(MethodImplOptions.Synchronized)]
152    public virtual bool Delete(ObjT obj) {
153      if (obj != null) {
154        RowT row =
155          GetRowById(obj.Id);
156
157        if (row != null) {
158          row.Delete();
159          UpdateRow(row);
160
161          return true;
162        }
163      }
164
165      return false;
166    }
167  }
168}
169
Note: See TracBrowser for help on using the repository browser.