-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure the project dependencies and ioc registrations
- Loading branch information
Showing
47 changed files
with
730 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace BusinessServices.Person.Extensions | ||
{ | ||
public static class PersonExtensions | ||
{ | ||
public static DataAccess.Person.Person ToEnitity(this Models.DTOs.PersonModel model) | ||
{ | ||
if (model == null) | ||
return null; | ||
|
||
return new DataAccess.Person.Person | ||
{ | ||
Id = model.Id, | ||
Firstname = model.Firstname, | ||
Lastname = model.Lastname, | ||
Buildings = model.Buildings, | ||
Line = model.Line, | ||
Manager = model.Manager.ToEnitity(), | ||
Products = model.Products, | ||
Team = model.Team, | ||
}; | ||
} | ||
|
||
public static Models.DTOs.PersonModel ToModel(this DataAccess.Person.Person entity) | ||
{ | ||
if (entity == null) | ||
return null; | ||
|
||
return new Models.DTOs.PersonModel | ||
{ | ||
Id = entity.Id, | ||
Firstname = entity.Firstname, | ||
Lastname = entity.Lastname, | ||
Buildings = entity.Buildings, | ||
Line = entity.Line, | ||
Manager = entity.Manager.ToModel(), | ||
Products = entity.Products, | ||
Team = entity.Team, | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
using System; | ||
using Models.DTOs; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace BusinessServices.Person | ||
{ | ||
public interface IPersonService | ||
{ | ||
Task<object> Get(string id); | ||
Task<object> Update(object person); | ||
Task<object> Delete(string id); | ||
Task<PersonModel> Add(PersonModel person); | ||
|
||
Task<PersonModel> Delete(string id); | ||
|
||
Task<PersonModel> Get(string id); | ||
|
||
Task<IEnumerable<PersonModel>> GetAll(); | ||
|
||
Task<PersonModel> Update(PersonModel person); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,85 @@ | ||
using DataAccess.Interfaces; | ||
using BusinessServices.Person.Extensions; | ||
using DataAccess.CacheProvider; | ||
using DataAccess.Person; | ||
using System; | ||
using Models.DTOs; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace BusinessServices.Person | ||
{ | ||
public class PersonService : IPersonService | ||
{ | ||
private readonly IPersonRepository repository; | ||
private readonly ICacheProvider _cache; | ||
private const string CACHE_KEY_PREFIX = "PERSON_QUERY"; | ||
|
||
public PersonService(IPersonRepository repository) | ||
public PersonService(IPersonRepository repository, ICacheProvider cache) | ||
{ | ||
this.repository = repository; | ||
this._cache = cache; | ||
} | ||
|
||
public async Task<object> Delete(string id) | ||
public async Task<PersonModel> Get(string id) | ||
{ | ||
var result = await repository.Delete(id); | ||
return result; | ||
var queryCacheKey = string.Format("{0}_{1}", CACHE_KEY_PREFIX, id.ToUpper()); | ||
var found = _cache.Fetch<PersonModel>(queryCacheKey); | ||
if (found != null) | ||
{ | ||
return found; | ||
} | ||
|
||
var results = await repository.Get(id); | ||
if (results != null && _cache.Save(queryCacheKey, results)) | ||
{ | ||
return results.ToModel(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task<object> Get(string id) | ||
public async Task<PersonModel> Update(PersonModel person) | ||
{ | ||
var result = await repository.Get(id); | ||
return result; | ||
var result = await repository.Update(person.ToEnitity()); | ||
|
||
var queryCacheKey = string.Format("{0}_{1}", CACHE_KEY_PREFIX, result.Id.ToUpper()); | ||
if (result != null && _cache.Save(queryCacheKey, result.ToModel())) | ||
{ | ||
return result.ToModel(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task<object> Update(object person) | ||
public async Task<IEnumerable<PersonModel>> GetAll() | ||
{ | ||
var result = await repository.Update((DataAccess.Person.Person)person); | ||
return result; | ||
var result = await repository.All(); | ||
return result.Select(r => r.ToModel()); | ||
} | ||
|
||
public async Task<IEnumerable<object>> GetAll() | ||
public async Task<PersonModel> Create(PersonModel person) | ||
{ | ||
var result = await repository.All(); | ||
return result; | ||
var result = await repository.Add(person.ToEnitity()); | ||
return result.ToModel(); | ||
} | ||
|
||
public async Task<object> Create(object person) | ||
public async Task<PersonModel> Add(PersonModel person) | ||
{ | ||
var result = await repository.Add((DataAccess.Person.Person)person); | ||
return result; | ||
var result = await repository.Add(person.ToEnitity()); | ||
|
||
var queryCacheKey = string.Format("{0}_{1}", CACHE_KEY_PREFIX, result.Id.ToUpper()); | ||
if (result != null && _cache.Save(queryCacheKey, result.ToModel())) | ||
{ | ||
return result.ToModel(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task<PersonModel> Delete(string id) | ||
{ | ||
var result = await repository.Delete(id); | ||
return new PersonModel { Id = id }; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace DataAccess.CacheProvider | ||
{ | ||
public interface ICacheProvider | ||
{ | ||
T Fetch<T>(string key); | ||
|
||
bool Save(string key, object value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
|
||
namespace DataAccess.CacheProvider | ||
{ | ||
public class InMemoryCache : ICacheProvider | ||
{ | ||
private readonly IMemoryCache _memoryCache; | ||
|
||
public InMemoryCache(IMemoryCache memoryCache) | ||
{ | ||
_memoryCache = memoryCache; | ||
} | ||
|
||
public T Fetch<T>(string key) | ||
{ | ||
T entry; | ||
|
||
if (!_memoryCache.TryGetValue(key, out entry)) | ||
{ | ||
_memoryCache.Set(key, entry, new MemoryCacheEntryOptions() | ||
.SetSlidingExpiration(TimeSpan.FromMinutes(5)) | ||
.SetAbsoluteExpiration(TimeSpan.FromHours(1)) | ||
.SetPriority(CacheItemPriority.Low) | ||
); | ||
} | ||
|
||
return entry; | ||
} | ||
|
||
public bool Save(string key, object value) | ||
{ | ||
try | ||
{ | ||
_memoryCache.Set(key, value, new MemoryCacheEntryOptions() | ||
.SetSlidingExpiration(TimeSpan.FromMinutes(5)) | ||
.SetAbsoluteExpiration(TimeSpan.FromHours(1)) | ||
.SetPriority(CacheItemPriority.Low) | ||
); | ||
|
||
return true; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace DataAccess.Person | ||
{ | ||
public class PersonQueries | ||
{ | ||
public IDictionary<string, string> Mutations | ||
{ | ||
get | ||
{ | ||
return new Dictionary<string, string> | ||
{ | ||
{ "MERGE_PERSON", @"CREATE (p:Person) SET p = $p RETURN p"}, | ||
{ "DELETE_PERSON", @""}, | ||
{ "DEACTIVATE_PERSON", @""} | ||
}; | ||
} | ||
} | ||
|
||
public IDictionary<string, string> Queries | ||
{ | ||
get | ||
{ | ||
return new Dictionary<string, string> | ||
{ | ||
{ "GET_PERSON", @"MATCH (p:Person {id = $id}) RETURN p"}, | ||
{ "GET_PEOPLE", @"MATCH (p:Person) RETURN p" } | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
using System; | ||
using DataAccess.Interfaces; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DataAccess.Person | ||
{ | ||
public class PersonRepository | ||
public class PersonRepository : IPersonRepository | ||
{ | ||
|
||
private readonly IDictionary<string, string> _mutations; | ||
private readonly IDictionary<string, string> _queries; | ||
private readonly IRepository _repository; | ||
|
||
public PersonRepository(IRepository repository) | ||
{ | ||
_repository = repository; | ||
_queries = new PersonQueries().Queries; | ||
_mutations = new PersonQueries().Mutations; | ||
} | ||
|
||
public async Task<Person> Add(Person person) | ||
{ | ||
var entity = await _repository.Write<Person>(_mutations["MERGE_PERSON"], person); | ||
return entity; | ||
} | ||
|
||
public async Task<IEnumerable<Person>> All() | ||
{ | ||
var entity = await _repository.Read<IEnumerable<Person>>(_queries["GET_PEOPLE"], null); | ||
return entity; | ||
} | ||
|
||
public async Task<string> Delete(string id) | ||
{ | ||
var entity = await _repository.Write<Person>(_mutations["DELETE_PERSON"], id); | ||
return entity.Id; | ||
} | ||
|
||
public async Task<Person> Get(string id) | ||
{ | ||
var entity = await _repository.Read<Person>(_queries["GET_PERSON"], id); | ||
return entity; | ||
} | ||
|
||
public async Task<Person> Update(Person person) | ||
{ | ||
var entity = await _repository.Write<Person>(_mutations["MERGE_PERSON"], person); | ||
return entity; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="9.0.0" /> | ||
<PackageReference Include="GraphQL" Version="2.4.0" /> | ||
<PackageReference Include="GraphQL.Common" Version="1.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BusinessServices\BusinessServices.csproj" /> | ||
<ProjectReference Include="..\Models\Models.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.AspNetCore.Http.Abstractions"> | ||
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.