public void AddOrUpdate(IDataSetting dataSetting) { Check.NotNull(dataSetting, nameof(dataSetting)); if (string.IsNullOrEmpty(dataSetting.Name)) { throw new MapleException("未设置DataSetting名称"); } if (this.CheckDisposed()) { throw new ObjectDisposedException("DataProviderFactory"); } object sync = this._sync; lock (sync) { if (!_dataProviders.ContainsKey(dataSetting.Name)) { _dataProviders.Add(dataSetting.Name, dataSetting); } else { _dataProviders[dataSetting.Name] = dataSetting; } } }
public IDataProvider GetDataProvider(string dataSettingName) { IDataSetting dataSetting = this.dataSettingContainer.GetDataSetting(dataSettingName); if (dataSetting == null) { throw new MapleException($"未设置[{dataSettingName}]对应的数据库配置信息"); } IDbTranslator dbTranslator = getDbTranslator(dataSetting); var databaseContext = new InternalDatabaseContext(dataSetting, dbTranslator); return(new InternalDataProvider(databaseContext)); }
/// <summary> /// 保持配置信息 /// </summary> /// <param name="settings">Data settings</param> public virtual void SaveSettings(IDataSetting settings) { Singleton <IDataSetting> .Instance = settings ?? throw new ArgumentNullException(nameof(settings)); var filePath = CommonHelper.MapPath(DataSettingsFilePath); //如果文件不存在则创建之 if (!File.Exists(filePath)) { using (File.Create(filePath)) { } } //保存信息 var text = JsonConvert.SerializeObject(Singleton <DataSetting> .Instance, Formatting.Indented); File.WriteAllText(filePath, text); }
private IDbTranslator getDbTranslator(IDataSetting dataSetting) { IDbTranslator dbTranslator; switch (dataSetting.DataSouceType) { case DataSouceType.MySQL: dbTranslator = new MySQLTranslator(); break; case DataSouceType.Oracle: dbTranslator = new OracleTranslator(); break; case DataSouceType.Sql2000: dbTranslator = new Sql2000Translator(); break; case DataSouceType.Sql2005: dbTranslator = new Sql2005Translator(); break; case DataSouceType.Sql2008: dbTranslator = new Sql2008Translator(); break; case DataSouceType.Sqlite: dbTranslator = new SqliteTranslator(); break; default: throw new MapleException(string.Format("未知的数据源类型“{0}”。请核实数据源类型配置是否正确。", dataSetting.DataSouceType)); } return(dbTranslator); }
/// <summary> /// 数据库上线文构造函数 /// </summary> /// <param name="dataSetting"></param> /// <param name="dbTranslator"></param> public InternalDatabaseContext(IDataSetting dataSetting, IDbTranslator dbTranslator) { this._dataSetting = dataSetting; this.DbTranslator = dbTranslator; }