/// <inheritdoc/> public IRepository SelectRepository(Type entityType, bool memRepositoryEnabled = true) { var mainRepository = _mainRepositoryMapping[entityType]; // 是否启用了 MemoryRepository? if (memRepositoryEnabled) { var isStaticEntity = PersistenceConfig.IsStaticConfigTable(entityType); var isDynamicEntity = PersistenceConfig.IsDynamicConfigTable(entityType); // 如果为“静态配置表” || “为动态配置表且此表所在的远程库没有连接” if (isStaticEntity || (isDynamicEntity && !mainRepository.Connected)) { return(this.MemoryRepository); } else { return(_mainRepositoryMapping[entityType]); } } else { // 主用库是否连接? if (mainRepository.Connected) { return(mainRepository); } else { _backupRepositoryMapping.TryGetValue(entityType, out Repository backupRepository); return(backupRepository); } } }
private void InitializeEnvironment(bool enableRemoteDb) { // 数据库日志配置 Database.UseLog(LoggerNames.Persistence); // 配置SQLite 底层的dll 对应的环境变量(主要是为了加载该底层的dll) Environment.SetEnvironmentVariable("PreLoadSQLite_BaseDirectory", HelperTools.CurrentDllPath); // 初始化配置。 PersistenceConfig.Initialize(enableRemoteDb); }
/// <summary> /// Close manager. /// </summary> public void Close() { // _repositories.Values.ForEach(p => p.Dispose()); _repositories.Clear(); // DbConnectionMonitor.Current.Close(); PersistenceConfig.Close(); }
/// <summary> /// /// </summary> public void Update <T>(object instance, Expression <Func <T, bool> > predicate) where T : Entity { // 如果是静态数据,则不允许此操作。 var entityType = typeof(T); if (PersistenceConfig.IsStaticConfigTable(entityType)) { throw new InvalidOperationException(string.Format($"{entityType.Name} 为静态配置数据,无法执行Update操作。")); } var theRepository = _repositorySelector.SelectRepository(typeof(T)); if (theRepository == null) { throw new InvalidOperationException(); } else { theRepository.Update(instance, predicate); } }
/// <summary> /// /// </summary> public void AsyncInsert <T>(T[] entities, Action <Exception> exceptionHandler) where T : Entity { // 如果是静态数据,则不允许此操作。 var entityType = typeof(T); if (PersistenceConfig.IsStaticConfigTable(entityType)) { throw new InvalidOperationException(string.Format($"{entityType.Name} 为静态配置数据,无法执行AsyncInsert操作。")); } var theRepository = _repositorySelector.SelectRepository(typeof(T)); if (theRepository == null) { throw new InvalidOperationException(); } else { theRepository.AsyncInsert(entities, exceptionHandler); } }
private void CreateRepositories(bool enableRemoteDb) { var dataSources = PersistenceConfig.GetDataSources(); // 是否启用远程数据库? if (!enableRemoteDb) { dataSources = dataSources.Where(p => !PersistenceConfig.IsRemoteDatabase((DataBaseType)p.DbType)); } // 内置 MemoryRepository。 var memoryDataSource = new DataSource() { Name = DataBaseType.Memory.ToString(), DbType = (int)DataBaseType.Memory, }; dataSources = dataSources.Union(new DataSource[] { memoryDataSource }); // 创建 Repository。 _repositories = RepositoryFactory.Create(dataSources, _repositorySelector).ToDictionary(p => p.DataSource.Name, q => q); }