|
@@ -1,217 +0,0 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Concurrent;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
-using System.Threading.Tasks;
|
|
|
-
|
|
|
-using Vit.Core.Util.ConfigurationManager;
|
|
|
-using Vit.Core.Util.Reflection;
|
|
|
-using Vit.Linq;
|
|
|
-
|
|
|
-using Vitorm.DataProvider;
|
|
|
-using Vitorm.Entity;
|
|
|
-
|
|
|
-namespace Vitorm
|
|
|
-{
|
|
|
- public partial class Data
|
|
|
- {
|
|
|
- static Data()
|
|
|
- {
|
|
|
- Init(Appsettings.json);
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// Data.Init("appsettings.Development.json")
|
|
|
- /// </summary>
|
|
|
- /// <param name="appsettingsFileName"></param>
|
|
|
- public static void Init(string appsettingsFileName)
|
|
|
- {
|
|
|
- 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();
|
|
|
-
|
|
|
- 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
|
|
|
- }
|
|
|
-
|
|
|
- public static bool AddDataSource(Dictionary<string, object> dataSourceConfig)
|
|
|
- {
|
|
|
- var provider = CreateDataProvider(dataSourceConfig);
|
|
|
- if (provider == null) return false;
|
|
|
-
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- #region LoadDataProvider
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// <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);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- #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>();
|
|
|
-
|
|
|
-
|
|
|
- // #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);
|
|
|
-
|
|
|
- // #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>();
|
|
|
-
|
|
|
-
|
|
|
- // #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);
|
|
|
-
|
|
|
-
|
|
|
- // #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 DeleteByKey<Entity>(object keyValue) => DataProvider<Entity>().DeleteByKey<Entity>(keyValue);
|
|
|
- public static int DeleteByKeys<Entity, Key>(IEnumerable<Key> keys) => DataProvider<Entity>().DeleteByKeys<Entity, Key>(keys);
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- #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>();
|
|
|
-
|
|
|
-
|
|
|
- // #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);
|
|
|
-
|
|
|
- // #2 Retrieve : Get Query
|
|
|
- public static Task<Entity> GetAsync<Entity>(object keyValue) => DataProvider<Entity>().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);
|
|
|
-
|
|
|
-
|
|
|
- // #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> 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);
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- }
|
|
|
-}
|