#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Linq.Expressions;
using HeuristicLab.Services.Hive.DataAccess.Interfaces;
namespace HeuristicLab.Services.Hive.DataAccess.Daos {
public abstract class GenericDao : IGenericDao where T : class {
private readonly DataContext dataContext;
protected DataContext DataContext {
get { return dataContext; }
}
protected Table Table {
get { return dataContext.GetTable(); }
}
protected GenericDao(DataContext dataContext) {
this.dataContext = dataContext;
}
#region IGenericDao Members
public abstract T GetById(PK id);
public IQueryable Get(Expression> predicate) {
return Table.Where(predicate);
}
public IQueryable GetAll() {
return Table;
}
public T Save(T entity) {
Table.InsertOnSubmit(entity);
return entity;
}
public IEnumerable Save(params T[] entities) {
return entities.Select(Save).ToList();
}
public IEnumerable Save(IEnumerable entities) {
return entities.Select(Save).ToList();
}
public T SaveOrAttach(T entity) {
if (Table.Contains(entity)) {
if (Table.GetOriginalEntityState(entity) == null) {
Table.Attach(entity);
}
} else {
Table.InsertOnSubmit(entity);
}
return entity;
}
public IEnumerable SaveOrAttach(params T[] entities) {
return entities.Select(SaveOrAttach).ToList();
}
public IEnumerable SaveOrAttach(IEnumerable entities) {
return entities.Select(SaveOrAttach).ToList();
}
public void Delete(PK id) {
T entity = GetById(id);
if (entity != null) {
Table.DeleteOnSubmit(entity);
}
}
public virtual void Delete(IEnumerable ids) {
foreach (var id in ids) {
Delete(id);
}
}
public void Delete(T entity) {
Table.DeleteOnSubmit(entity);
}
public void Delete(IEnumerable entities) {
foreach (var entity in entities) {
Delete(entity);
}
}
public bool Exists(T entity) {
return ExistsQuery(DataContext, entity);
}
public bool Exists(PK id) {
return GetById(id) != null;
}
#endregion
#region Compiled queries
private static readonly Func ExistsQuery =
CompiledQuery.Compile((DataContext db, T entity) =>
db.GetTable().Contains(entity));
#endregion
}
}