Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1092 was 1088, checked in by msteinbi, 16 years ago

Started implementation of Lifecycle Management for CLient Communicator (#453)

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    protected abstract RowT InsertNewRow(ObjT obj);
43
44    protected abstract void UpdateRow(RowT row);
45
46    protected abstract IEnumerable<RowT> FindById(long id);
47
48    protected abstract IEnumerable<RowT> FindAll();
49    #endregion
50
51    protected delegate IEnumerable<RowT> Selector();
52
53    [MethodImpl(MethodImplOptions.Synchronized)]
54    protected virtual RowT FindSingleRow(Selector selector) {
55      RowT row = default(RowT);
56
57      IEnumerable<RowT> found =
58        selector();
59
60      if (found.Count<RowT>() == 1)
61        row = found.First<RowT>();
62
63      return row;
64    }
65
66    [MethodImpl(MethodImplOptions.Synchronized)]
67    protected virtual ObjT FindSingle(Selector selector) {
68      RowT row = FindSingleRow(selector);
69
70      if (row != null) {
71        ObjT obj = new ObjT();
72        Convert(row, obj);
73
74        return obj;
75      } else {
76        return default(ObjT);
77      }
78    }
79
80    [MethodImpl(MethodImplOptions.Synchronized)]
81    protected virtual ICollection<ObjT> FindMultiple(Selector selector) {
82      IEnumerable<RowT> found =
83        selector();
84
85      IList<ObjT> result =
86        new List<ObjT>();
87
88      foreach (RowT row in found) {
89        ObjT obj = new ObjT();
90        Convert(row, obj);
91        result.Add(obj);
92      }
93
94      return result;
95    }
96
97    [MethodImpl(MethodImplOptions.Synchronized)]
98    protected virtual RowT GetRowById(long id) {
99      return FindSingleRow(
100        delegate() {
101          return FindById(id);
102        });
103    }
104
105    [MethodImpl(MethodImplOptions.Synchronized)]
106    public virtual void Update(ObjT obj) {
107      if (obj != null) {
108        RowT row =
109          GetRowById(obj.Id);
110
111        if (row == null) {
112          row = InsertNewRow(obj);
113        }
114
115        Convert(obj, row);
116
117        UpdateRow(row);
118
119        obj.Id = (long)row[row.Table.PrimaryKey[0]];
120      }
121    }
122
123    [MethodImpl(MethodImplOptions.Synchronized)]
124    public virtual ObjT GetById(long id) {
125      return FindSingle(delegate() {
126        return FindById(id);
127      });
128    }
129
130    [MethodImpl(MethodImplOptions.Synchronized)]
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.