271 lines
8.9 KiB
C#
271 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Abstrakte Implementation des Service Interface als Basis für spezialisierte Klassen
|
|
/// </summary>
|
|
/// <typeparam name="T">Typ der Klasse</typeparam>
|
|
public abstract class ServiceBase<T> : IDisposable, IService<T> where T : class
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private bool _disposed = false;
|
|
internal readonly IRepository<T> Repository;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
protected ServiceBase(IUnitOfWork unitOfWork)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
Repository = _unitOfWork.GetRepository<T>();
|
|
}
|
|
|
|
#region Get
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität anhand der eindeutigen Id zurück
|
|
/// </summary>
|
|
/// <param name="id">Id der Entität</param>
|
|
/// <returns>Entität oder null, wenn nicht gefunden</returns>
|
|
public virtual T Get(object id)
|
|
{
|
|
return id == null ? null : Repository.Get(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität anhand der eindeutigen Id zurück
|
|
/// </summary>
|
|
/// <param name="id">Id der Entität</param>
|
|
/// <returns>Entität oder null, wenn nicht gefunden</returns>
|
|
public virtual async Task<T> GetAsync(object id)
|
|
{
|
|
return id == null ? null : await Repository.GetAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt alle Entitäten zurück
|
|
/// </summary>
|
|
/// <returns>Liste aller Entitäten</returns>
|
|
public virtual IEnumerable<T> GetAll()
|
|
{
|
|
return Repository.GetAll();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt alle Entitäten zurück
|
|
/// </summary>
|
|
/// <returns>Liste aller Entitäten</returns>
|
|
public virtual async Task<IEnumerable<T>> GetAllAsync()
|
|
{
|
|
return await Repository.GetAllAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste aller Entitäten zurück welche definierten Kriterien entsprechen
|
|
/// </summary>
|
|
/// <param name="predicate">Kriterien</param>
|
|
/// <returns>Liste gefundener Entitäten</returns>
|
|
public virtual IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
return Repository.Find(predicate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste aller Entitäten zurück welche definierten Kriterien entsprechen
|
|
/// </summary>
|
|
/// <param name="predicate">Kriterien</param>
|
|
/// <returns>Liste gefundener Entitäten</returns>
|
|
public virtual async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
return await Repository.FindAsync(predicate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste aller Entitäten zurück welche definierten Kriterien entsprechen
|
|
/// </summary>
|
|
/// <param name="predicate">Kriterien</param>
|
|
/// <returns>Liste gefundener Entitäten</returns>
|
|
public IQueryable<T> Query(Expression<Func<T, bool>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
return Repository.Query(predicate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität oder den Default wert zurück
|
|
/// </summary>
|
|
/// <param name="predicate">Kriterien</param>
|
|
/// <returns>Gefundene entität oder Default-Wert</returns>
|
|
public virtual T SingleOrDefault(Expression<Func<T, bool>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
return Repository.SingleOrDefault(predicate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität oder den Default wert zurück
|
|
/// </summary>
|
|
/// <param name="predicate">Kriterien</param>
|
|
/// <returns>Gefundene entität oder Default-Wert</returns>
|
|
public virtual async Task<T> SingleOrDefaultAsync(Expression<Func<T, bool>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
return await Repository.SingleOrDefaultAsync(predicate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität oder den Default wert zurück
|
|
/// </summary>
|
|
/// <param name="predicate">Kriterien</param>
|
|
/// <returns>Gefundene entität oder Default-Wert</returns>
|
|
public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
return await Repository.FirstOrDefaultAsync(predicate);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region CRUD
|
|
|
|
/// <summary>
|
|
/// Fügt eine Entität zum Repository hinzu
|
|
/// </summary>
|
|
/// <param name="entity">Entität</param>
|
|
public virtual void Add(T entity)
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException(nameof(entity));
|
|
Repository.Add(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fügt eine Liste von Entitäten zum Repository hinzu
|
|
/// </summary>
|
|
/// <param name="entities">List von Entitäten</param>
|
|
public virtual void AddRange(IEnumerable<T> entities)
|
|
{
|
|
if (entities == null)
|
|
throw new ArgumentNullException(nameof(entities));
|
|
Repository.AddRange(entities);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernt eine Entität aus dem Repository
|
|
/// </summary>
|
|
/// <param name="entity">Zu entfernende Entität</param>
|
|
public virtual void Remove(T entity)
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException(nameof(entity));
|
|
Repository.Remove(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernt eine Liste von Entitäten aus dem Repository
|
|
/// </summary>
|
|
/// <param name="entities">Liste von Entitäten</param>
|
|
public virtual void RemoveRange(IEnumerable<T> entities)
|
|
{
|
|
if (entities == null)
|
|
throw new ArgumentNullException(nameof(entities));
|
|
Repository.RemoveRange(entities);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Count
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl aller Entitäten zurück
|
|
/// </summary>
|
|
/// <returns>Anzahl</returns>
|
|
public virtual int Count()
|
|
{
|
|
return Repository.Count();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl aller Entitäten zurück
|
|
/// </summary>
|
|
/// <returns>Anzahl</returns>
|
|
public virtual async Task<int> CountAsync()
|
|
{
|
|
return await Repository.CountAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Commit / UnitOfWork
|
|
|
|
/// <summary>
|
|
/// Schließt Daten-Änderungen ab und speichert die selben
|
|
/// </summary>
|
|
/// <param name="userName">Benutzername</param>
|
|
/// <param name="noAudit">Gibt an, dass kein Audit-Eintrag erstellt werden soll, auch wenn Audit aktiviert ist</param>
|
|
[ExcludeFromCodeCoverage]
|
|
public int Commit(string userName, bool noAudit = false)
|
|
{
|
|
return _unitOfWork.Commit(userName, noAudit);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Schließt Daten-Änderungen ab und speichert die selben
|
|
/// </summary>
|
|
/// <param name="userName">Benutzername</param>
|
|
/// <param name="noAudit">Gibt an, dass kein Audit-Eintrag erstellt werden soll, auch wenn Audit aktiviert ist</param>
|
|
[ExcludeFromCodeCoverage]
|
|
public async Task<int> CommitAsync(string userName, bool noAudit = false)
|
|
{
|
|
return await _unitOfWork.CommitAsync(userName, noAudit);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
|
|
/// <summary>
|
|
/// Dispose
|
|
/// </summary>
|
|
/// <param name="disposing"></param>
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!this._disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_unitOfWork.Dispose();
|
|
}
|
|
}
|
|
this._disposed = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|