Skip to content

Commit 31a34f2

Browse files
committed
[增加]1. 增加数据库查询函数的自动创建参数
1 parent 779f879 commit 31a34f2

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

GameFrameX.DataBase/Abstractions/IDatabaseService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ public interface IDatabaseService
2424
/// </summary>
2525
/// <param name="id">数据的唯一ID</param>
2626
/// <param name="filter">查询条件</param>
27+
/// <param name="isCreateIfNotExists">是否创建不存在的文档</param>
2728
/// <typeparam name="TState">实现ICacheState接口的类型。</typeparam>
2829
/// <returns>返回符合条件的数据对象</returns>
29-
Task<TState> FindAsync<TState>(long id, Expression<Func<TState, bool>> filter = null) where TState : BaseCacheState, new();
30+
Task<TState> FindAsync<TState>(long id, Expression<Func<TState, bool>> filter = null, bool isCreateIfNotExists = true) where TState : BaseCacheState, new();
3031

3132
/// <summary>
3233
/// 查询单条数据
3334
/// </summary>
3435
/// <param name="filter">查询条件</param>
36+
/// <param name="isCreateIfNotExists">是否创建不存在的文档</param>
3537
/// <typeparam name="TState">实现ICacheState接口的类型。</typeparam>
3638
/// <returns>返回符合条件的数据对象</returns>
37-
Task<TState> FindAsync<TState>(Expression<Func<TState, bool>> filter) where TState : BaseCacheState, new();
39+
Task<TState> FindAsync<TState>(Expression<Func<TState, bool>> filter, bool isCreateIfNotExists = true) where TState : BaseCacheState, new();
3840

3941
/// <summary>
4042
/// 查询数据

GameFrameX.DataBase/GameDB.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ public static void Close()
136136
/// <typeparam name="TState">文档的类型,必须继承自BaseCacheState且有无参构造函数</typeparam>
137137
/// <param name="id">要查找的文档ID</param>
138138
/// <param name="filter">可选的附加过滤条件</param>
139+
/// <param name="isCreateIfNotExists">是否创建不存在的文档</param>
139140
/// <returns>找到的文档,如果不存在则返回新的空文档</returns>
140-
public static Task<TState> FindAsync<TState>(long id, Expression<Func<TState, bool>> filter = null) where TState : BaseCacheState, new()
141+
public static Task<TState> FindAsync<TState>(long id, Expression<Func<TState, bool>> filter = null, bool isCreateIfNotExists = true) where TState : BaseCacheState, new()
141142
{
142143
ArgumentNullException.ThrowIfNull(_dbServiceImplementation, nameof(_dbServiceImplementation));
143-
return _dbServiceImplementation.FindAsync(id, filter);
144+
return _dbServiceImplementation.FindAsync(id, filter, isCreateIfNotExists);
144145
}
145146

146147
/// <summary>
@@ -149,11 +150,12 @@ public static void Close()
149150
/// </summary>
150151
/// <typeparam name="TState">文档的类型,必须继承自BaseCacheState</typeparam>
151152
/// <param name="filter">用于筛选文档的Lambda表达式</param>
153+
/// <param name="isCreateIfNotExists">是否创建不存在的文档</param>
152154
/// <returns>找到的第一个匹配文档,如果没有匹配项则返回null</returns>
153-
public static Task<TState> FindAsync<TState>(Expression<Func<TState, bool>> filter) where TState : BaseCacheState, new()
155+
public static Task<TState> FindAsync<TState>(Expression<Func<TState, bool>> filter, bool isCreateIfNotExists = true) where TState : BaseCacheState, new()
154156
{
155157
ArgumentNullException.ThrowIfNull(_dbServiceImplementation, nameof(_dbServiceImplementation));
156-
return _dbServiceImplementation.FindAsync(filter);
158+
return _dbServiceImplementation.FindAsync(filter, isCreateIfNotExists);
157159
}
158160

159161
/// <summary>

GameFrameX.DataBase/Mongo/MongoDbService.Query.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ public sealed partial class MongoDbService
2323
/// <typeparam name="TState">缓存状态的类型,必须是BaseCacheState的子类,并具有无参数构造函数。</typeparam>
2424
/// <param name="id">要加载的缓存状态的ID。</param>
2525
/// <param name="filter">可选的过滤器,用于进一步限制查询结果的条件。</param>
26+
/// <param name="isCreateIfNotExists">是否创建不存在的文档</param>
2627
/// <returns>加载的缓存状态,如果未找到则返回新创建的状态。</returns>
27-
public async Task<TState> FindAsync<TState>(long id, Expression<Func<TState, bool>> filter = null) where TState : BaseCacheState, new()
28+
public async Task<TState> FindAsync<TState>(long id, Expression<Func<TState, bool>> filter = null, bool isCreateIfNotExists = true) where TState : BaseCacheState, new()
2829
{
2930
var findExpression = GetDefaultFindExpression(filter);
3031
var state = await _mongoDbContext.Find<TState>().Match(findExpression).OneAsync(id);
32+
if (!isCreateIfNotExists)
33+
{
34+
return state;
35+
}
36+
3137
var isNew = state == null;
3238

3339
if (state == null)
@@ -38,6 +44,7 @@ public sealed partial class MongoDbService
3844

3945
// 调用后处理方法以加载状态的其他数据
4046
state.LoadFromDbPostHandler(isNew);
47+
4148
return state;
4249
}
4350

@@ -47,11 +54,18 @@ public sealed partial class MongoDbService
4754
/// </summary>
4855
/// <typeparam name="TState">缓存状态的类型,必须是BaseCacheState的子类,并具有无参数构造函数。</typeparam>
4956
/// <param name="filter">查询条件,用于限制查找的结果。</param>
57+
/// <param name="isCreateIfNotExists">是否创建不存在的文档</param>
5058
/// <returns>满足条件的缓存状态,如果未找到则返回新创建的状态。</returns>
51-
public async Task<TState> FindAsync<TState>(Expression<Func<TState, bool>> filter) where TState : BaseCacheState, new()
59+
public async Task<TState> FindAsync<TState>(Expression<Func<TState, bool>> filter, bool isCreateIfNotExists = true) where TState : BaseCacheState, new()
5260
{
5361
var findExpression = GetDefaultFindExpression(filter);
5462
var state = await _mongoDbContext.Queryable<TState>().Where(findExpression).SingleOrDefaultAsync();
63+
64+
if (!isCreateIfNotExists)
65+
{
66+
return state;
67+
}
68+
5569
var isNew = state == null;
5670

5771
if (state == null)
@@ -62,6 +76,7 @@ public sealed partial class MongoDbService
6276

6377
// 调用后处理方法以加载状态的其他数据
6478
state.LoadFromDbPostHandler(isNew);
79+
6580
return state;
6681
}
6782

0 commit comments

Comments
 (0)