Skip to content

Commit

Permalink
Merge pull request #5 from EISK/net-6.0-upgrade
Browse files Browse the repository at this point in the history
Net 6.0 upgrade
  • Loading branch information
AshrafAlam authored Apr 27, 2022
2 parents 8c20976 + 5cef958 commit de24a94
Show file tree
Hide file tree
Showing 96 changed files with 2,489 additions and 602 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,4 @@ ASALocalRun/
/build/dnn-template-render
/build/content
/build/content-repo
/WebApi/Eisk.WebApi/Properties/ServiceDependencies/eisk-webapi-1 - Web Deploy/profile.arm.json
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion Core
Submodule Core deleted from bd729e
22 changes: 22 additions & 0 deletions Core/Eisk.Core.Tests/Eisk.Core.Tests.csproj
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>
12 changes: 12 additions & 0 deletions Core/Eisk.Core.Tests/UnitTest1.cs
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()
{

}
}
50 changes: 50 additions & 0 deletions Core/Eisk.Core/DataService/EFCore/EntityDataService.cs
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();
}
}
13 changes: 13 additions & 0 deletions Core/Eisk.Core/DataService/IEntityDataService.cs
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);
}
100 changes: 100 additions & 0 deletions Core/Eisk.Core/DomainService/DomainService.cs
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);
}
}
13 changes: 13 additions & 0 deletions Core/Eisk.Core/Eisk.Core.csproj
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>
31 changes: 31 additions & 0 deletions Core/Eisk.Core/Exceptions/CoreException.cs
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;
}

}
9 changes: 9 additions & 0 deletions Core/Eisk.Core/Exceptions/DomainException.cs
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 Core/Eisk.Core/Exceptions/InvalidLookupIdParameterException.cs
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")
{

}
}
11 changes: 11 additions & 0 deletions Core/Eisk.Core/Exceptions/NonExistantEntityException.cs
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")
{

}
}
9 changes: 9 additions & 0 deletions Core/Eisk.Core/Exceptions/NullInputEntityException.cs
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")
{

}
}
11 changes: 11 additions & 0 deletions Core/Eisk.Core/Exceptions/UpdatingIdIsNotSupported.cs
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")
{

}
}
13 changes: 13 additions & 0 deletions Core/Eisk.Core/Utils/ExceptionThrower.cs
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();
}
}
35 changes: 35 additions & 0 deletions Core/Eisk.Core/Utils/ExpressionUtil.cs
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;
}
}
12 changes: 12 additions & 0 deletions Core/Eisk.Core/Utils/ObjectExtensions.cs
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));
}
}
Loading

0 comments on commit de24a94

Please sign in to comment.