Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/Generator.Domain/DealDomain.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
using Generator.DomainApi.Port;
using Generator.Persistence.Adapter.Context;
using Microsoft.EntityFrameworkCore;
using Generator.DomainApi.Model;
using System.Collections.Generic;
using System.Linq;

namespace Generator.Domain
{
public class DealDomain<T> : IRequestDeal<T> where T : class
public class DealDomain<T> : IRequestDeal<T> where T : Deal
{
private readonly DbSet<T> table;
private readonly IDealSet table;

public DealDomain(ApplicationDbContext dbContext)
public DealDomain(IApplicationDbContext dbContext)
{
ApplicationDbContext _dbContext;
IApplicationDbContext _dbContext;
_dbContext = dbContext;
table = _dbContext.Set<T>();
table = _dbContext.Deals;
}
public T GetDeal(int id)
{
return table.Find(id);
return table.Find<T>(id);
}

public List<T> GetDeals()
{
return table.ToList();
return table.ToList<T>();
}
}
}
19 changes: 9 additions & 10 deletions src/Generator.Domain/Generator.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Generator.DomainApi\Generator.DomainApi.csproj" />
<ProjectReference Include="..\Generator.Persistence.Adapter\Generator.Persistence.Adapter.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Generator.DomainApi\Generator.DomainApi.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Generator.DomainApi/Port/IApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Generator.DomainApi.Port
{
public interface IApplicationDbContext
{
public IDealSet Deals { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Generator.DomainApi/Port/IDealSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Generator.DomainApi.Model;

namespace Generator.DomainApi.Port
{
public interface IDealSet
{
void AddRange(List<Deal> getDeals);
void Add(Deal deal);
void Remove(Deal deal);
T Find<T>(int id) where T : Deal;
List<T> ToList<T>() where T : Deal;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Generator.DomainApi.Model;
using Generator.Persistence.Adapter.UnitTest.Common;
using Generator.Persistence.Adapter.Common;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Generator.DomainApi.Model;
using System;
using System.Collections.Generic;
using Generator.DomainApi.Model;
using Generator.Persistence.Adapter.Context;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

namespace Generator.Persistence.Adapter.UnitTest.Common
namespace Generator.Persistence.Adapter.Common
{
public static class ApplicationDbContextFactory
{
Expand Down
14 changes: 12 additions & 2 deletions src/Generator.Persistence.Adapter/Context/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
using Generator.DomainApi.Model;
using Generator.DomainApi.Port;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;

namespace Generator.Persistence.Adapter.Context
{
public class ApplicationDbContext : DbContext
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
public ApplicationDbContext()
{
}

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

public DbSet<Deal> Deals { get; set; }
public DbSet<Deal> DbDeals { get; set; }

private IDealSet _deals;

public IDealSet Deals
{
get { return _deals ??= new DealSet(Set<Deal>()); }
set => _deals = value;
}

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
Expand Down
43 changes: 43 additions & 0 deletions src/Generator.Persistence.Adapter/Context/DealSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using Generator.DomainApi.Model;
using Generator.DomainApi.Port;
using Microsoft.EntityFrameworkCore;

namespace Generator.Persistence.Adapter.Context
{
public class DealSet: IDealSet
{
private readonly DbSet<Deal> _dealDbSet;

public DealSet(DbSet<Deal> dealDbSet)
{
_dealDbSet = dealDbSet;
}

public void AddRange(List<Deal> deals)
{
_dealDbSet.AddRange(deals);
}

public void Add(Deal deal)
{
_dealDbSet.Add(deal);
}

public void Remove(Deal deal)
{
_dealDbSet.Remove(deal);
}

public T Find<T>(int id) where T : Deal
{
return _dealDbSet.Find(id) as T;
}

public List<T> ToList<T>() where T : Deal
{
return _dealDbSet.ToList() as List<T>;
}
}
}
5 changes: 4 additions & 1 deletion src/Generator.Persistence.Adapter/PersistenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Generator.Persistence.Adapter.Context;
using Generator.DomainApi.Port;
using Generator.Persistence.Adapter.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -10,6 +11,8 @@ public static void AddPersistence(this IServiceCollection serviceCollection)
{
serviceCollection.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("HexaArchConnInMemoryDb"));

serviceCollection.AddScoped<IApplicationDbContext, ApplicationDbContext>();
}
}
}
14 changes: 14 additions & 0 deletions src/Generator/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Generator.DomainApi.Services;
using Generator.Extension;
using Generator.Persistence.Adapter;
using Generator.Persistence.Adapter.Common;
using Generator.Persistence.Adapter.Context;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -46,6 +48,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
SeedTestingData(app);
}

app.UseHttpsRedirection();
Expand All @@ -67,5 +70,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
endpoints.MapControllers();
});
}

private void SeedTestingData(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();

context.Deals.AddRange(ApplicationDbContextFactory.GetDeals());
context.SaveChanges();
}
}
}
}