Skip to content

Commit bde66e3

Browse files
committed
feat: allow set order by string config concurrently
1 parent 862b933 commit bde66e3

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/Cnblogs.Architecture.Ddd.Cqrs.Abstractions/PageableQueryHandlerBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task<PagedList<TView>> Handle(TQuery request, CancellationToken can
3232
if (request.PagingParams.PageSize == 0 || totalCount == 0)
3333
{
3434
// need count only or no available item, short circuit here.
35-
return new PagedList<TView>(Array.Empty<TView>(), request.PagingParams, totalCount);
35+
return new PagedList<TView>([], request.PagingParams, totalCount);
3636
}
3737

3838
ordered = ordered.Paging(request.PagingParams);
@@ -95,4 +95,4 @@ protected virtual IQueryable<TView> ProjectToView(TQuery query, IQueryable<TEnti
9595
/// <param name="queryable">Projected <see cref="IQueryable{T}" />.</param>
9696
/// <returns>The query result.</returns>
9797
protected abstract Task<List<TView>> ToListAsync(TQuery query, IQueryable<TView> queryable);
98-
}
98+
}

src/Cnblogs.Architecture.Ddd.Infrastructure.Abstractions/OrderBySegmentConfig.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Collections.Concurrent;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Linq.Expressions;
34

45
namespace Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
@@ -8,7 +9,7 @@ namespace Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
89
/// </summary>
910
public static class OrderBySegmentConfig
1011
{
11-
private static readonly Dictionary<Type, Dictionary<string, OrderBySegment>> Cache = new();
12+
private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, OrderBySegment>> Cache = new();
1213

1314
/// <summary>
1415
/// 注册新的可排序列。
@@ -24,7 +25,7 @@ public static void RegisterSortableProperty<TSource, TProperty>(
2425
var sourceType = typeof(TSource);
2526
if (Cache.ContainsKey(sourceType) == false)
2627
{
27-
Cache[sourceType] = new Dictionary<string, OrderBySegment>(StringComparer.OrdinalIgnoreCase);
28+
Cache[sourceType] = new ConcurrentDictionary<string, OrderBySegment>(StringComparer.OrdinalIgnoreCase);
2829
}
2930

3031
Cache[sourceType][name] = new OrderBySegment(false, exp);
@@ -73,4 +74,24 @@ public static bool TryParseOrderBySegments<T>(
7374

7475
return segments.Count > 0;
7576
}
76-
}
77+
}
78+
79+
/// <summary>
80+
/// 管理可排序列的映射。
81+
/// </summary>
82+
/// <typeparam name="TEntity">The entity to config.</typeparam>
83+
public static class OrderBySegmentConfig<TEntity>
84+
{
85+
/// <summary>
86+
/// 注册新的可排序列。
87+
/// </summary>
88+
/// <param name="name">列名。</param>
89+
/// <param name="exp">属性表达式。</param>
90+
/// <typeparam name="TProperty">属性类型。</typeparam>
91+
public static void RegisterSortableProperty<TProperty>(
92+
string name,
93+
Expression<Func<TEntity, TProperty>> exp)
94+
{
95+
OrderBySegmentConfig.RegisterSortableProperty(name, exp);
96+
}
97+
}

0 commit comments

Comments
 (0)