diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdca8dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.vs/* +/*/obj/* diff --git a/DaSaSo.Domain/DaSaSo.Domain.csproj b/DaSaSo.Domain/DaSaSo.Domain.csproj new file mode 100644 index 0000000..4de8048 --- /dev/null +++ b/DaSaSo.Domain/DaSaSo.Domain.csproj @@ -0,0 +1,8 @@ + + + + net6.0 + enable + + + diff --git a/DaSaSo.Domain/Model/Client.cs b/DaSaSo.Domain/Model/Client.cs new file mode 100644 index 0000000..7790099 --- /dev/null +++ b/DaSaSo.Domain/Model/Client.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.Domain.Model +{ + public class Client : DomainObject + { + public string Firstname { get; set; } + public string LastName { get; set; } + } +} diff --git a/DaSaSo.Domain/Model/DomainObject.cs b/DaSaSo.Domain/Model/DomainObject.cs new file mode 100644 index 0000000..492f9f4 --- /dev/null +++ b/DaSaSo.Domain/Model/DomainObject.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.Domain.Model +{ + public class DomainObject + { + public int Id { get; set; } + } +} diff --git a/DaSaSo.Domain/Model/Project.cs b/DaSaSo.Domain/Model/Project.cs new file mode 100644 index 0000000..2c6451c --- /dev/null +++ b/DaSaSo.Domain/Model/Project.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.Domain.Model +{ + public class Project : DomainObject + { + public string Name { get; set; } + public Client Client { get; set; } + } +} diff --git a/DaSaSo.Domain/Services/IDataService.cs b/DaSaSo.Domain/Services/IDataService.cs new file mode 100644 index 0000000..aacb14f --- /dev/null +++ b/DaSaSo.Domain/Services/IDataService.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.Domain.Service +{ + public interface IDataService + { + Task> GetAll(); + Task Get(int id); + Task Create(T entity); + Task Update(int id, T entity); + Task Delete(int id); + } +} diff --git a/DaSaSo.EntityFramework/DaSaSo.EntityFramework.csproj b/DaSaSo.EntityFramework/DaSaSo.EntityFramework.csproj new file mode 100644 index 0000000..4de8048 --- /dev/null +++ b/DaSaSo.EntityFramework/DaSaSo.EntityFramework.csproj @@ -0,0 +1,8 @@ + + + + net6.0 + enable + + + diff --git a/DaSaSo.EntityFramework/DaSaSoDbContext.cs b/DaSaSo.EntityFramework/DaSaSoDbContext.cs new file mode 100644 index 0000000..04141dc --- /dev/null +++ b/DaSaSo.EntityFramework/DaSaSoDbContext.cs @@ -0,0 +1,19 @@ +using DaSaSo.Domain.Model; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.EntityFramework +{ + public class DaSaSoDbContext : DbContext + { + public DaSaSoDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet Clients { get; set; } + } +} diff --git a/DaSaSo.EntityFramework/DaSaSoDbContextFactory.cs b/DaSaSo.EntityFramework/DaSaSoDbContextFactory.cs new file mode 100644 index 0000000..f7d13bb --- /dev/null +++ b/DaSaSo.EntityFramework/DaSaSoDbContextFactory.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.EntityFramework +{ + public class DaSaSoDbContextFactory : IDesignTimeDbContextFactory + { + public DaSaSoDbContext CreateDbContext(string[] args) + { + var options = new DbContextOptionsBuilder(); + options.UseNpgsql("server = localhost"); + return new DaSaSoDbContext(options.Options); + } + } +} diff --git a/DaSaSo.EntityFramework/Services/GenericDataService.cs b/DaSaSo.EntityFramework/Services/GenericDataService.cs new file mode 100644 index 0000000..8aa69f6 --- /dev/null +++ b/DaSaSo.EntityFramework/Services/GenericDataService.cs @@ -0,0 +1,74 @@ +using DaSaSo.Domain.Model; +using DaSaSo.Domain.Service; +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; + + public GenericDataService(DaSaSoDbContextFactory contextFactory) + { + this._contextFactory = contextFactory; + } + + public async Task Create(T entity) + { + using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) + { + EntityEntry createdEntity = await context.Set().AddAsync(entity); + await context.SaveChangesAsync(); + return createdEntity.Entity; + } + } + + public async Task Delete(int id) + { + using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) + { + T entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); + context.Set().Remove(entity); + await context.SaveChangesAsync(); + return true; + } + } + + 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) + { + using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) + { + entity.Id = id; + await context.Set().Update(entity); + + return entity; + } + } + } +} diff --git a/DaSaSo.sln b/DaSaSo.sln new file mode 100644 index 0000000..cae29bb --- /dev/null +++ b/DaSaSo.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31612.314 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DaSaSo.Domain", "DaSaSo.Domain\DaSaSo.Domain.csproj", "{9434EB7A-C6EE-46C7-8306-BE806375E852}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DaSaSo.EntityFramework", "DaSaSo.EntityFramework\DaSaSo.EntityFramework.csproj", "{E75F15AE-D49B-4768-A5EA-DC05AB60A898}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9434EB7A-C6EE-46C7-8306-BE806375E852}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9434EB7A-C6EE-46C7-8306-BE806375E852}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9434EB7A-C6EE-46C7-8306-BE806375E852}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9434EB7A-C6EE-46C7-8306-BE806375E852}.Release|Any CPU.Build.0 = Release|Any CPU + {E75F15AE-D49B-4768-A5EA-DC05AB60A898}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E75F15AE-D49B-4768-A5EA-DC05AB60A898}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E75F15AE-D49B-4768-A5EA-DC05AB60A898}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E75F15AE-D49B-4768-A5EA-DC05AB60A898}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0C7F29DD-63BA-4EC1-80F3-0688F866C9B6} + EndGlobalSection +EndGlobal