|
@@ -1,5 +1,4 @@
|
|
|
using System;
|
|
|
-using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
@@ -15,10 +14,13 @@ namespace Vitorm
|
|
|
{
|
|
|
public partial class Data
|
|
|
{
|
|
|
+ public static DataSource dataSource = new DataSource();
|
|
|
+
|
|
|
static Data()
|
|
|
{
|
|
|
Init(Appsettings.json);
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// Data.Init("appsettings.Development.json")
|
|
|
/// </summary>
|
|
@@ -27,123 +29,52 @@ namespace Vitorm
|
|
|
{
|
|
|
Init(new JsonFile(appsettingsFileName));
|
|
|
}
|
|
|
+
|
|
|
public static void Init(JsonFile json)
|
|
|
{
|
|
|
- #region #1 load dataProvider
|
|
|
- {
|
|
|
- var dataSourceConfigs = json.GetByPath<List<Dictionary<string, object>>>("Vitorm.Data");
|
|
|
- var dataProviders = dataSourceConfigs?.Select(CreateDataProvider).NotNull().ToList();
|
|
|
+ // #1 load dataProviders
|
|
|
+ dataSource.LoadDataProviders(json);
|
|
|
|
|
|
- if (dataProviders?.Any() == true) providerCache.AddRange(dataProviders);
|
|
|
- }
|
|
|
- #endregion
|
|
|
|
|
|
-
|
|
|
- #region #2 load entityLoader
|
|
|
- {
|
|
|
- var configs = json.GetByPath<List<Dictionary<string, object>>>("Vitorm.EntityLoader");
|
|
|
- configs?.ForEach(config =>
|
|
|
- {
|
|
|
- object temp;
|
|
|
- string className = config.TryGetValue("className", out temp) ? temp as string : null;
|
|
|
- string assemblyFile = config.TryGetValue("assemblyFile", out temp) ? temp as string : null;
|
|
|
- string assemblyName = config.TryGetValue("assemblyName", out temp) ? temp as string : null;
|
|
|
-
|
|
|
- int index = config.TryGetValue("index", out temp) && temp is int i ? i : 0;
|
|
|
-
|
|
|
- var entityLoader = ObjectLoader.CreateInstance(className, assemblyFile: assemblyFile, assemblyName: assemblyName) as IEntityLoader;
|
|
|
- if (entityLoader == null) return;
|
|
|
-
|
|
|
- EntityLoaders.Instance.loaders.Insert(index, entityLoader);
|
|
|
- });
|
|
|
- }
|
|
|
- #endregion
|
|
|
+ // #2 load DefaultEntityLoaders
|
|
|
+ var entityLoaderConfigs = json.GetByPath<List<Dictionary<string, object>>>("Vitorm.EntityLoader");
|
|
|
+ LoadDefaultEntityLoaders(entityLoaderConfigs);
|
|
|
}
|
|
|
|
|
|
- public static bool AddDataSource(Dictionary<string, object> dataSourceConfig)
|
|
|
+ public static void LoadDefaultEntityLoaders(IEnumerable<Dictionary<string, object>> entityLoaderConfigs)
|
|
|
{
|
|
|
- var provider = CreateDataProvider(dataSourceConfig);
|
|
|
- if (provider == null) return false;
|
|
|
+ entityLoaderConfigs?.ForEach(config =>
|
|
|
+ {
|
|
|
+ object temp;
|
|
|
+ string className = config.TryGetValue("className", out temp) ? temp as string : null;
|
|
|
+ string assemblyFile = config.TryGetValue("assemblyFile", out temp) ? temp as string : null;
|
|
|
+ string assemblyName = config.TryGetValue("assemblyName", out temp) ? temp as string : null;
|
|
|
|
|
|
- providerCache.Insert(0, provider);
|
|
|
- providerMap.Clear();
|
|
|
- return true;
|
|
|
- }
|
|
|
- public static void ClearDataSource(Predicate<DataProviderCache> predicate = null)
|
|
|
- {
|
|
|
- if (predicate != null)
|
|
|
- providerCache.RemoveAll(predicate);
|
|
|
- else
|
|
|
- providerCache.Clear();
|
|
|
- providerMap.Clear();
|
|
|
+ int index = config.TryGetValue("index", out temp) && temp is int i ? i : 0;
|
|
|
+
|
|
|
+ var entityLoader = ObjectLoader.CreateInstance(className, assemblyFile: assemblyFile, assemblyName: assemblyName) as IEntityLoader;
|
|
|
+ if (entityLoader == null) return;
|
|
|
+
|
|
|
+ EntityLoaders.Instance.loaders.Insert(index, entityLoader);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
+ public static bool LoadDataProvider(Dictionary<string, object> dataProviderConfig) => dataSource.LoadDataProvider(dataProviderConfig);
|
|
|
|
|
|
- #region LoadDataProvider
|
|
|
+ public static void ClearDataProviders(Predicate<DataProviderCache> predicate = null) => dataSource.ClearDataProviders(predicate);
|
|
|
|
|
|
- public static IDataProvider DataProvider<Entity>() => DataProvider(typeof(Entity));
|
|
|
- public static IDataProvider DataProvider(Type entityType)
|
|
|
- {
|
|
|
- return providerMap.GetOrAdd(entityType, GetDataProviderFromConfig);
|
|
|
|
|
|
- static IDataProvider GetDataProviderFromConfig(Type entityType)
|
|
|
- {
|
|
|
- var classFullName = entityType.FullName;
|
|
|
- return providerCache.FirstOrDefault(cache => cache.Match(classFullName))?.dataProvider
|
|
|
- ?? throw new NotImplementedException("can not find config for type: " + classFullName);
|
|
|
- }
|
|
|
- }
|
|
|
+ #region LoadDataProvider
|
|
|
+
|
|
|
+ public static IDataProvider DataProvider<Entity>() => dataSource.DataProvider<Entity>();
|
|
|
+ public static IDataProvider DataProvider(Type entityType) => dataSource.DataProvider(entityType);
|
|
|
|
|
|
/// <summary>
|
|
|
/// dataProviderName: dataProviderName or dataProviderNamespace
|
|
|
/// </summary>
|
|
|
/// <param name="dataProviderName"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static IDataProvider DataProvider(string dataProviderName)
|
|
|
- {
|
|
|
- return providerCache.FirstOrDefault(cache => cache.name == dataProviderName || cache.@namespace == dataProviderName)?.dataProvider;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- static readonly ConcurrentDictionary<Type, IDataProvider> providerMap = new();
|
|
|
-
|
|
|
- static readonly List<DataProviderCache> providerCache = new();
|
|
|
-
|
|
|
-
|
|
|
- static DataProviderCache CreateDataProvider(Dictionary<string, object> dataSourceConfig)
|
|
|
- {
|
|
|
- /*
|
|
|
- "provider": "Vitorm.Sqlite.DataProvider",
|
|
|
- "assemblyName": "Vitorm.Sqlite",
|
|
|
- "assemblyFile": "Vitorm.Sqlite.dll",
|
|
|
- */
|
|
|
-
|
|
|
- object temp;
|
|
|
- string provider = dataSourceConfig.TryGetValue("provider", out temp) ? temp as string : null;
|
|
|
- string assemblyName = dataSourceConfig.TryGetValue("assemblyName", out temp) ? temp as string : null;
|
|
|
- string assemblyFile = dataSourceConfig.TryGetValue("assemblyFile", out temp) ? temp as string : null;
|
|
|
-
|
|
|
- Type providerType;
|
|
|
- IDataProvider dataProvider;
|
|
|
-
|
|
|
- // #1 load
|
|
|
- providerType = ObjectLoader.GetType(className: provider, assemblyName: assemblyName, assemblyFile: assemblyFile);
|
|
|
- dataProvider = providerType?.GetConstructor(Type.EmptyTypes)?.Invoke(new object[] { }) as IDataProvider;
|
|
|
-
|
|
|
- // #2 try load by database type (Sqlite/MySql/SqlServer)
|
|
|
- if (dataProvider == null)
|
|
|
- {
|
|
|
- providerType = ObjectLoader.GetType(className: $"Vitorm.{provider}.DataProvider", assemblyName: $"Vitorm.{provider}", assemblyFile: $"Vitorm.{provider}.dll");
|
|
|
- dataProvider = providerType?.GetConstructor(Type.EmptyTypes)?.Invoke(new object[] { }) as IDataProvider;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (dataProvider == null) return null;
|
|
|
-
|
|
|
- dataProvider.Init(dataSourceConfig);
|
|
|
- return new DataProviderCache(dataProvider, dataSourceConfig);
|
|
|
- }
|
|
|
-
|
|
|
+ public static IDataProvider DataProvider(string dataProviderName) => dataSource.DataProvider(dataProviderName);
|
|
|
|
|
|
#endregion
|
|
|
|
|
@@ -153,31 +84,31 @@ namespace Vitorm
|
|
|
#region CRUD Sync
|
|
|
|
|
|
// #0 Schema : TryCreateTable TryDropTable
|
|
|
- public static void TryCreateTable<Entity>() => DataProvider<Entity>().TryCreateTable<Entity>();
|
|
|
- public static void TryDropTable<Entity>() => DataProvider<Entity>().TryDropTable<Entity>();
|
|
|
- public static void Truncate<Entity>() => DataProvider<Entity>().Truncate<Entity>();
|
|
|
+ public static void TryCreateTable<Entity>() => dataSource.TryCreateTable<Entity>();
|
|
|
+ public static void TryDropTable<Entity>() => dataSource.TryDropTable<Entity>();
|
|
|
+ public static void Truncate<Entity>() => dataSource.Truncate<Entity>();
|
|
|
|
|
|
|
|
|
// #1 Create : Add AddRange
|
|
|
- public static Entity Add<Entity>(Entity entity) => DataProvider<Entity>().Add<Entity>(entity);
|
|
|
- public static void AddRange<Entity>(IEnumerable<Entity> entities) => DataProvider<Entity>().AddRange<Entity>(entities);
|
|
|
+ public static Entity Add<Entity>(Entity entity) => dataSource.Add<Entity>(entity);
|
|
|
+ public static void AddRange<Entity>(IEnumerable<Entity> entities) => dataSource.AddRange<Entity>(entities);
|
|
|
|
|
|
// #2 Retrieve : Get Query
|
|
|
- public static Entity Get<Entity>(object keyValue) => DataProvider<Entity>().Get<Entity>(keyValue);
|
|
|
- public static IQueryable<Entity> Query<Entity>() => DataProvider<Entity>().Query<Entity>();
|
|
|
+ public static Entity Get<Entity>(object keyValue) => dataSource.Get<Entity>(keyValue);
|
|
|
+ public static IQueryable<Entity> Query<Entity>() => dataSource.Query<Entity>();
|
|
|
|
|
|
|
|
|
// #3 Update: Update UpdateRange
|
|
|
- public static int Update<Entity>(Entity entity) => DataProvider<Entity>().Update<Entity>(entity);
|
|
|
- public static int UpdateRange<Entity>(IEnumerable<Entity> entities) => DataProvider<Entity>().UpdateRange<Entity>(entities);
|
|
|
+ public static int Update<Entity>(Entity entity) => dataSource.Update<Entity>(entity);
|
|
|
+ public static int UpdateRange<Entity>(IEnumerable<Entity> entities) => dataSource.UpdateRange<Entity>(entities);
|
|
|
|
|
|
|
|
|
// #4 Delete : Delete DeleteRange DeleteByKey DeleteByKeys
|
|
|
- public static int Delete<Entity>(Entity entity) => DataProvider<Entity>().Delete<Entity>(entity);
|
|
|
- public static int DeleteRange<Entity>(IEnumerable<Entity> entities) => DataProvider<Entity>().DeleteRange<Entity>(entities);
|
|
|
+ public static int Delete<Entity>(Entity entity) => dataSource.Delete<Entity>(entity);
|
|
|
+ public static int DeleteRange<Entity>(IEnumerable<Entity> entities) => dataSource.DeleteRange<Entity>(entities);
|
|
|
|
|
|
- public static int DeleteByKey<Entity>(object keyValue) => DataProvider<Entity>().DeleteByKey<Entity>(keyValue);
|
|
|
- public static int DeleteByKeys<Entity, Key>(IEnumerable<Key> keys) => DataProvider<Entity>().DeleteByKeys<Entity, Key>(keys);
|
|
|
+ public static int DeleteByKey<Entity>(object keyValue) => dataSource.DeleteByKey<Entity>(keyValue);
|
|
|
+ public static int DeleteByKeys<Entity, Key>(IEnumerable<Key> keys) => dataSource.DeleteByKeys<Entity, Key>(keys);
|
|
|
|
|
|
#endregion
|
|
|
|
|
@@ -186,30 +117,30 @@ namespace Vitorm
|
|
|
#region CRUD Async
|
|
|
|
|
|
// #0 Schema : TryCreateTable TryDropTable
|
|
|
- public static Task TryCreateTableAsync<Entity>() => DataProvider<Entity>().TryCreateTableAsync<Entity>();
|
|
|
- public static Task TryDropTableAsync<Entity>() => DataProvider<Entity>().TryDropTableAsync<Entity>();
|
|
|
- public static Task TruncateAsync<Entity>() => DataProvider<Entity>().TruncateAsync<Entity>();
|
|
|
+ public static Task TryCreateTableAsync<Entity>() => dataSource.TryCreateTableAsync<Entity>();
|
|
|
+ public static Task TryDropTableAsync<Entity>() => dataSource.TryDropTableAsync<Entity>();
|
|
|
+ public static Task TruncateAsync<Entity>() => dataSource.TruncateAsync<Entity>();
|
|
|
|
|
|
|
|
|
// #1 Create : Add AddRange
|
|
|
- public static Task<Entity> AddAsync<Entity>(Entity entity) => DataProvider<Entity>().AddAsync<Entity>(entity);
|
|
|
- public static Task AddRangeAsync<Entity>(IEnumerable<Entity> entities) => DataProvider<Entity>().AddRangeAsync<Entity>(entities);
|
|
|
+ public static Task<Entity> AddAsync<Entity>(Entity entity) => dataSource.AddAsync<Entity>(entity);
|
|
|
+ public static Task AddRangeAsync<Entity>(IEnumerable<Entity> entities) => dataSource.AddRangeAsync<Entity>(entities);
|
|
|
|
|
|
// #2 Retrieve : Get Query
|
|
|
- public static Task<Entity> GetAsync<Entity>(object keyValue) => DataProvider<Entity>().GetAsync<Entity>(keyValue);
|
|
|
+ public static Task<Entity> GetAsync<Entity>(object keyValue) => dataSource.GetAsync<Entity>(keyValue);
|
|
|
|
|
|
|
|
|
// #3 Update: Update UpdateRange
|
|
|
- public static Task<int> UpdateAsync<Entity>(Entity entity) => DataProvider<Entity>().UpdateAsync<Entity>(entity);
|
|
|
- public static Task<int> UpdateRangeAsync<Entity>(IEnumerable<Entity> entities) => DataProvider<Entity>().UpdateRangeAsync<Entity>(entities);
|
|
|
+ public static Task<int> UpdateAsync<Entity>(Entity entity) => dataSource.UpdateAsync<Entity>(entity);
|
|
|
+ public static Task<int> UpdateRangeAsync<Entity>(IEnumerable<Entity> entities) => dataSource.UpdateRangeAsync<Entity>(entities);
|
|
|
|
|
|
|
|
|
// #4 Delete : Delete DeleteRange DeleteByKey DeleteByKeys
|
|
|
- public static Task<int> DeleteAsync<Entity>(Entity entity) => DataProvider<Entity>().DeleteAsync<Entity>(entity);
|
|
|
- public static Task<int> DeleteRangeAsync<Entity>(IEnumerable<Entity> entities) => DataProvider<Entity>().DeleteRangeAsync<Entity>(entities);
|
|
|
+ public static Task<int> DeleteAsync<Entity>(Entity entity) => dataSource.DeleteAsync<Entity>(entity);
|
|
|
+ public static Task<int> DeleteRangeAsync<Entity>(IEnumerable<Entity> entities) => dataSource.DeleteRangeAsync<Entity>(entities);
|
|
|
|
|
|
- public static Task<int> DeleteByKeyAsync<Entity>(object keyValue) => DataProvider<Entity>().DeleteByKeyAsync<Entity>(keyValue);
|
|
|
- public static Task<int> DeleteByKeysAsync<Entity, Key>(IEnumerable<Key> keys) => DataProvider<Entity>().DeleteByKeysAsync<Entity, Key>(keys);
|
|
|
+ public static Task<int> DeleteByKeyAsync<Entity>(object keyValue) => dataSource.DeleteByKeyAsync<Entity>(keyValue);
|
|
|
+ public static Task<int> DeleteByKeysAsync<Entity, Key>(IEnumerable<Key> keys) => dataSource.DeleteByKeysAsync<Entity, Key>(keys);
|
|
|
|
|
|
#endregion
|
|
|
|