Initial Commit ...
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using xDataService.Configuration;
|
||||
|
||||
namespace xDataService.Db {
|
||||
/// <summary>
|
||||
/// Provide an abstraction layout arround DBContext ...
|
||||
/// Only Used on EFCore ...
|
||||
/// </summary>
|
||||
public abstract class XDbContext : DbContext {
|
||||
private readonly XDataServiceConfiguration config;
|
||||
|
||||
//
|
||||
#region Entities ...
|
||||
//
|
||||
// Register Default and Base Entities ...
|
||||
#endregion
|
||||
|
||||
//
|
||||
// Constructor ...
|
||||
public XDbContext (
|
||||
DbContextOptions options,
|
||||
XDataServiceConfiguration config
|
||||
) : base (options) {
|
||||
//
|
||||
try {
|
||||
Database.EnsureCreated ();
|
||||
Database.Migrate ();
|
||||
} catch { }
|
||||
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Fluent API ...
|
||||
protected override void OnModelCreating (ModelBuilder modelBuilder) {
|
||||
//
|
||||
OnXModelCreating (modelBuilder);
|
||||
|
||||
//
|
||||
// Since this class inherit from Identity Db Context,
|
||||
// we have to pass this to Building Identity Models ...
|
||||
base.OnModelCreating (modelBuilder);
|
||||
}
|
||||
|
||||
//
|
||||
public abstract void OnXModelCreating (ModelBuilder modelBuilder);
|
||||
|
||||
//
|
||||
// Prepare Configuring ...
|
||||
protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) {
|
||||
//
|
||||
if (config.EnableDetailedErrors) {
|
||||
optionsBuilder.EnableDetailedErrors ();
|
||||
}
|
||||
|
||||
//
|
||||
if (config.EnableSensitiveDataLogging) {
|
||||
optionsBuilder.EnableSensitiveDataLogging ();
|
||||
}
|
||||
|
||||
//
|
||||
if (config.EnableTracking) {
|
||||
optionsBuilder.UseQueryTrackingBehavior (QueryTrackingBehavior.TrackAll);
|
||||
} else {
|
||||
optionsBuilder.UseQueryTrackingBehavior (QueryTrackingBehavior.NoTracking);
|
||||
}
|
||||
|
||||
//
|
||||
OnXConfiguring (optionsBuilder);
|
||||
|
||||
//
|
||||
base.OnConfiguring (optionsBuilder);
|
||||
}
|
||||
|
||||
//
|
||||
public abstract void OnXConfiguring (DbContextOptionsBuilder optionsBuilder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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>
|
||||
/// <returns></returns>
|
||||
public async Task<int> SaveChangesAsync () {
|
||||
return await DbContext.SaveChangesAsync ();
|
||||
}
|
||||
|
||||
public void Dispose () {
|
||||
DbContext.Dispose ();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user