Skip to content

Commit

Permalink
Wireframe of hierarchy of services and classes
Browse files Browse the repository at this point in the history
and how to use them in DI
  • Loading branch information
jokerosky committed Nov 23, 2017
1 parent 317e1cb commit b966c52
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 27 deletions.
14 changes: 7 additions & 7 deletions N2N.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N2N.Infrastructure.Tests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N2N.Core", "src\N2N.Core\N2N.Core.csproj", "{94757EF4-4F40-4FCA-B64F-95AB5942B0D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N2N.Data", "src\N2N.Data\N2N.Data.csproj", "{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N2N.Infrasturcture", "src\N2N.Infrastructure\N2N.Infrasturcture.csproj", "{E45EAE66-10ED-4179-805A-F6A5B65F6532}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "N2N.Web", "src\N2N.Web\N2N.Web.csproj", "{8B788DD5-8887-4463-AD79-18FEA9E27272}"
Expand All @@ -32,6 +30,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{1567A8
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N2N.Utilities", "src\N2N.Utilities\N2N.Utilities.csproj", "{611CDBEB-2465-4BC4-B249-3A4ABC93E62A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "N2N.Services", "src\N2N.Services\N2N.Services.csproj", "{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -50,10 +50,6 @@ Global
{94757EF4-4F40-4FCA-B64F-95AB5942B0D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94757EF4-4F40-4FCA-B64F-95AB5942B0D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94757EF4-4F40-4FCA-B64F-95AB5942B0D6}.Release|Any CPU.Build.0 = Release|Any CPU
{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63}.Release|Any CPU.Build.0 = Release|Any CPU
{E45EAE66-10ED-4179-805A-F6A5B65F6532}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E45EAE66-10ED-4179-805A-F6A5B65F6532}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E45EAE66-10ED-4179-805A-F6A5B65F6532}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -66,6 +62,10 @@ Global
{611CDBEB-2465-4BC4-B249-3A4ABC93E62A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{611CDBEB-2465-4BC4-B249-3A4ABC93E62A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{611CDBEB-2465-4BC4-B249-3A4ABC93E62A}.Release|Any CPU.Build.0 = Release|Any CPU
{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -74,10 +74,10 @@ Global
{52F13DD7-82C4-4C44-8023-7F6D7B0AD8EB} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
{AE7BBF0E-58EF-4076-A191-2F4229D0D53D} = {7F19DC56-9B6E-4B4F-909C-20B87759A4BA}
{94757EF4-4F40-4FCA-B64F-95AB5942B0D6} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
{E45EAE66-10ED-4179-805A-F6A5B65F6532} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
{8B788DD5-8887-4463-AD79-18FEA9E27272} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
{611CDBEB-2465-4BC4-B249-3A4ABC93E62A} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C} = {E17A8916-7BC2-4F80-9198-1CA6744EBB3C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EEA9FF11-6FF0-4EF2-A7AF-699EDC60DA0E}
Expand Down
17 changes: 10 additions & 7 deletions src/N2N.Api/Configuration/AppStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using N2N.Data.Repositories;
using N2N.Infrastructure.DataContext;
using N2N.Infrastructure.Models;
using N2N.Services;
using N2N.Services.Users;
using SimpleInjector;
using SimpleInjector.Integration.AspNetCore.Mvc;
using SimpleInjector.Lifestyles;
Expand Down Expand Up @@ -63,16 +65,17 @@ internal static void InitializeContainer(IApplicationBuilder app, Container cont
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);

container.CrossWire<N2NDataContext>(app);
container.Register<IRepository<N2NUser>, DbRepository<N2NUser>>();
container.Register<N2N.Data.Repositories.TestClass>();
// Add application services for AccountController
//container.RegisterSingleton<IEmailSender, AuthMessageSender>();
//container.RegisterSingleton<ISmsSender, AuthMessageSender>();

// Cross wire Identity services
container.CrossWire<N2NDataContext>(app);
container.CrossWire<UserManager<N2NIdentityUser>>(app);
container.CrossWire<SignInManager<N2NIdentityUser>>(app);

// Dependencies
container.Register<IRepository<N2NUser>, DbRepository<N2NUser>>();
container.Register<ISecurityService, SecurityService>();
container.Register<IN2NUserService, N2NUserService>();
container.Register<N2N.Data.Repositories.TestClass>();

}

internal static bool BootstrapDb(N2NDataContext ctx)
Expand Down
47 changes: 47 additions & 0 deletions src/N2N.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using N2N.Api.Services;
using N2N.Core.Entities;
using N2N.Infrastructure.Models;
using N2N.Services.Users;

namespace N2N.Api.Controllers
{
[Produces("application/json")]
[Route("User")]
public class UserController : Controller
{
private N2NApiUserService _apiUserService;

public UserController(N2NApiUserService apiUserService)
{
this._apiUserService = apiUserService;
}

// GET: api/User
[HttpGet]
public async Task<IActionResult> Get()
{
var result = await this._apiUserService.CreateUserAsync(new N2NUser()
{
NickName = "Test",
Email = "[email protected]"
},
"Password"
);

if (!result.Success)
{
return BadRequest( "Baaad");
}

return Ok();
}

}
}
7 changes: 2 additions & 5 deletions src/N2N.Api/N2N.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@

<ItemGroup>
<ProjectReference Include="..\N2N.Core\N2N.Core.csproj" />
<ProjectReference Include="..\N2N.Data\N2N.Data.csproj" />
<ProjectReference Include="..\N2N.Infrastructure\N2N.Infrasturcture.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Services\" />
<ProjectReference Include="..\N2N.Services\N2N.Services.csproj" />
<ProjectReference Include="..\N2N.Utilities\N2N.Utilities.csproj" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions src/N2N.Api/Services/N2NApiUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using N2N.Core.Entities;
using N2N.Infrastructure.Models;
using N2N.Services.Users;

namespace N2N.Api.Services
{
public class N2NApiUserService
{
private IN2NUserService _userservice;
private UserManager<N2NIdentityUser> _userManager;

public N2NApiUserService(IN2NUserService userService, UserManager<N2NIdentityUser> userManager)
{
this._userservice = userService;
this._userManager = userManager;
}

public async Task<OperationResult> CreateUserAsync(N2NUser user, string password)
{
// this all should be a transaction ↓

var result = this._userservice.CreateUser(user);
var identityResult = await this._userManager.CreateAsync(new N2NIdentityUser()
{
N2NUserId = user.Id
}, password);

//if operation fails, we should delete N2NUser

return result;
}
}
}
1 change: 1 addition & 0 deletions src/N2N.Infrastructure/Models/N2NIdentityUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace N2N.Infrastructure.Models
{
public class N2NIdentityUser : IdentityUser
{
public Guid N2NUserId { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/N2N.Infrastructure/Models/OperationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class OperationResult
{
public bool Success { get; set; }
public object Data { get; set; }
public List<string> Messages { get; set; }
public IEnumerable<string> Messages { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/N2N.Services/IN2NUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using N2N.Core.Entities;
using N2N.Infrastructure.Models;

namespace N2N.Services.Users
{
public interface IN2NUserService
{
OperationResult CreateUser(N2NUser user);
}
}
15 changes: 15 additions & 0 deletions src/N2N.Services/ISecurityService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace N2N.Services
{
public interface ISecurityService
{
bool HasAccess();
bool HasAccess(IIdentity identity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5141DFD2-0FFD-43D7-BAC7-1F9B3BBEBF63}</ProjectGuid>
<ProjectGuid>{2D14710E-BF4B-4A05-B189-DBC94D0DEC1C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>N2N.Data</RootNamespace>
<AssemblyName>N2N.Data</AssemblyName>
<RootNamespace>N2N.Services</RootNamespace>
<AssemblyName>N2N.Services</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
Expand Down Expand Up @@ -40,13 +40,22 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ISecurityService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IN2NUserService.cs" />
<Compile Include="N2NUserService.cs" />
<Compile Include="SecurityService.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\N2N.Core\N2N.Core.csproj">
<Project>{94757EF4-4F40-4FCA-B64F-95AB5942B0D6}</Project>
<Project>{94757ef4-4f40-4fca-b64f-95ab5942b0d6}</Project>
<Name>N2N.Core</Name>
</ProjectReference>
<ProjectReference Include="..\N2N.Infrastructure\N2N.Infrasturcture.csproj">
<Project>{e45eae66-10ed-4179-805a-f6a5b65f6532}</Project>
<Name>N2N.Infrasturcture</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
52 changes: 52 additions & 0 deletions src/N2N.Services/N2NUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using N2N.Core.Entities;
using N2N.Data.Repositories;
using N2N.Infrastructure.Models;
using N2N.Services.Users;

namespace N2N.Services
{
public class N2NUserService : IN2NUserService
{
private IRepository<N2NUser> _userRepo;
private ISecurityService _security;


public N2NUserService(IRepository<N2NUser> userRepo, ISecurityService security)
{
this._userRepo = userRepo;
this._security = security;
}

public OperationResult CreateUser(N2NUser user)
{
var result = new OperationResult()
{
Success = false
};

// add verification of user fields

if (this._security.HasAccess())
{
//add user here
_userRepo.Add(user);

result.Data = user;
result.Success = true;
result.Messages = new[] { $"User was created with Id = ${user.Id}" };
}
else
{
result.Messages = new []{ "You dont have permissions to create user" };
}

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("N2N.Data")]
[assembly: AssemblyTitle("N2N.Services")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("N2N.Data")]
[assembly: AssemblyProduct("N2N.Services")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand All @@ -20,7 +20,7 @@
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5141dfd2-0ffd-43d7-bac7-1f9b3bbebf63")]
[assembly: Guid("2d14710e-bf4b-4a05-b189-dbc94d0dec1c")]

// Version information for an assembly consists of the following four values:
//
Expand Down
25 changes: 25 additions & 0 deletions src/N2N.Services/SecurityService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace N2N.Services
{
public class SecurityService : ISecurityService
{
public bool HasAccess()
{
var principal = System.Threading.Thread.CurrentPrincipal;

return true;
}

public bool HasAccess(IIdentity identity)
{
throw new NotImplementedException();
}
}
}

0 comments on commit b966c52

Please sign in to comment.