/// <summary> /// Set the current provider by attributes /// </summary> /// <param name="providersourcEnumDb">Provider source - MySql, MicrosoftSQL, PostgreSQL, OleDB, Odbc</param> /// <param name="connectionname">Unique connectionname</param> /// <param name="host">Host</param> /// <param name="port"></param> /// <param name="username"></param> /// <param name="passwaord"></param> public void SetCurrentProviderByAttributes(EnumDB providersourcEnumDb, string connectionname, string host, string port, string username, string passwaord, string databasename = "") { try { ushort hostport; bool isNumeric = ushort.TryParse(port, out hostport); CurrentProvider.ConStringPort = isNumeric ? hostport : Convert.ToUInt16(3306); CurrentProvider.Name = connectionname; CurrentProvider.EnumDb = providersourcEnumDb; CurrentProvider.ConStringServer = host; CurrentProvider.ConStringPW = passwaord; CurrentProvider.ConStringUserName = username; CurrentProvider.ConStringDataBase = databasename; CurrentProvider.SetConnectionString(); CurrentProvider.SetDBConnection(); CurrentProvider.SetQueries(); if (CurrentProvider.ProviderName == null) { CurrentProvider.SetProviderName(); } } catch (Exception) { throw; } }
/// <summary> /// 构造函数传入数据库类型 /// </summary> /// <param name="dbType"></param> public DBAccess(EnumDB dbType) { #region this._dbType = dbType; _executor = new Executor(dbType); #endregion }
public IGetOptions GetOptionsRepository(Type type, EnumDB db, bool enableCache = true) { var t = GetType(); var method = t.GetMethod("GetRepository", new Type[] { typeof(EnumDB), typeof(bool) }); return((IGetOptions)method.MakeGenericMethod(type).Invoke(this, new object[] { db, enableCache })); //return null; }
public void Save(EnumDB db) { using (new BLog(LoggerMEF, "Save", "::::::::::>")) { //StorageContext.SaveChangesAsync().Wait(); if (db == EnumDB.Content) { StorageContextContent.SaveChanges(); } else { StorageContext.SaveChanges(); } } }
/// <summary> /// 构造函数(根据数据库类型构建对象组) /// </summary> /// <param name="DBType"></param> public Executor(EnumDB dbType) { #region switch (dbType) { case EnumDB.SqlServer: this.initSql(); break; case EnumDB.Oracle: this.initOracle(); break; default: break; } #endregion }
public T GetRepository <T>(EnumDB db, bool enableCache = true) where T : IRepositorySetStorageContext { var cacheKey = db + " " + typeof(T).FullName; Logger.LogTrace("Storage::GetRepository CacheExists={cache} type={type} key=[{key}]", _cacheRepos.ContainsKey(cacheKey), typeof(T).Name, cacheKey); if (enableCache && _cacheRepos.ContainsKey(cacheKey)) { return((T)_cacheRepos[cacheKey]); } foreach (Type type in this.GetType().GetTypeInfo().Assembly.GetTypes()) { if (typeof(T).GetTypeInfo().IsAssignableFrom(type) && type.GetTypeInfo().IsClass) { T repository = (T)Activator.CreateInstance(type); if (db == EnumDB.Content) { if (StorageContextContent == null) { Logger.LogCritical("Запрашиваемый репозиторий ({0}) не может быть получен до подключения к БД с контентом! Для начала надо выполнить подключение к БД сайта ConnectToSiteDB(siteid)", typeof(T).FullName); throw new Exception("Could't recive content Repository before connect to DB!"); } repository.SetStorageContext(StorageContextContent, this, LoggerFactory, Localizer2Garin); } else { repository.SetStorageContext(StorageContext, this, LoggerFactory, Localizer2Garin); } // в кеш помещаем тока если разрешено брать из кеша. if (enableCache) { _cacheRepos[cacheKey] = repository; } return(repository); } } Logger.LogCritical("Can't find repository {0}", typeof(T).FullName); return(default(T)); }