Skip to content

Commit d692685

Browse files
committed
Version 2.0.0 initial commit 2
1 parent 0172020 commit d692685

19 files changed

+449
-247
lines changed

MZBase.Domain/MZBase.Domain.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<Nullable>enable</Nullable>
6-
<Version>1.0.8</Version>
6+
<Version>2.0.0</Version>
77
<Authors>Mahdi Zandakbari</Authors>
88
<Description>A simple library for base classes of common usabilities needed in enterprise software</Description>
99
<PackageProjectUrl>https://github.com/mzand111/MZBase</PackageProjectUrl>
@@ -13,8 +13,8 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="MZSimpleDynamicLinq.Core" Version="1.0.2" />
17-
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
16+
<PackageReference Include="MZSimpleDynamicLinq.Core" Version="1.0.3" />
17+
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
1818
</ItemGroup>
1919

2020
</Project>
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<Version>1.0.1</Version>
7+
<Version>2.0.0</Version>
88
<Authors>Mahdi Zandakbari</Authors>
99
<Description>Contains:
10-
1- A helper to transfer description attributes of entity classes to extended properties (MS_Description) of related table/column of databse for any ef core DbContext. This changes could be used by any third party tool like Redgate SqlDoc to generate Database documentation.</Description>
10+
1- A helper to transfer description attributes of entity classes to extended properties (MS_Description) of related table/column of database for any ef core DbContext. This changes could be used by any third party tool like Redgate SqlDoc to generate Database documentation.</Description>
1111
<PackageProjectUrl>https://github.com/mzand111/MZBase</PackageProjectUrl>
1212
<RepositoryUrl>https://github.com/mzand111/MZBase</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
18-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.6" />
19-
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.6" />
17+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
19+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.2" />
2020
</ItemGroup>
2121

2222
</Project>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Logging;
3+
using MZBase.Domain;
4+
using MZBase.Infrastructure;
5+
using MZBase.Infrastructure.Service;
6+
using MZBase.Infrastructure.Service.Exceptions;
7+
using MZSimpleDynamicLinq.Core;
8+
9+
namespace MZBase.EntityFrameworkCore
10+
{
11+
public abstract class EFCoreStorageBusinessService<DomainModel, DBModelEntity, PrimaryKeyType, UnitOfWork, DataContext> :
12+
BaseStorageBusinessService<DomainModel, PrimaryKeyType>, IStorageBusinessService<DomainModel, PrimaryKeyType>
13+
where DomainModel : Model<PrimaryKeyType>
14+
where DBModelEntity : DomainModel, IConvertibleDBModelEntity<DomainModel>, new()
15+
where PrimaryKeyType : struct
16+
where UnitOfWork : UnitOfWorkAsync<DataContext>, IDynamicTestableUnitOfWorkAsync
17+
where DataContext : DbContext
18+
{
19+
protected readonly UnitOfWork _unitOfWork;
20+
protected readonly ILDRCompatibleRepositoryAsync<DomainModel, DBModelEntity, PrimaryKeyType> _baseRepo;
21+
22+
public EFCoreStorageBusinessService(UnitOfWork unitOfWork,
23+
IDateTimeProviderService dateTimeProvider,
24+
ILogger<DomainModel> logger
25+
) : base(logger, dateTimeProvider, 600)
26+
{
27+
_unitOfWork = unitOfWork;
28+
_baseRepo = _unitOfWork.GetRepo<DomainModel, DBModelEntity, PrimaryKeyType>();
29+
}
30+
public override async Task<PrimaryKeyType> AddAsync(DomainModel item)
31+
{
32+
if (item == null)
33+
{
34+
var ex = new ServiceArgumentNullException("Input parameter was null:" + nameof(item));
35+
LogAdd(null, null, ex);
36+
throw ex;
37+
}
38+
39+
await ValidateOnAddAsync(item);
40+
41+
var g = _baseRepo.Insert(item);
42+
try
43+
{
44+
await _unitOfWork.CommitAsync();
45+
46+
LogAdd(item, "Successfully add item with ,ID:" +
47+
g?.ID.ToString()
48+
);
49+
return g.ID;
50+
}
51+
catch (Exception ex)
52+
{
53+
LogAdd(item, "", ex);
54+
throw new ServiceStorageException("Error adding item", ex);
55+
}
56+
}
57+
public override async Task ModifyAsync(DomainModel item)
58+
{
59+
if (item == null)
60+
{
61+
var exception = new ServiceArgumentNullException(typeof(DomainModel).Name);
62+
LogModify(item, null, exception);
63+
throw exception;
64+
}
65+
66+
var currentItem = await _baseRepo.GetByIdAsync(item.ID);
67+
if (currentItem == null)
68+
{
69+
var noObj = new ServiceObjectNotFoundException(typeof(DomainModel).Name + " Not Found");
70+
LogModify(item, null, noObj);
71+
throw noObj;
72+
}
73+
await ValidateOnModifyAsync(item, currentItem);
74+
currentItem.SetFieldsFromDomainModel(item);
75+
try
76+
{
77+
await _unitOfWork.CommitAsync();
78+
LogModify(item, "Successfully modified item with ,ID:" +
79+
item.ID.ToString()
80+
);
81+
}
82+
83+
catch (Exception ex)
84+
{
85+
LogModify(item, "", ex);
86+
throw new ServiceStorageException("Error modifying item", ex);
87+
}
88+
}
89+
public override async Task RemoveByIdAsync(PrimaryKeyType ID)
90+
{
91+
var itemToDelete = await _baseRepo.FirstOrDefaultAsync(ss => ss.ID.Equals(ID));
92+
93+
if (itemToDelete == null)
94+
{
95+
var f = new ServiceObjectNotFoundException(typeof(DomainModel).Name + " not found");
96+
LogRemove(ID, "Item With This Id Not Found", f);
97+
throw f;
98+
}
99+
_baseRepo.Delete(itemToDelete);
100+
try
101+
{
102+
await _unitOfWork.CommitAsync();
103+
LogRemove(ID, "Item Deleted Successfully", null);
104+
}
105+
106+
catch (Exception ex)
107+
{
108+
var innerEx = new ServiceStorageException("Error deleting item with id" + ID.ToString(), ex);
109+
LogRemove(ID, null, ex);
110+
throw innerEx;
111+
}
112+
}
113+
public override async Task<DomainModel> RetrieveByIdAsync(PrimaryKeyType ID)
114+
{
115+
DBModelEntity? item;
116+
try
117+
{
118+
item = await _baseRepo.FirstOrDefaultAsync(ss => ss.ID.Equals(ID));
119+
}
120+
catch (Exception ex)
121+
{
122+
LogRetrieveSingle(ID, ex);
123+
throw new ServiceStorageException("Error loading item", ex);
124+
}
125+
if (item == null)
126+
{
127+
var f = new ServiceObjectNotFoundException(typeof(DomainModel).Name + " not found by id");
128+
LogRetrieveSingle(ID, f);
129+
throw f;
130+
}
131+
LogRetrieveSingle(ID);
132+
return item.GetDomainObject();
133+
}
134+
public override async Task<LinqDataResult<DomainModel>> ItemsAsync(LinqDataRequest request)
135+
{
136+
try
137+
{
138+
var f = await _baseRepo.AllItemsAsync(request);
139+
LogRetrieveMultiple(null, request);
140+
return f;
141+
}
142+
catch (Exception ex)
143+
{
144+
LogRetrieveMultiple(null, request, ex);
145+
throw new ServiceStorageException("Error retrieving items ", ex);
146+
}
147+
}
148+
149+
150+
}
151+
}

MZBase.EntityFrameworkCore/LDRComatibleRepositoryAsync.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,36 @@
33
using MZBase.Infrastructure;
44
using MZSimpleDynamicLinq.Core;
55
using MZSimpleDynamicLinq.EFCoreExtensions;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
116

127
namespace MZBase.EntityFrameworkCore
138
{
14-
public class LDRCompatibleRepositoryAsync<DBModelEntity,DomainModelEntity, PrimaryKeyType> : RepositoryAsync<DBModelEntity, DomainModelEntity, PrimaryKeyType>, ILDRCompatibleRepositoryAsync<DomainModelEntity, PrimaryKeyType>
9+
/// <summary>
10+
/// Provides an asynchronous repository implementation that is compatible with LinqDataRequest handling.
11+
/// </summary>
12+
/// <typeparam name="DBModelEntity">The type of the database model entity.</typeparam>
13+
/// <typeparam name="DomainModelEntity">The type of the domain model entity.</typeparam>
14+
/// <typeparam name="PrimaryKeyType">The type of the primary key.</typeparam>
15+
public class LDRCompatibleRepositoryAsync<DomainModelEntity, DBModelEntity, PrimaryKeyType> : RepositoryAsync<DomainModelEntity, DBModelEntity, PrimaryKeyType>, ILDRCompatibleRepositoryAsync<DomainModelEntity, DBModelEntity, PrimaryKeyType>
1516
where DomainModelEntity : Model<PrimaryKeyType>
16-
where DBModelEntity : DomainModelEntity
17+
where DBModelEntity : DomainModelEntity, IConvertibleDBModelEntity<DomainModelEntity>, new()
1718
where PrimaryKeyType : struct
1819
{
19-
public LDRCompatibleRepositoryAsync(DbContext context):base(context)
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="LDRCompatibleRepositoryAsync{DBModelEntity, DomainModelEntity, PrimaryKeyType}"/> class.
22+
/// </summary>
23+
/// <param name="context">The database context.</param>
24+
public LDRCompatibleRepositoryAsync(DbContext context) : base(context)
2025
{
21-
2226
}
27+
28+
/// <summary>
29+
/// Gets all items asynchronously based on the specified LinqDataRequest.
30+
/// </summary>
31+
/// <param name="request">The LinqDataRequest containing the query parameters.</param>
32+
/// <returns>A task that represents the asynchronous operation. The task result contains the LinqDataResult of the domain model entities.</returns>
2333
public virtual async Task<LinqDataResult<DomainModelEntity>> AllItemsAsync(LinqDataRequest request)
2434
{
25-
return await _context.Set<DBModelEntity>().ToLinqDataResultAsync< DomainModelEntity>(request.Take, request.Skip, request.Sort, request.Filter);
35+
return await _context.Set<DBModelEntity>().ToLinqDataResultAsync<DomainModelEntity>(request.Take, request.Skip, request.Sort, request.Filter);
2636
}
2737
}
2838
}

MZBase.EntityFrameworkCore/MZBase.EntityFrameworkCore.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<Version>1.0.5</Version>
7+
<Version>2.0.0</Version>
88
<AssemblyVersion>1.0.1.1</AssemblyVersion>
99
<Authors>Mahdi Zandakbari</Authors>
1010
<Description>An EFCore implementation of some of the more common interfaces of MZBase.Infrustructure.
@@ -15,8 +15,8 @@ Different repository implementations are included.</Description>
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.6" />
19-
<PackageReference Include="MZSimpleDynamicLinq.EFCoreExtensions" Version="1.0.1" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
19+
<PackageReference Include="MZSimpleDynamicLinq.EFCoreExtensions" Version="1.0.2" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

MZBase.EntityFrameworkCore/RepositoryAsync.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
using Microsoft.EntityFrameworkCore;
22
using MZBase.Domain;
33
using MZBase.Infrastructure;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
74
using System.Linq.Expressions;
8-
using System.Text;
9-
using System.Threading.Tasks;
105

116
namespace MZBase.EntityFrameworkCore
127
{
13-
public abstract class RepositoryAsync<DBModelEntity,DomainModelEntity, PrimaryKeyType> : IRepositoryAsync<DomainModelEntity, PrimaryKeyType>
14-
where DomainModelEntity : Model<PrimaryKeyType>
15-
where DBModelEntity: DomainModelEntity
16-
where PrimaryKeyType : struct
8+
public abstract class RepositoryAsync<DomainModelEntity, DBModelEntity, PrimaryKeyType> : IRepositoryAsync<DomainModelEntity, DBModelEntity, PrimaryKeyType>
9+
where DomainModelEntity : Model<PrimaryKeyType>
10+
where DBModelEntity : DomainModelEntity, IConvertibleDBModelEntity<DomainModelEntity>, new()
11+
where PrimaryKeyType : struct
1712
{
1813
protected readonly DbSet<DBModelEntity> _entities;
1914
protected readonly DbContext _context;
@@ -23,30 +18,28 @@ public RepositoryAsync(DbContext context)
2318
_context = context;
2419
}
2520
public virtual async Task<IReadOnlyList<DomainModelEntity>> AllItemsAsync() => await _entities.ToListAsync();
26-
public virtual Task<DomainModelEntity?> FirstOrDefaultAsync(Expression<Func<DomainModelEntity, bool>> predicate) => _entities.FirstOrDefaultAsync(predicate);
27-
public virtual async Task DeleteAsync(DomainModelEntity item)
21+
public virtual Task<DBModelEntity?> FirstOrDefaultAsync(Expression<Func<DBModelEntity, bool>> predicate) => _entities.FirstOrDefaultAsync(predicate);
22+
public virtual async void Delete(DBModelEntity item)
2823
{
29-
if (item is DBModelEntity)
30-
{
31-
_entities.Remove(item as DBModelEntity);
32-
}
33-
24+
_entities.Remove(item);
3425
}
35-
public virtual DomainModelEntity Insert(DomainModelEntity item)
26+
public virtual DBModelEntity Insert(DomainModelEntity item)
3627
{
37-
if (item is DBModelEntity)
28+
if (item is DBModelEntity ent)
3829
{
39-
return _entities.Add(item as DBModelEntity).Entity;
30+
return _entities.Add(ent).Entity;
4031
}
4132
else
4233
{
43-
return default(DomainModelEntity);
34+
DBModelEntity dbEntityObject = new DBModelEntity();
35+
dbEntityObject.SetFieldsFromDomainModel(item);
36+
return _entities.Add(dbEntityObject).Entity;
4437
}
4538
}
4639

47-
public virtual async Task<DomainModelEntity?> GetByIdAsync(PrimaryKeyType id) => await _entities.FindAsync(id);
40+
public virtual async Task<DBModelEntity?> GetByIdAsync(PrimaryKeyType id) => await _entities.FindAsync(id);
4841

49-
public virtual async Task UpdateAsync(DomainModelEntity item)
42+
public virtual void Update(DBModelEntity item)
5043
{
5144
_context.Entry(item).State = EntityState.Modified;
5245
return;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MZBase.Infrastructure
2+
{
3+
public interface IConvertibleDBModelEntity<DomainModelEntity> where DomainModelEntity : class
4+
{
5+
/// <summary>
6+
/// Set the fields of the database model entity from the domain model entity.
7+
/// </summary>
8+
/// <param name="domainModelEntity"></param>
9+
void SetFieldsFromDomainModel(DomainModelEntity domainModelEntity);
10+
/// <summary>
11+
/// Get the domain model entity instance from the database model entity.
12+
/// </summary>
13+
/// <returns></returns>
14+
DomainModelEntity GetDomainObject();
15+
}
16+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
using MZBase.Domain;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Text;
52

63
namespace MZBase.Infrastructure
74
{
8-
public interface IDynamicTestableUnitOfWorkAsync:IUnitOfWorkAsync
5+
public interface IDynamicTestableUnitOfWorkAsync : IUnitOfWorkAsync
96
{
10-
ILDRCompatibleRepositoryAsync<T, PrimKey> GetRepo<T, PrimKey>()
11-
where T : Model<PrimKey>
7+
ILDRCompatibleRepositoryAsync<DomainModel, DBModelEntity, PrimKey> GetRepo<DomainModel, DBModelEntity, PrimKey>()
8+
where DomainModel : Model<PrimKey>
9+
where DBModelEntity : DomainModel, IConvertibleDBModelEntity<DomainModel>, new()
1210
where PrimKey : struct;
1311
}
1412
}

MZBase.Infrastructure/ILDRComatibleRepositoryAsync.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
using MZBase.Domain;
22
using MZSimpleDynamicLinq.Core;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
63
using System.Threading.Tasks;
74

85
namespace MZBase.Infrastructure
96
{
107
/// <summary>
11-
/// Ripository interface compatible with handling LinqDataRequest
8+
/// Repository interface compatible with handling LinqDataRequest
129
/// </summary>
1310
/// <typeparam name="ModelItem"></typeparam>
1411
/// <typeparam name="T"></typeparam>
15-
public interface ILDRCompatibleRepositoryAsync<ModelItem, T>: IRepositoryAsync<ModelItem, T>
12+
public interface ILDRCompatibleRepositoryAsync<ModelItem, DBModelEntity, T> : IRepositoryAsync<ModelItem, DBModelEntity, T>
1613
where ModelItem : Model<T>
14+
where DBModelEntity : ModelItem, IConvertibleDBModelEntity<ModelItem>, new()
1715
where T : struct
1816
{
1917
Task<LinqDataResult<ModelItem>> AllItemsAsync(LinqDataRequest request);

0 commit comments

Comments
 (0)