Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/GenericDao.cs @ 15834

Last change on this file since 15834 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Data.Linq;
25using System.Linq;
26using System.Linq.Expressions;
27using HeuristicLab.Services.Hive.DataAccess.Interfaces;
28
29namespace HeuristicLab.Services.Hive.DataAccess.Daos {
30  public abstract class GenericDao<PK, T> : IGenericDao<PK, T> where T : class {
31    private readonly DataContext dataContext;
32    protected DataContext DataContext {
33      get { return dataContext; }
34    }
35
36    protected Table<T> Table {
37      get { return dataContext.GetTable<T>(); }
38    }
39
40    protected GenericDao(DataContext dataContext) {
41      this.dataContext = dataContext;
42    }
43
44    #region IGenericDao<PK,T> Members
45    public abstract T GetById(PK id);
46
47    public IQueryable<T> Get(Expression<Func<T, bool>> predicate) {
48      return Table.Where(predicate);
49    }
50
51    public IQueryable<T> GetAll() {
52      return Table;
53    }
54
55    public T Save(T entity) {
56      Table.InsertOnSubmit(entity);
57      return entity;
58    }
59
60    public IEnumerable<T> Save(params T[] entities) {
61      return entities.Select(Save).ToList();
62    }
63
64    public IEnumerable<T> Save(IEnumerable<T> entities) {
65      return entities.Select(Save).ToList();
66    }
67
68    public T SaveOrAttach(T entity) {
69      if (Table.Contains(entity)) {
70        if (Table.GetOriginalEntityState(entity) == null) {
71          Table.Attach(entity);
72        }
73      } else {
74        Table.InsertOnSubmit(entity);
75      }
76      return entity;
77    }
78
79    public IEnumerable<T> SaveOrAttach(params T[] entities) {
80      return entities.Select(SaveOrAttach).ToList();
81    }
82
83    public IEnumerable<T> SaveOrAttach(IEnumerable<T> entities) {
84      return entities.Select(SaveOrAttach).ToList();
85    }
86
87    public void Delete(PK id) {
88      T entity = GetById(id);
89      if (entity != null) {
90        Table.DeleteOnSubmit(entity);
91      }
92    }
93
94    public virtual void Delete(IEnumerable<PK> ids) {
95      foreach (var id in ids) {
96        Delete(id);
97      }
98    }
99
100    public void Delete(T entity) {
101      Table.DeleteOnSubmit(entity);
102    }
103
104    public void Delete(IEnumerable<T> entities) {
105      foreach (var entity in entities) {
106        Delete(entity);
107      }
108    }
109
110    public bool Exists(T entity) {
111      return ExistsQuery(DataContext, entity);
112    }
113
114    public bool Exists(PK id) {
115      return GetById(id) != null;
116    }
117    #endregion
118
119    #region Compiled queries
120    private static readonly Func<DataContext, T, bool> ExistsQuery =
121      CompiledQuery.Compile((DataContext db, T entity) =>
122        db.GetTable<T>().Contains(entity));
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.