Entityframework hinzugefügt
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Shared.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.EntityFramework.Services.Common
|
||||
{
|
||||
class NonQueryDataService<T> where T: DBObject
|
||||
{
|
||||
private readonly SewerStammGenDbContextFactory _contextFactory;
|
||||
|
||||
public NonQueryDataService(SewerStammGenDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
public async Task<T> Create(T entity)
|
||||
{
|
||||
using SewerStammGenDbContext context = _contextFactory.CreateDbContext();
|
||||
EntityEntry<T> createdEntity = await context.Set<T>().AddAsync(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return createdEntity.Entity;
|
||||
}
|
||||
|
||||
public T CreateNonAsync(T entity)
|
||||
{
|
||||
using SewerStammGenDbContext context = _contextFactory.CreateDbContext();
|
||||
EntityEntry<T> createdEntity = context.Set<T>().Add(entity);
|
||||
context.SaveChanges();
|
||||
return createdEntity.Entity;
|
||||
}
|
||||
|
||||
public async Task<bool> Delete(int id)
|
||||
{
|
||||
using SewerStammGenDbContext 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> Update(int id, T entity)
|
||||
{
|
||||
using SewerStammGenDbContext context = _contextFactory.CreateDbContext();
|
||||
entity.Id = id;
|
||||
context.Set<T>().Update(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
67
SewerStammGen.EntityFramework/Services/GenericDataService.cs
Normal file
67
SewerStammGen.EntityFramework/Services/GenericDataService.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SewerStammGen.EntityFramework.Services.Common;
|
||||
using Shared.Contracts;
|
||||
using Shared.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.EntityFramework.Services
|
||||
{
|
||||
public class GenericDataService<T> : IDataService<T> where T : DBObject
|
||||
{
|
||||
private readonly SewerStammGenDbContextFactory _contextFactory;
|
||||
private readonly NonQueryDataService<T> _nonQueryDataService;
|
||||
|
||||
public GenericDataService(SewerStammGenDbContextFactory contextFactory)
|
||||
{
|
||||
_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(SewerStammGenDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetAll()
|
||||
{
|
||||
using (SewerStammGenDbContext 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user