Implement N-layer architecture

This commit is contained in:
Stanislav Mykhailenko 2023-05-16 22:09:57 +03:00
parent 6dfa6908b1
commit 16ef307140
GPG key ID: 1E95E66A9C9D6A36
96 changed files with 132 additions and 28 deletions

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../NG_2023_Kanban.DataLayer/NG_2023_Kanban.DataLayer.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,33 @@
using NG_2023_Kanban.DataLayer.Service;
using NG_2023_Kanban.DataLayer.Entities;
using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.BusinessLayer.Service
{
public class BusinessService
{
private readonly DataService _service;
public BusinessService(DataService service)
{
_service = service;
}
public async Task<User> LoginAsync(string username, string password)
{
return await _service.LoginAsync(username, password);
}
public async Task<User> RegisterAsync(string fullName, string username, string password)
{
User account = await _service.AddAsync(new User
{
FullName = fullName,
Username = username,
Password = password, // TODO: hashing
IsAdmin = false
});
return account;
}
}
}

View file

@ -0,0 +1,14 @@
using NG_2023_Kanban.BusinessLayer.Service;
using Microsoft.Extensions.DependencyInjection;
namespace NG_2023_Kanban.BusinessLayer.Inject
{
public static class Injecting
{
public static void InjectBLL(
this IServiceCollection services)
{
services.AddScoped<BusinessService>();
}
}
}

View file

@ -1,11 +1,13 @@
using NG_2023_Kanban.BusinessLayer.Service; using NG_2023_Kanban.DataLayer.Service;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace NG_2023_Kanban.DataLayer.DbStartup namespace NG_2023_Kanban.DataLayer.DbStartup
{ {
public static class Injecting public static class Injecting
{ {
public static void Inject( public static void InjectDAL(
this IServiceCollection services, this IServiceCollection services,
IConfiguration configuration) IConfiguration configuration)
{ {
@ -14,7 +16,7 @@ namespace NG_2023_Kanban.DataLayer.DbStartup
options.UseSqlServer( options.UseSqlServer(
configuration["ConnectionString"]); configuration["ConnectionString"]);
}); });
services.AddScoped<DbService>(); services.AddScoped<DataService>();
} }
} }
} }

View file

@ -18,7 +18,8 @@ namespace NG_2023_Kanban.DataLayer.EntityConfiguration
.HasOne(x => x.Sender) .HasOne(x => x.Sender)
.WithMany(x => x.Comments) .WithMany(x => x.Comments)
.HasForeignKey(x => x.SenderId) .HasForeignKey(x => x.SenderId)
.HasPrincipalKey(x => x.Id); .HasPrincipalKey(x => x.Id)
.OnDelete(DeleteBehavior.Restrict);
builder builder
.HasOne(x => x.Card) .HasOne(x => x.Card)

View file

@ -1,15 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -2,14 +2,15 @@
using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace NG_2023_Kanban.BusinessLayer.Service namespace NG_2023_Kanban.DataLayer.Service
{ {
public class DbService public class DataService
{ {
private readonly DatabaseContext _context; private readonly DatabaseContext _context;
public DbService(DatabaseContext context) public DataService(DatabaseContext context)
{ {
_context = context; _context = context;
_context.Database.EnsureCreated();
} }
public async Task<User> AddAsync(User entity) public async Task<User> AddAsync(User entity)

34
NG_2023_Kanban.sln Normal file
View file

@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NG_2023_Kanban", "NG_2023_Kanban\NG_2023_Kanban.csproj", "{9487AF5A-D980-4015-B8EE-BF243BC0534E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NG_2023_Kanban.BusinessLayer", "NG_2023_Kanban.DataLayer\NG_2023_Kanban.BusinessLayer.csproj", "{B62FD923-EDD7-4A4E-AD73-408C2FD0AC2B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NG_2023_Kanban.BusinessLayer", "NG_2023_Kanban.BusinessLayer\NG_2023_Kanban.BusinessLayer.csproj", "{169BEBE0-05F8-463D-999E-323F37454289}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9487AF5A-D980-4015-B8EE-BF243BC0534E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9487AF5A-D980-4015-B8EE-BF243BC0534E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9487AF5A-D980-4015-B8EE-BF243BC0534E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9487AF5A-D980-4015-B8EE-BF243BC0534E}.Release|Any CPU.Build.0 = Release|Any CPU
{B62FD923-EDD7-4A4E-AD73-408C2FD0AC2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B62FD923-EDD7-4A4E-AD73-408C2FD0AC2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B62FD923-EDD7-4A4E-AD73-408C2FD0AC2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B62FD923-EDD7-4A4E-AD73-408C2FD0AC2B}.Release|Any CPU.Build.0 = Release|Any CPU
{169BEBE0-05F8-463D-999E-323F37454289}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{169BEBE0-05F8-463D-999E-323F37454289}.Debug|Any CPU.Build.0 = Debug|Any CPU
{169BEBE0-05F8-463D-999E-323F37454289}.Release|Any CPU.ActiveCfg = Release|Any CPU
{169BEBE0-05F8-463D-999E-323F37454289}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -5,14 +5,14 @@ using NG_2023_Kanban.Extensions;
using NG_2023_Kanban.DataLayer.Models; using NG_2023_Kanban.DataLayer.Models;
using NG_2023_Kanban.BusinessLayer.Service; using NG_2023_Kanban.BusinessLayer.Service;
namespace NG_2023_Kanban.PresentationLayer.Controllers; namespace NG_2023_Kanban.Controllers;
public class HomeController : Controller public class HomeController : Controller
{ {
private readonly DbService _service; private readonly BusinessService _service;
private readonly ILogger<HomeController> _logger; private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger, DbService service) public HomeController(ILogger<HomeController> logger, BusinessService service)
{ {
_logger = logger; _logger = logger;
_service = service; _service = service;
@ -82,13 +82,7 @@ public class HomeController : Controller
try try
{ {
User account = await _service.AddAsync(new User User account = await _service.RegisterAsync(fullName, username, password);
{
FullName = fullName,
Username = username,
Password = password, // TODO: hashing
IsAdmin = false
});
HttpContext.Session.SetObject("Account", account); HttpContext.Session.SetObject("Account", account);
return Redirect("/Home/Index"); return Redirect("/Home/Index");

View file

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../NG_2023_Kanban.DataLayer/NG_2023_Kanban.DataLayer.csproj" />
<ProjectReference Include="../NG_2023_Kanban.BusinessLayer/NG_2023_Kanban.BusinessLayer.csproj" />
</ItemGroup>
</Project>

View file

@ -1,10 +1,12 @@
using NG_2023_Kanban.BusinessLayer.Inject;
using NG_2023_Kanban.DataLayer.DbStartup; using NG_2023_Kanban.DataLayer.DbStartup;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = builder.Configuration; IConfiguration configuration = builder.Configuration;
builder.Services.Inject(configuration); builder.Services.InjectDAL(configuration);
builder.Services.InjectBLL();
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
@ -27,13 +29,6 @@ if (!app.Environment.IsDevelopment())
app.UseHsts(); app.UseHsts();
} }
using(var scope = app.Services.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var context = serviceProvider.GetRequiredService<DatabaseContext>();
DbInitializer.Initialize(context);
}
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();

View file

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB