-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from EISK/net-6.0-upgrade
Net 6.0 upgrade
- Loading branch information
Showing
96 changed files
with
2,489 additions
and
602 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 was deleted.
Oops, something went wrong.
Submodule Core
deleted from
bd729e
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Eisk.Core\Eisk.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,12 @@ | ||
using Xunit; | ||
|
||
namespace Eisk.Core.Tests; | ||
|
||
public class UnitTest1 | ||
{ | ||
[Fact] | ||
public void Test1() | ||
{ | ||
|
||
} | ||
} |
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.EntityFrameworkCore; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Eisk.Core.DataService.EFCore; | ||
|
||
public class EntityDataService<TEntity> : IEntityDataService<TEntity> where TEntity : class, new() | ||
{ | ||
protected readonly DbContext DbContext; | ||
|
||
public EntityDataService(DbContext dbContext) | ||
{ | ||
DbContext = dbContext; | ||
} | ||
|
||
public virtual async Task<TEntity> GetById<TId>(TId id) | ||
{ | ||
return await DbContext.Set<TEntity>().FindAsync(id); | ||
} | ||
|
||
public virtual async Task<IList<TEntity>> GetAll() | ||
{ | ||
return await DbContext.Set<TEntity>().ToListAsync(); | ||
} | ||
|
||
public virtual async Task<TEntity> Add(TEntity entity) | ||
{ | ||
var obj = DbContext.Add(entity); | ||
|
||
await DbContext.SaveChangesAsync(); | ||
|
||
return obj.Entity; | ||
} | ||
|
||
public virtual async Task<TEntity> Update(TEntity entity) | ||
{ | ||
var obj = DbContext.Update(entity); | ||
|
||
await DbContext.SaveChangesAsync(); | ||
|
||
return obj.Entity; | ||
} | ||
|
||
public virtual async Task Delete(TEntity entity) | ||
{ | ||
DbContext.Remove(entity); | ||
|
||
await DbContext.SaveChangesAsync(); | ||
} | ||
} |
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,13 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Eisk.Core.DataService; | ||
|
||
public interface IEntityDataService<TEntity> where TEntity : class, new() | ||
{ | ||
Task<TEntity> GetById<TId>(TId id); | ||
Task<IList<TEntity>> GetAll(); | ||
Task<TEntity> Add(TEntity entity); | ||
Task<TEntity> Update(TEntity entity); | ||
Task Delete(TEntity 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,100 @@ | ||
using Eisk.Core.Exceptions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Eisk.Core.DomainService; | ||
|
||
using DataService; | ||
using Utils; | ||
|
||
public class DomainService<TDomain, TId> | ||
where TDomain : class, new() | ||
{ | ||
readonly IEntityDataService<TDomain> _entityDataService; | ||
|
||
public DomainService(IEntityDataService<TDomain> entityDataService) | ||
{ | ||
_entityDataService = entityDataService; | ||
} | ||
|
||
public virtual async Task<IEnumerable<TDomain>> GetAll() | ||
{ | ||
return await _entityDataService.GetAll(); | ||
} | ||
|
||
public virtual async Task<TDomain> GetById(TId id) | ||
{ | ||
if (id.IsNullOrEmpty()) | ||
ThrowExceptionForInvalidLookupIdParameter(); | ||
|
||
var entityInDb = await _entityDataService.GetById(id); | ||
|
||
if (entityInDb == null) | ||
ThrowExceptionForNonExistantEntity(id); | ||
|
||
return entityInDb; | ||
} | ||
|
||
public virtual async Task<TDomain> Add(TDomain entity) | ||
{ | ||
return await Add(entity, null); | ||
} | ||
|
||
public virtual async Task<TDomain> Add(TDomain entity, Action<TDomain> preProcessAction, Action<TDomain> postProcessAction = null) | ||
{ | ||
if (entity == null) | ||
ThrowExceptionForNullInputEntity(); | ||
|
||
preProcessAction?.Invoke(entity); | ||
|
||
var returnVal = await _entityDataService.Add(entity); | ||
|
||
postProcessAction?.Invoke(returnVal); | ||
|
||
return returnVal; | ||
} | ||
|
||
public virtual async Task<TDomain> Update(TId id, TDomain newEntity) | ||
{ | ||
return await Update(id, newEntity, null); | ||
} | ||
|
||
public virtual async Task<TDomain> Update(TId id, TDomain newEntity, Action<TDomain, TDomain> preProcessAction, Action<TDomain> postProcessAction = null) | ||
{ | ||
if (newEntity == null) | ||
ThrowExceptionForNullInputEntity(); | ||
|
||
var oldEntity = await GetById(id); | ||
|
||
preProcessAction?.Invoke(oldEntity, newEntity); | ||
|
||
var returnVal = await _entityDataService.Update(newEntity); | ||
|
||
postProcessAction?.Invoke(returnVal); | ||
|
||
return returnVal; | ||
} | ||
|
||
public virtual async Task Delete(TId id) | ||
{ | ||
var entityInDb = await GetById(id); | ||
|
||
await _entityDataService.Delete(entityInDb); | ||
} | ||
|
||
protected virtual void ThrowExceptionForNullInputEntity() | ||
{ | ||
throw new NullInputEntityException<TDomain>(); | ||
} | ||
|
||
protected virtual void ThrowExceptionForInvalidLookupIdParameter() | ||
{ | ||
throw new InvalidLookupIdParameterException<TDomain>(); | ||
} | ||
|
||
protected virtual void ThrowExceptionForNonExistantEntity(TId idValue) | ||
{ | ||
throw new NonExistantEntityException<TDomain>(idValue); | ||
} | ||
} |
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,31 @@ | ||
using System; | ||
|
||
namespace Eisk.Core.Exceptions; | ||
|
||
public class CoreException : Exception | ||
{ | ||
private const string DefaultErrorCode = "APP-ERROR-000"; | ||
|
||
private string _message; | ||
public override string Message => _message ?? (_message = ConvertToSentence(GetType().Name, ErrorCode)); | ||
|
||
private string _errorCode; | ||
|
||
/// <summary> | ||
/// Error code helps distringuishing same types of errors in different context. | ||
/// </summary> | ||
public string ErrorCode => _errorCode ?? (_errorCode = DefaultErrorCode); | ||
|
||
public CoreException(string message = null, string errorCode = null) | ||
{ | ||
_message = message; | ||
_errorCode = errorCode; | ||
} | ||
|
||
//TODO: convert class name to sentence | ||
static string ConvertToSentence(string message, string errorCode) | ||
{ | ||
return errorCode + ": " + message; | ||
} | ||
|
||
} |
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 Eisk.Core.Exceptions; | ||
|
||
public class DomainException<TEntity> : CoreException | ||
{ | ||
public DomainException(string message = null, string errorCode = null) : base(message, errorCode) | ||
{ | ||
|
||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Core/Eisk.Core/Exceptions/InvalidLookupIdParameterException.cs
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,10 @@ | ||
namespace Eisk.Core.Exceptions; | ||
|
||
public class InvalidLookupIdParameterException<TEntity> : DomainException<TEntity> | ||
{ | ||
public InvalidLookupIdParameterException(string paramName = "id") : base( | ||
$"Invalid lookup parameter: {paramName} to find {typeof(TEntity).Name}.", "APP-DATA-ERROR-001") | ||
{ | ||
|
||
} | ||
} |
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,11 @@ | ||
namespace Eisk.Core.Exceptions; | ||
|
||
public class NonExistantEntityException<TEntity> : DomainException<TEntity> | ||
|
||
{ | ||
public NonExistantEntityException(object paramValue, string paramName = "id") : base( | ||
$"No {typeof(TEntity).Name} exists for given id {paramValue} for parameter {paramName}.", "APP-DATA-ERROR-002") | ||
{ | ||
|
||
} | ||
} |
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 Eisk.Core.Exceptions; | ||
|
||
public class NullInputEntityException<TEntity> : DomainException<TEntity> | ||
{ | ||
public NullInputEntityException() : base("Input object to be created or updated is null.", "APP-DATA-ERROR-003") | ||
{ | ||
|
||
} | ||
} |
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,11 @@ | ||
namespace Eisk.Core.Exceptions; | ||
|
||
public class UpdatingIdIsNotSupported<TEntity> : DomainException<TEntity> | ||
|
||
{ | ||
public UpdatingIdIsNotSupported(object paramValue, string paramName = "id") : base( | ||
$"Updating {typeof(TEntity).Name} field {paramName} is not supported. Provided value: {paramValue}.", "APP-DATA-ERROR-004") | ||
{ | ||
|
||
} | ||
} |
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,13 @@ | ||
namespace Eisk.Core.Utils; | ||
|
||
using Exceptions; | ||
|
||
public class ExceptionThrower | ||
{ | ||
|
||
public static void Throws<T>() | ||
where T : CoreException, new() | ||
{ | ||
throw new T(); | ||
} | ||
} |
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,35 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace Eisk.Core.Utils; | ||
|
||
using Exceptions; | ||
|
||
public static class ExpressionUtil<TDomain> | ||
{ | ||
public static object GetPropertyValue<TField>(Expression<Func<TDomain, TField>> expression, TDomain data) | ||
{ | ||
if (data == null) | ||
throw new NullInputEntityException<TDomain>(); | ||
|
||
var prop = GetPropertyInfo(expression); | ||
var value = prop.GetValue(data); | ||
return value; | ||
} | ||
|
||
public static void SetPropertyValue<TField>(Expression<Func<TDomain, TField>> expression, TDomain data, object value) | ||
{ | ||
if (data == null) | ||
throw new NullInputEntityException<TDomain>(); | ||
|
||
var prop = GetPropertyInfo(expression); | ||
prop.SetValue(data, value); | ||
} | ||
|
||
public static PropertyInfo GetPropertyInfo<TField>(Expression<Func<TDomain, TField>> expression) | ||
{ | ||
var expr = (MemberExpression)expression.Body; | ||
return (PropertyInfo)expr.Member; | ||
} | ||
} |
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,12 @@ | ||
namespace Eisk.Core.Utils; | ||
|
||
public static class ObjectExtensions | ||
{ | ||
public static bool IsNullOrEmpty<T>(this T value) | ||
{ | ||
if (typeof(T) == typeof(string)) | ||
return string.IsNullOrEmpty(value as string); | ||
|
||
return value == null || value.Equals(default(T)); | ||
} | ||
} |
Oops, something went wrong.