Add project files.

This commit is contained in:
2022-05-26 15:19:31 +02:00
parent 44afa68e45
commit a57dcfe03b
57 changed files with 1907 additions and 0 deletions

View File

@ -0,0 +1,73 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using OemanTrader.Domain.Models;
using OemanTrader.Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OemanTrader.EntityFramework.Services
{
public class GenericDataService<T> : IDataService<T> where T : DomainObject
{
private readonly OemanTraderDbContextFactory _contextFactory;
public GenericDataService(OemanTraderDbContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public async Task<T> Create(T entity)
{
using (var context = _contextFactory.CreateDbContext())
{
EntityEntry<T> createdResult = await context.Set<T>().AddAsync(entity);
await context.SaveChangesAsync();
return createdResult.Entity;
}
}
public async Task<bool> Delete(int id)
{
using (var context = _contextFactory.CreateDbContext())
{
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
context.Set<T>().Remove(entity);
await context.SaveChangesAsync();
return true;
}
}
public async Task<T> Get(int id)
{
using (var context = _contextFactory.CreateDbContext())
{
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
return entity;
}
}
public async Task<IEnumerable<T>> GetAll()
{
using (var context = _contextFactory.CreateDbContext())
{
IEnumerable<T> entities = await context.Set<T>().ToListAsync();
return entities;
}
}
public async Task<T> Update(int Id, T entity)
{
using (var context = _contextFactory.CreateDbContext())
{
entity.Id = Id;
context.Set<T>().Update(entity);
await context.SaveChangesAsync();
return entity;
}
}
}
}