Skip to content

Commit

Permalink
refactor: restructure the project dependencies and ioc registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
psyphore committed Sep 14, 2019
1 parent 2c22cab commit 2c39632
Show file tree
Hide file tree
Showing 47 changed files with 730 additions and 349 deletions.
1 change: 1 addition & 0 deletions BusinessServices/BusinessServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="GraphQL" Version="2.4.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="System.Reactive" Version="4.1.6" />
</ItemGroup>
Expand Down
41 changes: 41 additions & 0 deletions BusinessServices/Person/Extensions/PersonExtensions.cs
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,
};
}
}
}
18 changes: 11 additions & 7 deletions BusinessServices/Person/IPersonService.cs
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);
}
}
}
75 changes: 55 additions & 20 deletions BusinessServices/Person/PersonService.cs
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 };
}
}
}
}
9 changes: 9 additions & 0 deletions DataAccess/CacheProvider/ICacheProvider.cs
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);
}
}
50 changes: 50 additions & 0 deletions DataAccess/CacheProvider/InMemoryCache.cs
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;
}
}

}
}
7 changes: 7 additions & 0 deletions DataAccess/DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="Neo4j.Driver" Version="1.7.2" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Caching.Abstractions">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.caching.abstractions\2.2.0\lib\netstandard2.0\Microsoft.Extensions.Caching.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions DataAccess/Person/PersonQueries.cs
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" }
};
}
}
}
}
49 changes: 43 additions & 6 deletions DataAccess/Person/PersonRepository.cs
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;
}
}
}
}
24 changes: 24 additions & 0 deletions GraphQLCore/GraphQLCore.csproj
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>
Loading

0 comments on commit 2c39632

Please sign in to comment.