48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using xDataService.Interfaces;
|
|
using xModels.Base;
|
|
|
|
namespace xDataService.Db {
|
|
/// <summary>
|
|
/// Provide Unit Of Works Design Pattern for Db Transactions ...
|
|
/// Only Used on EFCore ...
|
|
/// </summary>
|
|
/// <typeparam name="TDbContext"></typeparam>
|
|
public class XUnitOfWorks<TDbContext> : IXUnitOfWorks<TDbContext>
|
|
where TDbContext : XDbContext {
|
|
public TDbContext DbContext { get; }
|
|
|
|
public XUnitOfWorks (TDbContext context) {
|
|
this.DbContext = context;
|
|
}
|
|
|
|
public DbSet<T> GetDbSet<T, TKey> ()
|
|
where T : XBaseEntity<TKey> {
|
|
return DbContext.Set<T> ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save Changes
|
|
/// </summary>
|
|
public int SaveChanges () {
|
|
return DbContext.SaveChanges ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save Changes Async
|
|
/// </summary>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<int> SaveChangesAsync (
|
|
CancellationToken cancellationToken = default
|
|
) {
|
|
return await DbContext.SaveChangesAsync (cancellationToken);
|
|
}
|
|
|
|
public void Dispose () {
|
|
DbContext.Dispose ();
|
|
}
|
|
}
|
|
} |