Updated Unit of Work (markdown)

AlexLEWIS
2021-09-08 11:06:13 +08:00
parent 2f67d3257f
commit c5a204702e

@@ -43,14 +43,14 @@ The `uow.GetOrBeginTransaction()` method can get the transaction object.
public interface IUnitOfWork : IDisposable public interface IUnitOfWork : IDisposable
{ {
/// <summary> /// <summary>
/// 该对象 Select/Delete/Insert/Update/InsertOrUpdate 与工作单元事务保持一致,可省略传递 WithTransaction /// The object Select/Delete/Insert/Update/InsertOrUpdate is consistent with the unit of work transaction and can be omitted to pass WithTransaction
/// </summary> /// </summary>
IFreeSql Orm { get; } IFreeSql Orm { get; }
/// <summary> /// <summary>
/// 开启事务,或者返回已开启的事务 /// Open the transaction, or return to the opened transaction
/// </summary> /// </summary>
/// <param name="isCreate">若未开启事务,则开启</param> /// <param name="isCreate">If the transaction is not opened, then open</param>
/// <returns></returns> /// <returns></returns>
DbTransaction GetOrBeginTransaction(bool isCreate = true); DbTransaction GetOrBeginTransaction(bool isCreate = true);
@@ -61,15 +61,15 @@ public interface IUnitOfWork : IDisposable
void Rollback(); void Rollback();
/// <summary> /// <summary>
/// 工作单元内的实体变化跟踪 /// Entity change tracking within the unit of work
/// </summary> /// </summary>
DbContext.EntityChangeReport EntityChangeReport { get; } DbContext.EntityChangeReport EntityChangeReport { get; }
} }
``` ```
## 实体变化事件 ## Entity Changing Event
全局设置: Global Settings:
```csharp ```csharp
fsql.SetDbContextOptions(opt => { fsql.SetDbContextOptions(opt => {
@@ -79,7 +79,7 @@ fsql.SetDbContextOptions(opt => {
}); });
``` ```
单独设置: Individual settings:
```csharp ```csharp
var uow = fsql.CreateUnitOfWork(); var uow = fsql.CreateUnitOfWork();
@@ -88,26 +88,26 @@ uow.OnEntityChange = report => {
}; };
``` ```
参数 report 是一个 List 集合,集合元素的类型定义如下: The parameter `report` is a list collection, and the type of the collection elements is defined as follows:
```csharp ```csharp
public class ChangeInfo { public class ChangeInfo {
public object Object { get; set; } public object Object { get; set; }
public EntityChangeType Type { get; set; } public EntityChangeType Type { get; set; }
/// <summary> /// <summary>
/// Type = Update 的时候,获取更新之前的对象 /// When Type = Update, get the object before the update
/// </summary> /// </summary>
public object BeforeObject { get; set; } public object BeforeObject { get; set; }
} }
public enum EntityChangeType { Insert, Update, Delete, SqlRaw } public enum EntityChangeType { Insert, Update, Delete, SqlRaw }
``` ```
| 变化类型 | 说明 | | Type of Change | Description |
| -- | -- | | -- | -- |
| Insert | 实体对象被插入 | | Insert | The entity object is inserted |
| Update | 实体对象被更新 | | Update | The entity object is updated |
| Delete | 实体对象被删除 | | Delete | The entity object is deleted |
| SqlRaw | 执行了SQL语句 | | SqlRaw | SQL statement executed |
SqlRaw 目前有两处地方比较特殊: SqlRaw 目前有两处地方比较特殊:
- 多对多联级更新导航属性的时候,对中间表的全部删除操作; - 多对多联级更新导航属性的时候,对中间表的全部删除操作;
@@ -118,12 +118,12 @@ int Delete(Expression<Func<TEntity, bool>> predicate);
DbContext.SaveChanges或者 Repository 对实体的 Insert/Update/Delete或者 UnitOfWork.Commit 操作都会最多触发一次该事件。 DbContext.SaveChanges或者 Repository 对实体的 Insert/Update/Delete或者 UnitOfWork.Commit 操作都会最多触发一次该事件。
## 参考资料 ## Reference
- [租户》](%e7%a7%9f%e6%88%b7) - [Tenant》](Tenant)
- [读写分离》](%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) - [Using Read/Write Separation》](Using-Read-Write-Separation)
- [分表、分库》](%e5%88%86%e8%a1%a8%e5%88%86%e5%ba%93) - [Sharding Tables and Database》](Sharding-Tables-and-Database)
- [仓储层Repository》](Repository) - [《Repository Layer](Repository-Layer)
- [过滤器、全局过滤器》](%e8%bf%87%e6%bb%a4%e5%99%a8) - [Filters and Global Filters》](Filters-and-Global-Filters)
- [《AOP》](AOP) - [《AOP》](Aspect-Oriented-Programming)
- [《DbContext》](DbContext) - [《DbContext》](Batabase-Context)