Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL, Improved Caching (#372)

File size: 3.7 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    protected virtual RowT FindSingleRow(Selector selector) {
54      RowT row = default(RowT);
55
56      IEnumerable<RowT> found =
57        selector();
58
59      if (found.Count<RowT>() == 1)
60        row = found.First<RowT>();
61
62      return row;
63    }
64
65    protected virtual ObjT FindSingle(Selector selector) {
66      RowT row = FindSingleRow(selector);
67
68      if (row != null) {
69        ObjT obj = new ObjT();
70        Convert(row, obj);
71
72        return obj;
73      } else {
74        return default(ObjT);
75      }
76    }
77
78    protected virtual ICollection<ObjT> FindMultiple(Selector selector) {
79      IEnumerable<RowT> found =
80        selector();
81
82      IList<ObjT> result =
83        new List<ObjT>();
84
85      foreach (RowT row in found) {
86        ObjT obj = new ObjT();
87        Convert(row, obj);
88        result.Add(obj);
89      }
90
91      return result;
92    }
93
94    protected virtual RowT GetRowById(long id) {
95      return FindSingleRow(
96        delegate() {
97          return FindById(id);
98        });
99    }
100
101    [MethodImpl(MethodImplOptions.Synchronized)]
102    public virtual void Update(ObjT obj) {
103      if (obj != null) {
104        RowT row =
105          GetRowById(obj.Id);
106
107        if (row == null) {
108          row = InsertNewRow(obj);
109        }
110
111        Convert(obj, row);
112
113        UpdateRow(row);
114
115        obj.Id = (long)row[row.Table.PrimaryKey[0]];
116      }
117    }
118
119    public virtual ObjT GetById(long id) {
120      return FindSingle(delegate() {
121        return FindById(id);
122      });
123    }
124
125    public virtual ICollection<ObjT> GetAll() {
126      return new List<ObjT>(
127        FindMultiple(
128          new Selector(FindAll)));
129    }
130
131    [MethodImpl(MethodImplOptions.Synchronized)]
132    public virtual bool Delete(ObjT obj) {
133      if (obj != null) {
134        RowT row =
135          GetRowById(obj.Id);
136
137        if (row != null) {
138          row.Delete();
139          UpdateRow(row);
140        }
141      }
142
143      return false;
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.