Refactoring durchgeführt
This commit is contained in:
@@ -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 ClientDataService : IDataService<Client>
|
||||
{
|
||||
private readonly DaSaSoDbContextFactory _contextFactory;
|
||||
private readonly NonQueryDataService<Client> _nonQueryDataService;
|
||||
|
||||
public ClientDataService(DaSaSoDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
_nonQueryDataService = new NonQueryDataService<Client>(contextFactory);
|
||||
}
|
||||
|
||||
public async Task<Client> Create(Client entity)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
EntityEntry<Client> createdResult = await context.Set<Client>().AddAsync(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return createdResult.Entity;
|
||||
}
|
||||
return await _nonQueryDataService.Create(entity);
|
||||
}
|
||||
|
||||
public async Task<bool> Delete(int id)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
Client entity = await context.Set<Client>().FirstOrDefaultAsync((e) => e.Id == id);
|
||||
context.Set<Client>().Remove(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
return await _nonQueryDataService.Delete(id);
|
||||
}
|
||||
|
||||
public async Task<Client> Get(int id)
|
||||
@@ -54,7 +46,7 @@ namespace DaSaSo.EntityFramework.Services
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
IEnumerable<Client> entities = await context.Set<Client>().ToListAsync();
|
||||
IEnumerable<Client> entities = await context.Clients.ToListAsync();
|
||||
|
||||
return entities;
|
||||
}
|
||||
@@ -62,13 +54,7 @@ namespace DaSaSo.EntityFramework.Services
|
||||
|
||||
public async Task<Client> Update(int id, Client entity)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
entity.Id = id;
|
||||
context.Set<Client>().Update(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
return await _nonQueryDataService.Update(id, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user