IStartupTask連線資料庫的方法

來源:果殼範文吧 8.02K

系統啟動時執行任務:IStartupTask,啟動時執行的任務主要是資料庫的初始化和載入。

IStartupTask連線資料庫的方法

IStartupTask呼叫IEfDataProvider進行資料庫的初始化。

IEfDataProvider,SqlCeDataProvider:獲取資料連線工廠,不同型別資料庫,連線工廠不同。

介面IStartupTask的.實體類EfStartUpTask的實現如下:

public class EfStartUpTask : IStartupTask { public void Execute() { var settings = lve(); if (settings != null && lid()) { var provider = lve(); if (provider == null) throw new NopException("No EfDataProvider found"); atabaseInitializer(); } } public int Order { //ensure that this task is run first get { return -1000; } } }

SqlCeInitializer,CreateCeDatabaseIfNotExists初始化資料庫。

IDbContext,NopObjectContext系統資料庫操作上下文。載入所有資料庫對映類:EntityTypeConfiguration。程式碼如下:

protected override void OnModelCreating(DbModelBuilder modelBuilder) { //dynamically load all configuration // configType = typeof(LanguageMap); //any of your configuration classes here //var typesToRegister = ssembly(configType)ypes() var typesToRegister = xecutingAssembly()ypes() e(type => !llOrEmpty(space)) e(type => Type != null && nericType && enericTypeDefinition() == typeof(EntityTypeConfiguration<>)); foreach (var type in typesToRegister) { dynamic configurationInstance = teInstance(type); (configurationInstance); } // do it manually below. For example, //(new LanguageMap()); delCreating(modelBuilder); }

此方法是繼承自DbContext。並在系統啟動時呼叫,建立資料表與實體的對應關係。

在型別依賴註冊類ndencyRegistrar中實現資料庫工廠的建立、資料庫的載入。如下程式碼:

//data layer var dataSettingsManager = new DataSettingsManager(); var dataProviderSettings = Settings(); ster(c => Settings())(); ster(x => new EfDataProviderManager(lve()))()ancePerDependency(); ster(x => (IEfDataProvider)lve()DataProvider())()ancePerDependency(); ster(x => (IEfDataProvider)lve()DataProvider())()ancePerDependency(); if (dataProviderSettings != null && lid()) { var efDataProviderManager = new EfDataProviderManager(Settings()); var dataProvider = (IEfDataProvider)DataProvider(); ConnectionFactory(); ster(c => new NopObjectContext(ConnectionString))ancePerHttpRequest(); } else { ster(c => new NopObjectContext(Settings()ConnectionString))ancePerHttpRequest(); } sterGeneric(typeof(EfRepository<>))(typeof(IRepository<>))ancePerHttpRequest();

介面IEfDataProvider 的實體類SqlServerDataProvider的資料庫初始化方法如下

///

/// Set database initializer ///

public override void SetDatabaseInitializer() { //pass some table names to ensure that we have nopCommerce 2.X installed var tablesToValidate = new[] {"Customer", "Discount", "Order", "Product", "ShoppingCartItem"}; //custom commands (stored proedures, indexes) var customCommands = new List

(); //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/"), false)); //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/"), false)); var initializer = new CreateTablesIfNotExist(tablesToValidate, ray()); nitializer(initializer); }

另外,EntityFramework本事是ORM框架,通過資料庫訪問上下文建立與資料庫的連線及實體與資料表的對應廣西。並通過建立IRepository的泛型實體類來實現對每一種資料的處理,也就是所謂的Dao層。業務邏輯層通過每種實體的資料訪問倉庫Repository來進行資料庫操作。

熱門標籤