Files
DaSaSo/DaSaSo.EntityFramework/Services/GenericDataService.cs
2021-09-14 17:08:06 +02:00

64 lines
1.8 KiB
C#

using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services;
using DaSaSo.EntityFramework.Services.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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)
{
return await _nonQueryDataService.Create(entity);
}
public async Task<bool> Delete(int id)
{
return await _nonQueryDataService.Delete(id);
}
public async Task<T> Get(int id)
{
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
{
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
return entity;
}
}
public async Task<IEnumerable<T>> GetAll()
{
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
{
IEnumerable<T> entities = await context.Set<T>().ToListAsync();
return entities;
}
}
public async Task<T> Update(int id, T entity)
{
return await _nonQueryDataService.Update(id, entity);
}
}
}
//764 / 21