Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added lock to avoid race conditions (#372).

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