Archetektur angefangen
This commit is contained in:
17
DataStoring.EF/DataStoring.EF.csproj
Normal file
17
DataStoring.EF/DataStoring.EF.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DataStoring.Contract\DataStoring.Contract.csproj" />
|
||||
<ProjectReference Include="..\KanSan.DataClasses\KanSan.DataClasses.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
31
DataStoring.EF/KanSanContext.cs
Normal file
31
DataStoring.EF/KanSanContext.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using KanSan.CrossCutting.DataClasses;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DataStoring.EF
|
||||
{
|
||||
public class KanSanContext : DbContext
|
||||
{
|
||||
public DbSet<Projekt> Projekte { get; set; }
|
||||
public DbSet<Kunde> Kunden { get; set; }
|
||||
public DbSet<Sewer> Kanaele { get; set; }
|
||||
public DbSet<SewerPoint> SewerPoints { get; set; }
|
||||
public DbSet<Schaeden> Schaeden { get; set; }
|
||||
public DbSet<Sanierungskonzept> Sanierungskonzept { get; set; }
|
||||
public DbSet<Taetigkeiten> Taetigkeiten { get; set; }
|
||||
public DbSet<LeistungsverzeichnisPosition> LeistungsverzeichnisPositionen { get; set; }
|
||||
public DbSet<BaustelleLeistungsverzeichnisReferenz> LeistungsverzeichnisBaustellen { get; set; }
|
||||
public DbSet<Fahrzeug> Fahrzeuge { get; set; }
|
||||
public DbSet<KurzlinerSan> KurzlinerSanierung { get; set; }
|
||||
public DbSet<HutprofilSan> HutprofilSanierung { get; set; }
|
||||
|
||||
public KanSanContext()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseNpgsql("Host = 192.168.122.1; Database = kanSan; Username = husky; Password = bodde05");
|
||||
}
|
||||
}
|
||||
}
|
||||
111
DataStoring.EF/Repository.cs
Normal file
111
DataStoring.EF/Repository.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using KanSan.DataStoring.Contract;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace DataStoring.EF
|
||||
{
|
||||
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
|
||||
{
|
||||
private readonly KanSanContext _db;
|
||||
private readonly DbSet<TEntity> dbSet;
|
||||
|
||||
public IQueryable<TEntity> Query => dbSet;
|
||||
|
||||
public Repository(KanSanContext db)
|
||||
{
|
||||
_db = db;
|
||||
this.dbSet = db.Set<TEntity>();
|
||||
}
|
||||
public virtual void Delete(TEntity entityToDelete)
|
||||
{
|
||||
if (_db.Entry(entityToDelete).State == EntityState.Detached)
|
||||
dbSet.Attach(entityToDelete);
|
||||
dbSet.Remove(entityToDelete);
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var entity = _db.Set<TEntity>().Find(id);
|
||||
if(entity == null)
|
||||
{
|
||||
throw new Exception("Id not found");
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new Exception("Cant delete id");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> GetAll(string include)
|
||||
{
|
||||
return dbSet.Include(include);
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = dbSet;
|
||||
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
query = query.Where(filter);
|
||||
}
|
||||
|
||||
if (includeProperties != null)
|
||||
{
|
||||
foreach (var includeProperty in includeProperties.Split
|
||||
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
query = query.Include(includeProperty);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (orderBy != null)
|
||||
{
|
||||
return orderBy(query).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
return query.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public TEntity GetByID(object id)
|
||||
{
|
||||
return dbSet.Find(id);
|
||||
}
|
||||
|
||||
public virtual void Insert(TEntity entity)
|
||||
{
|
||||
_db.Set<TEntity>().Add(entity);
|
||||
_db.SaveChanges();
|
||||
|
||||
}
|
||||
|
||||
public IQueryable<TEntity> Include(params Expression<Func<TEntity, object>>[] includeExpressions)
|
||||
{
|
||||
IQueryable<TEntity> query = null;
|
||||
foreach (var include in includeExpressions)
|
||||
{
|
||||
query = dbSet.Include(include);
|
||||
}
|
||||
return query ?? dbSet;
|
||||
}
|
||||
|
||||
public void Update(TEntity entity)
|
||||
{
|
||||
_db.Entry(entity).State = EntityState.Modified;
|
||||
_db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user