Skip to content

Commit 4a42537

Browse files
committed
feat: add Specification
1 parent 08dc3f1 commit 4a42537

8 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎src/DDD/MASA.BuildingBlocks.DDD.Domain/MASA.BuildingBlocks.DDD.Domain.csproj‎

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

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>

src/DDD/MASA.BuildingBlocks.DDD.Domain/Repositories/RepositoryBase.cs renamed to src/DDD/MASA.BuildingBlocks.DDD.Domain/Repositories/BaseRepository.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace MASA.BuildingBlocks.DDD.Domain.Repositories;
2-
public abstract class RepositoryBase<TEntity> : IRepository<TEntity>, IUnitOfWork
3-
where TEntity : class, IEntity
2+
public abstract class BaseRepository<TEntity> : IRepository<TEntity>, IUnitOfWork
3+
where TEntity : class, IAggregateRoot
44
{
55
#region IRepository<TEntity>
66

@@ -102,6 +102,10 @@ public async Task<PaginatedList<TEntity>> GetPaginatedListAsync(Expression<Func<
102102

103103
public bool DisableRollbackOnFailure { get; set; }
104104

105+
public abstract DbTransaction Transaction { get; }
106+
107+
public abstract IUnitOfWork UnitOfWork { get; }
108+
105109
public abstract Task CommitAsync(CancellationToken cancellationToken = default);
106110

107111
public abstract ValueTask DisposeAsync();
@@ -110,5 +114,7 @@ public async Task<PaginatedList<TEntity>> GetPaginatedListAsync(Expression<Func<
110114

111115
public abstract Task SaveChangesAsync(CancellationToken cancellationToken = default);
112116

117+
public abstract Task<DbTransaction> BeginTransaction(CancellationToken cancellationToken = default);
118+
113119
#endregion
114120
}

‎src/DDD/MASA.BuildingBlocks.DDD.Domain/Repositories/IRepository.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace MASA.BuildingBlocks.DDD.Domain.Repositories;
2-
public interface IRepository<TAggregateRoot>
3-
where TAggregateRoot : class, IAggregateRoot
2+
public interface IRepository<TEntity>
3+
where TEntity : class, IAggregateRoot
44
{
55
IUnitOfWork UnitOfWork { get; }
66

‎src/DDD/MASA.BuildingBlocks.DDD.Domain/SeedWork/Enumeration.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
namespace MASA.BuildingBlocks.DDD.Domain.SeedWork;
2+
/// <summary>
3+
/// Reference from https://docs.microsoft.com/zh-cn/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types
4+
/// </summary>
25
public abstract class Enumeration : IComparable
36
{
47
public string Name { get; private set; }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MASA.BuildingBlocks.DDD.Domain.Specifications;
2+
public abstract class BaseSpecification<T> : ISpecification<T>
3+
{
4+
public BaseSpecification(Expression<Func<T, bool>> whereExpression)
5+
{
6+
WhereExpression = whereExpression;
7+
}
8+
public Expression<Func<T, bool>> WhereExpression { get; }
9+
10+
public List<Expression<Func<T, object>>> Includes { get; } =
11+
new List<Expression<Func<T, object>>>();
12+
13+
public List<string> IncludeStrings { get; } = new List<string>();
14+
15+
public abstract bool IsSatisfiedBy(T obj);
16+
17+
protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
18+
{
19+
Includes.Add(includeExpression);
20+
}
21+
22+
protected virtual void AddInclude(string includeString)
23+
{
24+
IncludeStrings.Add(includeString);
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MASA.BuildingBlocks.DDD.Domain.Specifications;
2+
/// <summary>
3+
/// Reference from
4+
/// https://docs.microsoft.com/zh-cn/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-implementation-entity-framework-core
5+
/// https://martinfowler.com/apsupp/spec.pdf
6+
/// </summary>
7+
public interface ISpecification<T>
8+
{
9+
bool IsSatisfiedBy(T obj);
10+
11+
Expression<Func<T, bool>> WhereExpression { get; }
12+
13+
List<Expression<Func<T, object>>> Includes { get; }
14+
15+
List<string> IncludeStrings { get; }
16+
}

‎src/DDD/MASA.BuildingBlocks.DDD.Domain/_Imports.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
global using MASA.BuildingBlocks.Dispatcher.Remoting;
77
global using MASA.BuildingBlocks.ReadWriteSpliting.CQRS.Commands;
88
global using MASA.BuildingBlocks.ReadWriteSpliting.CQRS.Queries;
9+
global using System.Data.Common;
910
global using System.Linq.Expressions;
1011
global using System.Reflection;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace MASA.BuildingBlocks.Data.Uow;
22
public interface ITransaction
33
{
4-
DbTransaction Transaction { get; set; }
4+
DbTransaction Transaction { get; }
55
}

0 commit comments

Comments
 (0)