Skip to content

Commit de24a94

Browse files
authored
Merge pull request #5 from EISK/net-6.0-upgrade
Net 6.0 upgrade
2 parents 8c20976 + 5cef958 commit de24a94

File tree

96 files changed

+2489
-602
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+2489
-602
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,4 @@ ASALocalRun/
331331
/build/dnn-template-render
332332
/build/content
333333
/build/content-repo
334+
/WebApi/Eisk.WebApi/Properties/ServiceDependencies/eisk-webapi-1 - Web Deploy/profile.arm.json

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

Core

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\Eisk.Core\Eisk.Core.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

Core/Eisk.Core.Tests/UnitTest1.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Xunit;
2+
3+
namespace Eisk.Core.Tests;
4+
5+
public class UnitTest1
6+
{
7+
[Fact]
8+
public void Test1()
9+
{
10+
11+
}
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace Eisk.Core.DataService.EFCore;
6+
7+
public class EntityDataService<TEntity> : IEntityDataService<TEntity> where TEntity : class, new()
8+
{
9+
protected readonly DbContext DbContext;
10+
11+
public EntityDataService(DbContext dbContext)
12+
{
13+
DbContext = dbContext;
14+
}
15+
16+
public virtual async Task<TEntity> GetById<TId>(TId id)
17+
{
18+
return await DbContext.Set<TEntity>().FindAsync(id);
19+
}
20+
21+
public virtual async Task<IList<TEntity>> GetAll()
22+
{
23+
return await DbContext.Set<TEntity>().ToListAsync();
24+
}
25+
26+
public virtual async Task<TEntity> Add(TEntity entity)
27+
{
28+
var obj = DbContext.Add(entity);
29+
30+
await DbContext.SaveChangesAsync();
31+
32+
return obj.Entity;
33+
}
34+
35+
public virtual async Task<TEntity> Update(TEntity entity)
36+
{
37+
var obj = DbContext.Update(entity);
38+
39+
await DbContext.SaveChangesAsync();
40+
41+
return obj.Entity;
42+
}
43+
44+
public virtual async Task Delete(TEntity entity)
45+
{
46+
DbContext.Remove(entity);
47+
48+
await DbContext.SaveChangesAsync();
49+
}
50+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Eisk.Core.DataService;
5+
6+
public interface IEntityDataService<TEntity> where TEntity : class, new()
7+
{
8+
Task<TEntity> GetById<TId>(TId id);
9+
Task<IList<TEntity>> GetAll();
10+
Task<TEntity> Add(TEntity entity);
11+
Task<TEntity> Update(TEntity entity);
12+
Task Delete(TEntity entity);
13+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using Eisk.Core.Exceptions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace Eisk.Core.DomainService;
7+
8+
using DataService;
9+
using Utils;
10+
11+
public class DomainService<TDomain, TId>
12+
where TDomain : class, new()
13+
{
14+
readonly IEntityDataService<TDomain> _entityDataService;
15+
16+
public DomainService(IEntityDataService<TDomain> entityDataService)
17+
{
18+
_entityDataService = entityDataService;
19+
}
20+
21+
public virtual async Task<IEnumerable<TDomain>> GetAll()
22+
{
23+
return await _entityDataService.GetAll();
24+
}
25+
26+
public virtual async Task<TDomain> GetById(TId id)
27+
{
28+
if (id.IsNullOrEmpty())
29+
ThrowExceptionForInvalidLookupIdParameter();
30+
31+
var entityInDb = await _entityDataService.GetById(id);
32+
33+
if (entityInDb == null)
34+
ThrowExceptionForNonExistantEntity(id);
35+
36+
return entityInDb;
37+
}
38+
39+
public virtual async Task<TDomain> Add(TDomain entity)
40+
{
41+
return await Add(entity, null);
42+
}
43+
44+
public virtual async Task<TDomain> Add(TDomain entity, Action<TDomain> preProcessAction, Action<TDomain> postProcessAction = null)
45+
{
46+
if (entity == null)
47+
ThrowExceptionForNullInputEntity();
48+
49+
preProcessAction?.Invoke(entity);
50+
51+
var returnVal = await _entityDataService.Add(entity);
52+
53+
postProcessAction?.Invoke(returnVal);
54+
55+
return returnVal;
56+
}
57+
58+
public virtual async Task<TDomain> Update(TId id, TDomain newEntity)
59+
{
60+
return await Update(id, newEntity, null);
61+
}
62+
63+
public virtual async Task<TDomain> Update(TId id, TDomain newEntity, Action<TDomain, TDomain> preProcessAction, Action<TDomain> postProcessAction = null)
64+
{
65+
if (newEntity == null)
66+
ThrowExceptionForNullInputEntity();
67+
68+
var oldEntity = await GetById(id);
69+
70+
preProcessAction?.Invoke(oldEntity, newEntity);
71+
72+
var returnVal = await _entityDataService.Update(newEntity);
73+
74+
postProcessAction?.Invoke(returnVal);
75+
76+
return returnVal;
77+
}
78+
79+
public virtual async Task Delete(TId id)
80+
{
81+
var entityInDb = await GetById(id);
82+
83+
await _entityDataService.Delete(entityInDb);
84+
}
85+
86+
protected virtual void ThrowExceptionForNullInputEntity()
87+
{
88+
throw new NullInputEntityException<TDomain>();
89+
}
90+
91+
protected virtual void ThrowExceptionForInvalidLookupIdParameter()
92+
{
93+
throw new InvalidLookupIdParameterException<TDomain>();
94+
}
95+
96+
protected virtual void ThrowExceptionForNonExistantEntity(TId idValue)
97+
{
98+
throw new NonExistantEntityException<TDomain>(idValue);
99+
}
100+
}

Core/Eisk.Core/Eisk.Core.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<OutputType>Library</OutputType>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace Eisk.Core.Exceptions;
4+
5+
public class CoreException : Exception
6+
{
7+
private const string DefaultErrorCode = "APP-ERROR-000";
8+
9+
private string _message;
10+
public override string Message => _message ?? (_message = ConvertToSentence(GetType().Name, ErrorCode));
11+
12+
private string _errorCode;
13+
14+
/// <summary>
15+
/// Error code helps distringuishing same types of errors in different context.
16+
/// </summary>
17+
public string ErrorCode => _errorCode ?? (_errorCode = DefaultErrorCode);
18+
19+
public CoreException(string message = null, string errorCode = null)
20+
{
21+
_message = message;
22+
_errorCode = errorCode;
23+
}
24+
25+
//TODO: convert class name to sentence
26+
static string ConvertToSentence(string message, string errorCode)
27+
{
28+
return errorCode + ": " + message;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)