1. 簡介
工作單元:維護受事務影響的對象列表,並協調對象改變的持久化和解決並發場景的問題
- 在 EntityFrameworkCore 中使用
DbContext封裝了:- 實體對象狀態記錄跟蹤
- 數據庫的交互
- 數據庫事務
- 關於協調對象改變的持久化是通過調用
DbContext的相關方法實現的 - 在並發場景下
DbContext的使用也完全交給了開發者處理,主要靠文檔規范說明DbContext的使用。
2. DbContext 生命周期和使用規范
2.1. 生命周期
DbContext 的生命周期從創建實例時開始,並在釋放實例時結束。 DbContext 實例旨在用於單個工作單元。 這意味着 DbContext 實例的生命周期通常很短。
使用 Entity Framework Core (EF Core) 時的典型工作單元包括:
- 創建 DbContext 實例
- 根據上下文跟蹤實體實例。 實體將在以下情況下被跟蹤
- 查詢返回時
- 添加或附加到
DbContext
- 根據需要對所跟蹤的實體進行更改以實現業務規則
- 調用
SaveChanges或SaveChangesAsync- EF Core 將檢測所做的更改,並將這些更改寫入數據庫。
- 釋放
DbContext實例
2.2. 使用規范
- 使用后釋放
DbContext非常重要。 這可確保釋放所有非托管資源,並注銷任何事件或其他鈎子(hooks),以防止實例在保持引用時出現內存泄漏。 - DbContext 不是線程安全的。 不要在線程之間共享
DbContext。 請確保在繼續使用DbContext實例之前,等待所有異步調用。 - EF Core 代碼引發的
InvalidOperationException可以使DbContext進入不可恢復的狀態。- 此類異常指示程序錯誤,並且不應該從其中恢復。
2.3. 避免 DbContext 線程處理問題
- Entity Framework Core 不支持在同一
DbContext實例上運行多個並行操作。- 這包括異步查詢的並行執行以及從多個線程進行的任何顯式並發使用。
- 因此,始終
await異步調用,或對並行執行的操作使用單獨的 DbContext 實例。 - 當 EF Core 檢測到嘗試同時使用
DbContext實例時,將會拋出異常InvalidOperationException,其中包含類:- 在上一個操作完成之前,第二個操作已在此
DbContext中啟動。- 使用同一個
DbContext實例的不同線程不保證實例成員是線程安全的,因此拋出此異常。
- 使用同一個
- 在上一個操作完成之前,第二個操作已在此
如果框架沒檢測到並發訪問,可能會導致不可預知的行為:應用程序崩潰、數據損壞等
並發訪問 DbContext 實例的常見情況:
-
異步操作缺陷
- 使用異步方法,EF Core 可以啟動以非阻塞式訪問數據庫的操作。 但是,如果調用方不等待其中一個方法完成,而是繼續對
DbContext執行其他操作,則DbContext的狀態可能會(並且很可能會)損壞。
- 使用異步方法,EF Core 可以啟動以非阻塞式訪問數據庫的操作。 但是,如果調用方不等待其中一個方法完成,而是繼續對
-
通過依賴注入隱式共享 DbContext 實例
- 默認情況下
AddDbContext擴展方法使用有范圍的生命周期來注冊DbContext類型。 - 這樣可以避免在大多數 ASP.NET Core 應用程序中出現並發訪問問題
- 因為在給定時間內只有一個線程在執行每個客戶端請求,並且每個請求都有單獨的依賴注入范圍(dependency injection scope)(因此有單獨的
DbContext實例)。 - 對於 Blazor Server 托管模型,一個邏輯請求用來維護 Blazor 用戶線路,因此,如果使用默認注入范圍,則每個用戶線路只能提供一個范圍內的 DbContext 實例。
- 因為在給定時間內只有一個線程在執行每個客戶端請求,並且每個請求都有單獨的依賴注入范圍(dependency injection scope)(因此有單獨的
- 默認情況下
-
建議
- 任何顯式的並行執行多個線程的代碼都應確保
DbContext實例不會同時訪問。 - 使用依賴注入可以通過以下方式實現:
- 將
DbContext注冊為范圍的,並為每個線程創建范圍的服務提供實例(使用 IServiceScopeFactory) - 將
DbContext注冊為瞬時的(transient)- 程序初始化時使用具有
ServiceLifetime參數的AddDbContext方法的重載
- 程序初始化時使用具有
- 將
- 任何顯式的並行執行多個線程的代碼都應確保
引用:
- https://docs.microsoft.com/zh-cn/ef/core/dbcontext-configuration/#the-dbcontext-lifetime
- https://docs.microsoft.com/zh-cn/ef/core/dbcontext-configuration/#avoiding-dbcontext-threading-issues
- https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1#entity-framework-contexts-1
3. 封裝-工作單元
上面的內容來源於官方文檔,結合 使用規范 和 建議 的要求開始進行設計和封裝。
首先聲明:
其實DbContext的設計是很棒的,對於使用者更自由,更開放。
本文分享的關於工作單元的設計和封裝是針對我們經常面臨的特定的場景下對DbContext的取用、生命周期的維護進行的,目的是更優美,更方便的完成對工作單元的使用。
3.1. 分析
DbContext 的生存期從創建實例時開始,並在釋放實例時結束。我們對數據庫的操作都需要通過 DbContext 進行實現。簡單粗暴的方式是使用是 new 一個 DbContext 對象,操作完再進行 Dispose ,但這樣對使用者不友好。考慮再三,我認為可以從解決以下幾個問題:
- 對
DbContext的取用的封裝- 參考Abp對工作單元的封裝:
- 封裝的非常巧妙和智能,但是也隱藏了很多細節,存在過度封裝的嫌疑,但關於
DbContext的取用可以視為是其中的精華,有很大的借鑒意義。 - 在Abp中
DbContext是存放在AsyncLocal<T>類型的靜態字段中- 此數據結構的詳細介紹請閱讀《C#並發編程系列》中的《多線程共享變量和 AsyncLocal》
- 簡單來說就是
AsyncLocal<T>類型的靜態字段在被並發訪問時其中的一個線程及其輔助線程讀取寫入共享單對其他線程隔離
- 簡單來說就是
- 此數據結構的詳細介紹請閱讀《C#並發編程系列》中的《多線程共享變量和 AsyncLocal》
- 對
DbContext的生命周期維護的封裝- 根據
DbContext的初始化對依賴注入的配置,使用范圍型的方式依賴注入注冊DbContext - 通過范圍型(
IServiceScope)的服務提供者(ServiceProvider)控制DbContext的生命周期。
- 根據
- 對
DbContext的CURD進行封裝- 采用倉儲的方式對
Insert,Update,Get,Delete等數據訪問操作進行封裝
- 采用倉儲的方式對
3.2. 設計
3.2.1. 類圖
3.2.2. 時序圖
3.2.3. 說明
- 使用泛型的方式封裝工作單元相關類,當前生效的
DbContext顯而易見,訪問多個數據庫時互不干擾; IUnitOfWorkManager負責創建工作單元處理器- 通過
IUnitOfWorkHandle<TDbContext>可訪問當前的DbContext已完成數據訪問,運行事務等相關操作; - 靜態類
AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>提供DbContext封裝類型IUnitOfWorkHandle<TDbContext>對象的存取;GetCurrentUow(): IUnitOfWorkHandle<TDbContext>、void SetCurrentUow(IUnitOfWorkHandle<TDbContext> value)負責將當前IUnitOfWorkHandle<TDbContext>的包裝類型LocalUowWrapper的對象存儲到類型為AsyncLocal<LocalUowWrapper>的字段中,保證線程隔離;
LocalUowWrapper<TDbContext>類型提供了Outer屬性用於處理當工作單元處理器在嵌套方法中穿梭時,保證當前的工作單元處理器是設計者需要的;InnerUnitOfWorkHandle<TDbContext>的實現是:- 應對場景是:
- 外部存在工作單元時使用該工作單元;
- 外部不存在時需要創建工作單元;
- 此實現類不是
DbContext對象的代理,而是為了嵌套接力; - 也就是通過
UnitOfWorkManager對象調用Begin()時在兩種場景下返回不同類型的對象,詳細參見源碼部分。
- 應對場景是:
3.3. 源代碼
3.3.1. 工作單元
-
IUnitOfWorkManager和UnitOfWorkManagerusing Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; namespace EntityFramework.UnitOfWorks { public interface IUnitOfWorkManager { IUnitOfWorkHandle<TDbContext> Begin<TDbContext>() where TDbContext : DbContext; IUnitOfWorkHandle<TDbContext> BeginNew<TDbContext>() where TDbContext : DbContext; } }using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace EntityFramework.UnitOfWorks.Impl { public class UnitOfWorkManager : IUnitOfWorkManager { private readonly IServiceProvider _serviceProvider; public UnitOfWorkManager(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public IUnitOfWorkHandle<TDbContext> Begin<TDbContext>() where TDbContext : DbContext { var uowHander = AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>.Current; if (uowHander == null) { return BeginNew<TDbContext>(); } else { return new InnerUnitOfWorkHandle<TDbContext>(); } } public IUnitOfWorkHandle<TDbContext> BeginNew<TDbContext>() where TDbContext : DbContext { IServiceScope serviceScope = _serviceProvider.CreateScope(); var uowHander = new UnitOfWorkHandle<TDbContext>(serviceScope); AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>.Current = uowHander; return uowHander; } } } -
IUnitOfWorkHandle<TDbContext>,UnitOfWorkHandle<TDbContext>和InnerUnitOfWorkHandle<TDbContext>using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace EntityFramework.UnitOfWorks { public interface IUnitOfWorkHandle<TDbContext> : IDisposable where TDbContext:DbContext { TDbContext GetActiveUnitOfWork(); int SaveChange(); Task<int> SaveChangeAsync(); bool IsDisposed { get; } } }using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; namespace EntityFramework.UnitOfWorks.Impl { public class UnitOfWorkHandle<TDbContext> : IUnitOfWorkHandle<TDbContext> where TDbContext : DbContext { private readonly IServiceScope _serviceScope; public bool IsDisposed { get; private set; } public UnitOfWorkHandle(IServiceScope serviceScope) { _serviceScope = serviceScope; } public TDbContext GetActiveUnitOfWork() { return _serviceScope.ServiceProvider.GetRequiredService<TDbContext>(); } public void Dispose() { _serviceScope.Dispose(); IsDisposed = true; // 清空當前 Handle 或回到 OuterHandle AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>.Current = null; } public int SaveChange() { var dbContext = GetActiveUnitOfWork(); if (dbContext == null) { return 0; } return dbContext.SaveChanges(); } public async Task<int> SaveChangeAsync() { var dbContext = GetActiveUnitOfWork(); if (dbContext == null) { return await Task.FromResult(0); } return await dbContext.SaveChangesAsync(CancellationToken.None); } } }using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace Nuctech.TrDevice.EntityFramework.UnitOfWorks.Impl { public class InnerUnitOfWorkHandle<TDbContext> : IUnitOfWorkHandle<TDbContext> where TDbContext : DbContext { public bool IsDisposed { get; private set; } public void Dispose() { IsDisposed = true; } public TDbContext GetActiveUnitOfWork() => AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>.Current?.GetActiveUnitOfWork(); public int SaveChange() { return 0; } public Task<int> SaveChangeAsync() { return Task.FromResult(0); } } } -
AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>和LocalUowWrapper<TDbContext>- 由於這兩個類是強關聯的,所以這里將
LocalUowWrapper<TDbContext>定義為其內部類LocalUowWrapper
using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace EntityFramework.UnitOfWorks.Impl { public class AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext> where TDbContext : DbContext { private static readonly AsyncLocal<LocalUowWrapper> AsyncLocalUow = new AsyncLocal<LocalUowWrapper>(); public static UnitOfWorkHandle<TDbContext> Current { get { return GetCurrentUow(); } set { SetCurrentUow(value); } } private static UnitOfWorkHandle<TDbContext> GetCurrentUow() { var uow = AsyncLocalUow.Value?.UnitOfWorkHandle; if (uow == null) { return null; } if (uow.IsDisposed) { AsyncLocalUow.Value = null; return null; } return uow; } private static void SetCurrentUow(UnitOfWorkHandle<TDbContext> value) { lock (AsyncLocalUow) { if (value == null) { if (AsyncLocalUow.Value == null) { return; } if (AsyncLocalUow.Value.Outer == null) { AsyncLocalUow.Value.UnitOfWorkHandle = null; AsyncLocalUow.Value = null; return; } var oldValue = AsyncLocalUow.Value; AsyncLocalUow.Value = AsyncLocalUow.Value.Outer; oldValue.Outer = null; } else { if (AsyncLocalUow.Value?.UnitOfWorkHandle == null) { if (AsyncLocalUow.Value != null) { AsyncLocalUow.Value.UnitOfWorkHandle = value; } AsyncLocalUow.Value = new LocalUowWrapper(value); return; } var newValue = new LocalUowWrapper(value) { Outer = AsyncLocalUow.Value }; AsyncLocalUow.Value = newValue; } } } private class LocalUowWrapper { public UnitOfWorkHandle<TDbContext> UnitOfWorkHandle { get; set; } public LocalUowWrapper Outer { get; set; } public LocalUowWrapper(UnitOfWorkHandle<TDbContext> unitOfWorkHandle) { UnitOfWorkHandle = unitOfWorkHandle; } } } } - 由於這兩個類是強關聯的,所以這里將
3.3.2. 單元測試
- 以下單元測試基本涵蓋了常見的工作單元使用情況;
- 事務時直接使用
DbContext啟動的事務,更復雜的情況請查看官方文檔;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using EntityFramework.UnitOfWorks;
using EntityFramework.UnitOfWorks.Impl;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace EntityFramework.UnitOfWorkTest
{
public class UnitOfWorkTests
{
public const string ConnectString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TestDatabase";
private readonly IServiceProvider _serviceProvider;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public UnitOfWorkTests()
{
IServiceCollection services = new ServiceCollection();
services.AddDbContext<PersionDbContext>(options => options.UseSqlServer(ConnectString));
services.AddTransient<IUnitOfWorkManager, UnitOfWorkManager>();
_serviceProvider = services.BuildServiceProvider();
_unitOfWorkManager = _serviceProvider.GetRequiredService<IUnitOfWorkManager>();
}
/// <summary>
/// 正常操作
/// </summary>
/// <returns></returns>
[Fact]
public async Task ShouldNormalOperation()
{
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
var lists = uowHandle.GetActiveUnitOfWork().Persions.ToList();
//清理
lists.ForEach(u => uowHandle.GetActiveUnitOfWork().Persions.Remove(u));
uowHandle.SaveChange();
}
await AddUser("張三");
await AddUser("李四");
await AddUser("王五");
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
var lists = uowHandle.GetActiveUnitOfWork().Persions.ToList();
lists.Count.ShouldBe(3);
//清理
lists.ForEach(u => uowHandle.GetActiveUnitOfWork().Persions.Remove(u));
uowHandle.SaveChange();
}
async Task AddUser(string name)
{
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
uowHandle.GetActiveUnitOfWork().Persions.Add(new Persion() { Name = name });
await uowHandle.SaveChangeAsync();
}
}
}
/// <summary>
/// 超出使用范圍使用工作單元
/// </summary>
[Fact]
public void ShouldNotUseUow()
{
var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>();
uowHandle.Dispose();
Assert.Throws<ObjectDisposedException>(() => uowHandle.GetActiveUnitOfWork().Persions.ToList());
}
/// <summary>
/// 工作單元嵌套時,當前工作單IUnitOfWorkHandle和DbContext的實際情況
/// </summary>
[Fact]
public void ShouldAcrossMutiFunction()
{
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
var outerDbContext = uowHandle.GetActiveUnitOfWork();
uowHandle.ShouldBeOfType<UnitOfWorkHandle<PersionDbContext>>();
using (var innerUowHandle = _unitOfWorkManager.BeginNew<PersionDbContext>())
{
var innerDbContext = innerUowHandle.GetActiveUnitOfWork();
innerUowHandle.GetActiveUnitOfWork().ShouldNotBe(outerDbContext);
innerUowHandle.ShouldBeOfType<UnitOfWorkHandle<PersionDbContext>>();
innerUowHandle.ShouldNotBe(uowHandle);
using (var innerInnerUowHandle = _unitOfWorkManager.BeginNew<PersionDbContext>())
{
innerInnerUowHandle.ShouldBeOfType<UnitOfWorkHandle<PersionDbContext>>();
innerInnerUowHandle.GetActiveUnitOfWork().ShouldNotBe(outerDbContext);
innerInnerUowHandle.ShouldNotBe(uowHandle);
}
innerUowHandle.GetActiveUnitOfWork().ShouldBe(innerDbContext);
}
using (var innerUowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
innerUowHandle.ShouldBeOfType<InnerUnitOfWorkHandle<PersionDbContext>>();
innerUowHandle.GetActiveUnitOfWork().ShouldBe(outerDbContext);
}
uowHandle.GetActiveUnitOfWork().ShouldBe(outerDbContext);
}
}
/// <summary>
/// 使用數據庫事務
/// </summary>
/// <param name="isCommit">是否提交數據</param>
/// <returns></returns>
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ShouldCommitTransaction(bool isCommit)
{
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
var lists = uowHandle.GetActiveUnitOfWork().Persions.ToList();
lists.ForEach(u => uowHandle.GetActiveUnitOfWork().Persions.Remove(u));
uowHandle.SaveChange();
}
List<string> names = new List<string> { "張三", "李四", "王老五" };
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
using (var transaction = uowHandle.GetActiveUnitOfWork().Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
{
for (int i = 0; i < names.Count; i++)
{
uowHandle.GetActiveUnitOfWork().Persions.Add(new Persion() { Name = names[i] });
//事務期間的SaveChange不會提交到數據庫
await uowHandle.SaveChangeAsync();
}
if (isCommit)
{
transaction.Commit();
}
else
{
transaction.Rollback();
}
}
}
using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
{
uowHandle.GetActiveUnitOfWork().Persions.Count().ShouldBe(isCommit ? 3 : 0);
}
}
}
}
4. 封裝-倉儲
4.1. 分析
- 關於倉儲類的封裝主要是為了更方便的使用工作單元;
- 面向我們經常使用的操作: Select, Insert, Update, Delete 四個方向進行封裝。
4.2. 設計
4.2.1. 類圖
4.2.2. 時序圖
4.2.3. 源碼
IRepository<TDbContext, TEntity>
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using EntityFramework.UnitOfWorks;
namespace EntityFramework.Repositories
public interface IRepository<TDbContext, TEntity> where TEntity : class where TDbContext : DbContext
{
IUnitOfWorkManager UnitOfWorkManager { get; }
TDbContext CurrentDbContext { get; }
IUnitOfWorkHandle<TDbContext> BeginUnitOfWork();
IUnitOfWorkHandle<TDbContext> BeginNewUnitOfWork();
#region Select/Get/Query
IQueryable<TEntity> GetAll();
IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors);
List<TEntity> GetAllList();
Task<List<TEntity>> GetAllListAsync();
List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate);
Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate);
T Query<T>(Func<IQueryable<TEntity>, T> queryMethod);
TEntity Single(Expression<Func<TEntity, bool>> predicate);
Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate);
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
#endregion
TEntity Insert(TEntity entity);
Task<TEntity> InsertAsync(TEntity entity);
TEntity Update(TEntity entity);
Task<TEntity> UpdateAsync(TEntity entity);
void Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
}
}
EFRepository<TDbContext, TEntity>
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using EntityFramework.UnitOfWorks;
using EntityFramework.UnitOfWorks.Impl;
namespace EntityFramework.Repositories.Impl
{
public class EFRepository<TDbContext, TEntity> : IRepository<TDbContext, TEntity> where TEntity : class where TDbContext : DbContext
{
private readonly IUnitOfWorkManager _unitOfWorkManager;
public EFRepository(IUnitOfWorkManager unitOfWorkManager)
{
_unitOfWorkManager = unitOfWorkManager;
}
public virtual TDbContext CurrentDbContext => AsyncLocalCurrentUnitOfWorkHandleProvider<TDbContext>.Current?.GetActiveUnitOfWork();
public IUnitOfWorkManager UnitOfWorkManager => _unitOfWorkManager;
public IUnitOfWorkHandle<TDbContext> BeginUnitOfWork()
{
return _unitOfWorkManager.Begin<TDbContext>();
}
public IUnitOfWorkHandle<TDbContext> BeginNewUnitOfWork()
{
return _unitOfWorkManager.BeginNew<TDbContext>();
}
public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return uowHander.GetActiveUnitOfWork().Set<TEntity>().FirstOrDefault(predicate);
}
public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return await uowHander.GetActiveUnitOfWork().Set<TEntity>().FirstOrDefaultAsync(predicate);
}
public IQueryable<TEntity> GetAll()
{
var context = CurrentDbContext;
if (context != null)
{
return context.Set<TEntity>().AsQueryable();
}
throw new ArgumentNullException(nameof(CurrentDbContext));
}
public IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)
{
var context = CurrentDbContext;
if (context != null)
{
var query = context.Set<TEntity>().AsQueryable();
if (propertySelectors != null)
{
foreach (var propertySelector in propertySelectors)
{
query = query.Include(propertySelector);
}
}
return query;
}
throw new ArgumentNullException(nameof(CurrentDbContext));
}
public List<TEntity> GetAllList()
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return uowHander.GetActiveUnitOfWork().Set<TEntity>().ToList();
}
public List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return uowHander.GetActiveUnitOfWork().Set<TEntity>().Where(predicate).ToList();
}
public async Task<List<TEntity>> GetAllListAsync()
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return await uowHander.GetActiveUnitOfWork().Set<TEntity>().ToListAsync();
}
public async Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return await uowHander.GetActiveUnitOfWork().Set<TEntity>().Where(predicate).ToListAsync();
}
public T Query<T>(Func<IQueryable<TEntity>, T> queryMethod)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return queryMethod(GetAll());
}
public TEntity Single(Expression<Func<TEntity, bool>> predicate)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return uowHander.GetActiveUnitOfWork().Set<TEntity>().SingleOrDefault(predicate);
}
public async Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
return await uowHander.GetActiveUnitOfWork().Set<TEntity>().SingleOrDefaultAsync(predicate);
}
public TEntity Insert(TEntity entity)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
entity = uowHander.GetActiveUnitOfWork().Set<TEntity>().Add(entity).Entity;
uowHander.SaveChange();
return entity;
}
public async Task<TEntity> InsertAsync(TEntity entity)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
entity = uowHander.GetActiveUnitOfWork().Set<TEntity>().Add(entity).Entity;
await uowHander.SaveChangeAsync();
return entity;
}
public TEntity Update(TEntity entity)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
var context = uowHander.GetActiveUnitOfWork();
AttachIfNot(context, entity);
context.Entry(entity).State = EntityState.Modified;
uowHander.SaveChange();
return entity;
}
public async Task<TEntity> UpdateAsync(TEntity entity)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
var context = uowHander.GetActiveUnitOfWork();
AttachIfNot(context, entity);
context.Entry(entity).State = EntityState.Modified;
await uowHander.SaveChangeAsync();
return entity;
}
public void Delete(TEntity entity)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
var context = uowHander.GetActiveUnitOfWork();
AttachIfNot(context, entity);
context.Set<TEntity>().Remove(entity);
uowHander.SaveChange();
}
public async Task DeleteAsync(TEntity entity)
{
using var uowHander = _unitOfWorkManager.Begin<TDbContext>();
var context = uowHander.GetActiveUnitOfWork();
AttachIfNot(context, entity);
context.Set<TEntity>().Remove(entity);
await uowHander.SaveChangeAsync();
}
protected virtual void AttachIfNot(DbContext dbContext, TEntity entity)
{
var entry = dbContext.ChangeTracker.Entries().FirstOrDefault(ent => ent.Entity == entity);
if (entry != null)
{
return;
}
dbContext.Set<TEntity>().Attach(entity);
}
}
}
5. 總結
本片文章目的是分享自己將DbContext的使用封裝為工作單元的心得,希望給大家一點啟發。
歡迎大家留言討論,如果文章有錯誤歡迎指正。
