|
@@ -1,6 +1,7 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
using Vit.Linq.ExpressionNodes;
|
|
|
using Vit.Linq.ExpressionNodes.ExpressionConvertor.MethodCalls;
|
|
@@ -9,11 +10,11 @@ using Vitorm.Entity;
|
|
|
|
|
|
namespace Vitorm
|
|
|
{
|
|
|
- public abstract partial class DbContext : IDbContext, IDisposable
|
|
|
+ public partial class DbContext : IDbContext, IDisposable
|
|
|
{
|
|
|
- public DbContext()
|
|
|
+ public DbContext(Func<IDbContext, IEntityDescriptor, IDbSet> dbSetCreator = null)
|
|
|
{
|
|
|
- dbSetCreator = DefaultDbSetCreator;
|
|
|
+ this.dbSetCreator = dbSetCreator ?? Vitorm.DefaultDbSetConstructor.CreateDbSet;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -32,12 +33,7 @@ namespace Vitorm
|
|
|
|
|
|
|
|
|
#region DbSet
|
|
|
- protected IDbSet DefaultDbSetCreator(IEntityDescriptor entityDescriptor)
|
|
|
- {
|
|
|
- return DbSetConstructor.CreateDbSet(this, entityDescriptor);
|
|
|
- }
|
|
|
-
|
|
|
- protected virtual Func<IEntityDescriptor, IDbSet> dbSetCreator { set; get; }
|
|
|
+ protected virtual Func<IDbContext, IEntityDescriptor, IDbSet> dbSetCreator { set; get; }
|
|
|
|
|
|
protected Dictionary<Type, IDbSet> dbSetMap = null;
|
|
|
|
|
@@ -47,14 +43,14 @@ namespace Vitorm
|
|
|
|
|
|
var entityDescriptor = GetEntityDescriptor(entityType);
|
|
|
|
|
|
- dbSet = dbSetCreator(entityDescriptor);
|
|
|
+ dbSet = dbSetCreator(this, entityDescriptor);
|
|
|
if (dbSet == null) return null;
|
|
|
|
|
|
dbSetMap ??= new();
|
|
|
dbSetMap[entityType] = dbSet;
|
|
|
return dbSet;
|
|
|
}
|
|
|
- public virtual IDbSet CreateDbSet(IEntityDescriptor entityDescriptor) => dbSetCreator(entityDescriptor);
|
|
|
+ public virtual IDbSet CreateDbSet(IEntityDescriptor entityDescriptor) => dbSetCreator(this, entityDescriptor);
|
|
|
public virtual IDbSet<Entity> DbSet<Entity>()
|
|
|
{
|
|
|
return DbSet(typeof(Entity)) as IDbSet<Entity>;
|
|
@@ -84,8 +80,8 @@ namespace Vitorm
|
|
|
dbSet?.ChangeTable(tableName);
|
|
|
return dbSet;
|
|
|
}
|
|
|
- public virtual DbSet<Entity> ChangeTable<Entity>(string tableName)
|
|
|
- => ChangeTable(typeof(Entity), tableName) as DbSet<Entity>;
|
|
|
+ public virtual IDbSet<Entity> ChangeTable<Entity>(string tableName)
|
|
|
+ => ChangeTable(typeof(Entity), tableName) as IDbSet<Entity>;
|
|
|
|
|
|
|
|
|
public virtual IDbSet ChangeTableBack(Type entityType)
|
|
@@ -94,8 +90,8 @@ namespace Vitorm
|
|
|
dbSet?.ChangeTableBack();
|
|
|
return dbSet;
|
|
|
}
|
|
|
- public virtual DbSet<Entity> ChangeTableBack<Entity>()
|
|
|
- => ChangeTableBack(typeof(Entity)) as DbSet<Entity>;
|
|
|
+ public virtual IDbSet<Entity> ChangeTableBack<Entity>()
|
|
|
+ => ChangeTableBack(typeof(Entity)) as IDbSet<Entity>;
|
|
|
|
|
|
#endregion
|
|
|
|
|
@@ -106,36 +102,72 @@ namespace Vitorm
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
+ #region Sync Method
|
|
|
// #0 Schema : Create Drop
|
|
|
- public abstract void TryCreateTable<Entity>();
|
|
|
- public abstract void TryDropTable<Entity>();
|
|
|
- public abstract void Truncate<Entity>();
|
|
|
+ public virtual void TryCreateTable<Entity>() => DbSet<Entity>().TryCreateTable();
|
|
|
+ public virtual void TryDropTable<Entity>() => DbSet<Entity>().TryDropTable();
|
|
|
+ public virtual void Truncate<Entity>() => DbSet<Entity>().Truncate();
|
|
|
|
|
|
|
|
|
// #1 Create : Add AddRange
|
|
|
- public abstract Entity Add<Entity>(Entity entity);
|
|
|
- public abstract void AddRange<Entity>(IEnumerable<Entity> entities);
|
|
|
+ public virtual Entity Add<Entity>(Entity entity) => DbSet<Entity>().Add(entity);
|
|
|
+ public virtual void AddRange<Entity>(IEnumerable<Entity> entities) => DbSet<Entity>().AddRange(entities);
|
|
|
|
|
|
- // #2 Retrieve : Get Query
|
|
|
- public abstract Entity Get<Entity>(object keyValue);
|
|
|
- public abstract IQueryable<Entity> Query<Entity>();
|
|
|
+
|
|
|
+ // #2 Retrieve : Get
|
|
|
+ public virtual Entity Get<Entity>(object keyValue) => DbSet<Entity>().Get(keyValue);
|
|
|
+ public virtual IQueryable<Entity> Query<Entity>() => DbSet<Entity>().Query();
|
|
|
|
|
|
|
|
|
// #3 Update: Update UpdateRange
|
|
|
- public abstract int Update<Entity>(Entity entity);
|
|
|
- public abstract int UpdateRange<Entity>(IEnumerable<Entity> entities);
|
|
|
+ public virtual int Update<Entity>(Entity entity) => DbSet<Entity>().Update(entity);
|
|
|
+ public virtual int UpdateRange<Entity>(IEnumerable<Entity> entities) => DbSet<Entity>().UpdateRange(entities);
|
|
|
|
|
|
|
|
|
// #4 Delete : Delete DeleteRange DeleteByKey DeleteByKeys
|
|
|
- public abstract int Delete<Entity>(Entity entity);
|
|
|
- public abstract int DeleteRange<Entity>(IEnumerable<Entity> entities);
|
|
|
+ public virtual int Delete<Entity>(Entity entity) => DbSet<Entity>().Delete(entity);
|
|
|
+ public virtual int DeleteRange<Entity>(IEnumerable<Entity> entities) => DbSet<Entity>().DeleteRange(entities);
|
|
|
|
|
|
+ public virtual int DeleteByKey<Entity>(object keyValue) => DbSet<Entity>().DeleteByKey(keyValue);
|
|
|
+ public virtual int DeleteByKeys<Entity, Key>(IEnumerable<Key> keys) => DbSet<Entity>().DeleteByKeys<Key>(keys);
|
|
|
|
|
|
- public abstract int DeleteByKey<Entity>(object keyValue);
|
|
|
- public abstract int DeleteByKeys<Entity, Key>(IEnumerable<Key> keys);
|
|
|
+ #endregion
|
|
|
|
|
|
|
|
|
|
|
|
+ #region Async Method
|
|
|
+
|
|
|
+ // #0 Schema : Create Drop Truncate
|
|
|
+ public virtual Task TryCreateTableAsync<Entity>() => DbSet<Entity>().TryCreateTableAsync();
|
|
|
+ public virtual Task TryDropTableAsync<Entity>() => DbSet<Entity>().TryDropTableAsync();
|
|
|
+ public virtual Task TruncateAsync<Entity>() => DbSet<Entity>().TruncateAsync();
|
|
|
+
|
|
|
+
|
|
|
+ // #1 Create : Add AddRange
|
|
|
+ public virtual Task<Entity> AddAsync<Entity>(Entity entity) => DbSet<Entity>().AddAsync(entity);
|
|
|
+ public virtual Task AddRangeAsync<Entity>(IEnumerable<Entity> entities) => DbSet<Entity>().AddRangeAsync(entities);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // #2 Retrieve : Get Query
|
|
|
+ public virtual Task<Entity> GetAsync<Entity>(object keyValue) => DbSet<Entity>().GetAsync(keyValue);
|
|
|
+
|
|
|
+
|
|
|
+ // #3 Update: Update UpdateRange
|
|
|
+ public virtual Task<int> UpdateAsync<Entity>(Entity entity) => DbSet<Entity>().UpdateAsync(entity);
|
|
|
+ public virtual Task<int> UpdateRangeAsync<Entity>(IEnumerable<Entity> entities) => DbSet<Entity>().UpdateRangeAsync(entities);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // #4 Delete : Delete DeleteRange DeleteByKey DeleteByKeys
|
|
|
+ public virtual Task<int> DeleteAsync<Entity>(Entity entity) => DbSet<Entity>().DeleteAsync(entity);
|
|
|
+ public virtual Task<int> DeleteRangeAsync<Entity>(IEnumerable<Entity> entities) => DbSet<Entity>().DeleteRangeAsync(entities);
|
|
|
+
|
|
|
+ public virtual Task<int> DeleteByKeyAsync<Entity>(object keyValue) => DbSet<Entity>().DeleteByKeyAsync(keyValue);
|
|
|
+ public virtual Task<int> DeleteByKeysAsync<Entity, Key>(IEnumerable<Key> keys) => DbSet<Entity>().DeleteByKeysAsync<Key>(keys);
|
|
|
+ #endregion
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|