Refactoring durchgeführt

This commit is contained in:
HuskyTeufel
2021-09-14 17:08:06 +02:00
parent 6b2ed0d5ab
commit 8eccf7c478
24 changed files with 430 additions and 57 deletions

View File

@@ -1,5 +1,6 @@
using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services;
using DaSaSo.EntityFramework.Services.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
@@ -13,31 +14,22 @@ namespace DaSaSo.EntityFramework.Services
public class GenericDataService<T> : IDataService<T> where T : DomainObject
{
private readonly DaSaSoDbContextFactory _contextFactory;
private readonly NonQueryDataService<T> _nonQueryDataService;
public GenericDataService(DaSaSoDbContextFactory contextFactory)
{
this._contextFactory = contextFactory;
_nonQueryDataService = new NonQueryDataService<T>(contextFactory);
}
public async Task<T> Create(T entity)
{
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
{
EntityEntry<T> createdEntity = await context.Set<T>().AddAsync(entity);
await context.SaveChangesAsync();
return createdEntity.Entity;
}
return await _nonQueryDataService.Create(entity);
}
public async Task<bool> Delete(int id)
{
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
{
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
context.Set<T>().Remove(entity);
await context.SaveChangesAsync();
return true;
}
return await _nonQueryDataService.Delete(id);
}
public async Task<T> Get(int id)
@@ -62,13 +54,7 @@ namespace DaSaSo.EntityFramework.Services
public async Task<T> Update(int id, T entity)
{
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
{
entity.Id = id;
context.Set<T>().Update(entity);
await context.SaveChangesAsync();
return entity;
}
return await _nonQueryDataService.Update(id, entity);
}
}
}