74 lines
2.0 KiB
C#
74 lines
2.0 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 T CreateNonAsync(T entity)
|
|
{
|
|
return _nonQueryDataService.CreateNonAsync(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);
|
|
}
|
|
|
|
T IDataService<T>.CreateNonAsync(T entity)
|
|
{
|
|
return _nonQueryDataService.CreateNonAsync(entity);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//764 / 21
|