Skip to content

Commit ec4493b

Browse files
committed
feat: rename Add and Delete
1 parent 47948e9 commit ec4493b

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

src/Cnblogs.Architecture.Ddd.Domain.Abstractions/IUnitOfWork.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
/// <summary>
44
/// 定义 UnitOfWork。
55
/// </summary>
6+
/// <typeparam name="TEntity">实体类型。</typeparam>
7+
/// <typeparam name="TKey">主键类型。</typeparam>
68
public interface IUnitOfWork<TEntity, TKey>
79
where TEntity : EntityBase, IAggregateRoot
810
where TKey : IComparable<TKey>
@@ -12,15 +14,15 @@ public interface IUnitOfWork<TEntity, TKey>
1214
/// </summary>
1315
/// <param name="key">主键。</param>
1416
/// <returns>获取到的实体。</returns>
15-
Task<TEntity?> FindAsync(TKey key);
17+
Task<TEntity?> GetAsync(TKey key);
1618

1719
/// <summary>
1820
/// 添加实体,调用 <see cref="SaveEntitiesAsync"/> 或 <see cref="SaveChangesAsync"/> 后才会写入数据库。
1921
/// </summary>
2022
/// <param name="entity">要添加实体。</param>
2123
/// <typeparam name="TEntity">实体类型。</typeparam>
2224
/// <returns>被添加的实体。</returns>
23-
TEntity Insert(TEntity entity);
25+
TEntity Add(TEntity entity);
2426

2527
/// <summary>
2628
/// 更新实体,调用 <see cref="SaveEntitiesAsync"/> 或 <see cref="SaveChangesAsync"/> 后才会写入数据库。
@@ -36,7 +38,7 @@ public interface IUnitOfWork<TEntity, TKey>
3638
/// <param name="entity">要删除的实体。</param>
3739
/// <typeparam name="TEntity">实体类型。</typeparam>
3840
/// <returns></returns>
39-
TEntity Remove(TEntity entity);
41+
TEntity Delete(TEntity entity);
4042

4143
/// <summary>
4244
/// 提交所有更改(但不发布领域事件)。

src/Cnblogs.Architecture.Ddd.Infrastructure.EntityFramework/BaseRepository.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,7 @@ public async Task<TEntity> DeleteAsync(TEntity entity)
101101
}
102102

103103
/// <inheritdoc />
104-
public Task<TEntity?> FindAsync(TKey key)
105-
{
106-
return GetAsync(key);
107-
}
108-
109-
/// <inheritdoc />
110-
public TEntity Insert(TEntity entity)
104+
public TEntity Add(TEntity entity)
111105
{
112106
Context.Add(entity);
113107
return entity;
@@ -121,7 +115,7 @@ public TEntity Update(TEntity entity)
121115
}
122116

123117
/// <inheritdoc />
124-
public TEntity Remove(TEntity entity)
118+
public TEntity Delete(TEntity entity)
125119
{
126120
Context.Remove(entity);
127121
return entity;

src/Cnblogs.Architecture.Ddd.Infrastructure.MongoDb/MongoBaseRepository.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,7 @@ private async Task DispatchDomainEventsAsync(IReadOnlyCollection<IDomainEvent>?
151151
private static void EnsureBeforeUpdateCalled(TEntity entity) => entity.BeforeUpdate();
152152

153153
/// <inheritdoc />
154-
public Task<TEntity?> FindAsync(TKey key)
155-
{
156-
return GetAsync(key);
157-
}
158-
159-
/// <inheritdoc />
160-
public TEntity Insert(TEntity entity)
154+
public TEntity Add(TEntity entity)
161155
{
162156
if (_toDelete != null && _toDelete.ContainsKey(entity.Id))
163157
{
@@ -206,7 +200,7 @@ public TEntity Update(TEntity entity)
206200
}
207201

208202
/// <inheritdoc />
209-
public TEntity Remove(TEntity entity)
203+
public TEntity Delete(TEntity entity)
210204
{
211205
if (_toAdd != null && _toAdd.ContainsKey(entity.Id))
212206
{

test/Cnblogs.Architecture.UnitTests/Infrastructure/MongoDb/MongoBaseRepositoryTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public async Task Uow_Insert_CommitAsync()
133133

134134
// Act
135135
var uow = repository.UnitOfWork;
136-
uow.Insert(blog);
136+
uow.Add(blog);
137137
var response = await uow.SaveEntitiesAsync();
138138

139139
// Assert
@@ -164,8 +164,8 @@ public async Task Uow_InsertThenRemove_NoChangeAsync()
164164

165165
// Act
166166
var uow = repository.UnitOfWork;
167-
uow.Insert(blog);
168-
uow.Remove(blog);
167+
uow.Add(blog);
168+
uow.Delete(blog);
169169
var response = await uow.SaveEntitiesAsync();
170170

171171
// Assert
@@ -193,8 +193,8 @@ public async Task Uow_DeleteThenInsert_UpdateAsync()
193193

194194
// Act
195195
var uow = repository.UnitOfWork;
196-
uow.Remove(blog);
197-
uow.Insert(blog);
196+
uow.Delete(blog);
197+
uow.Add(blog);
198198
var response = await uow.SaveEntitiesAsync();
199199

200200
// Assert
@@ -222,7 +222,7 @@ public void Uow_DeleteThenUpdate_Throw()
222222

223223
// Act
224224
var uow = repository.UnitOfWork;
225-
uow.Remove(blog);
225+
uow.Delete(blog);
226226
var act = () => uow.Update(blog);
227227

228228
// Assert
@@ -240,7 +240,7 @@ public void Uow_UpdateThenInsert_Throw()
240240
// Act
241241
var uow = repository.UnitOfWork;
242242
uow.Update(blog);
243-
var act = () => uow.Insert(blog);
243+
var act = () => uow.Add(blog);
244244

245245
// Assert
246246
act.Should().ThrowExactly<InvalidOperationException>();
@@ -256,7 +256,7 @@ public async Task Uow_DeleteWithDomainEvent_CommitAsync()
256256

257257
// Act
258258
var uow = repository.UnitOfWork;
259-
uow.Remove(blog);
259+
uow.Delete(blog);
260260
var response = await uow.SaveEntitiesAsync();
261261

262262
// Assert

0 commit comments

Comments
 (0)