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 : IDataService where T : DomainObject { private readonly DaSaSoDbContextFactory _contextFactory; private readonly NonQueryDataService _nonQueryDataService; public GenericDataService(DaSaSoDbContextFactory contextFactory) { this._contextFactory = contextFactory; _nonQueryDataService = new NonQueryDataService(contextFactory); } public async Task Create(T entity) { return await _nonQueryDataService.Create(entity); } public T CreateNonAsync(T entity) { return _nonQueryDataService.CreateNonAsync(entity); } public async Task Delete(int id) { return await _nonQueryDataService.Delete(id); } public async Task Get(int id) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { T entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); return entity; } } public async Task> GetAll() { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { IEnumerable entities = await context.Set().ToListAsync(); return entities; } } public async Task Update(int id, T entity) { return await _nonQueryDataService.Update(id, entity); } T IDataService.CreateNonAsync(T entity) { return _nonQueryDataService.CreateNonAsync(entity); } } } //764 / 21