[995] | 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 |
|
---|
| 22 | using System;
|
---|
[925] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[995] | 26 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 27 | using System.Runtime.CompilerServices;
|
---|
[925] | 28 |
|
---|
| 29 | namespace HeuristicLab.Hive.Server.ADODataAccess {
|
---|
[995] | 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();
|
---|
[925] | 36 |
|
---|
[995] | 37 | #region Abstract methods
|
---|
| 38 | protected abstract RowT Convert(ObjT obj, RowT row);
|
---|
| 39 |
|
---|
| 40 | protected abstract ObjT Convert(RowT row, ObjT obj);
|
---|
| 41 |
|
---|
[1093] | 42 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 43 | protected abstract RowT InsertNewRow(ObjT obj);
|
---|
| 44 |
|
---|
[1093] | 45 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 46 | protected abstract void UpdateRow(RowT row);
|
---|
| 47 |
|
---|
[1093] | 48 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 49 | protected abstract IEnumerable<RowT> FindById(long id);
|
---|
| 50 |
|
---|
[1093] | 51 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 52 | protected abstract IEnumerable<RowT> FindAll();
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | protected delegate IEnumerable<RowT> Selector();
|
---|
| 56 |
|
---|
[1088] | 57 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 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;
|
---|
[925] | 68 | }
|
---|
| 69 |
|
---|
[995] | 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 | }
|
---|
[925] | 81 | }
|
---|
[995] | 82 |
|
---|
[1088] | 83 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 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);
|
---|
[1025] | 146 |
|
---|
| 147 | return true;
|
---|
[995] | 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | return false;
|
---|
| 152 | }
|
---|
[925] | 153 | }
|
---|
| 154 | }
|
---|