程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> NopCommerce架構分析之(三)EntityFramework數據庫初試化及數據操作

NopCommerce架構分析之(三)EntityFramework數據庫初試化及數據操作

編輯:ASP.NET基礎

系統啟動時執行任務:IStartupTask,啟動時執行的任務主要是數據庫的初始化和加載。

IStartupTask調用IEfDataProvider進行數據庫的初始化。

IEfDataProvider,SqlCeDataProvider:獲取數據連接工廠,不同類型數據庫,連接工廠不同。

接口IStartupTask的實體類EfStartUpTask的實現如下:

public class EfStartUpTask : IStartupTask 
{ 
 public void Execute() 
 { 
  var settings = EngineContext.Current.Resolve<DataSettings>(); 
  if (settings != null && settings.IsValid()) 
  { 
   var provider = EngineContext.Current.Resolve<IEfDataProvider>(); 
   if (provider == null) 
    throw new NopException("No EfDataProvider found"); 
   provider.SetDatabaseInitializer(); 
  } 
 } 

 public int Order 
 { 
  //ensure that this task is run first 
  get { return -1000; } 
 } 
}

SqlCeInitializer,CreateCeDatabaseIfNotExists初始化數據庫。

IDbContext,NopObjectContext系統數據庫操作上下文。加載所有數據庫映射類:EntityTypeConfiguration<TEntityType>。代碼如下:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
 //dynamically load all configuration 
 //System.Type configType = typeof(LanguageMap); //any of your configuration classes here 
 //var typesToRegister = Assembly.GetAssembly(configType).GetTypes() 

 var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() 
 .Where(type => !String.IsNullOrEmpty(type.Namespace)) 
 .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); 
 foreach (var type in typesToRegister) 
 { 
 dynamic configurationInstance = Activator.CreateInstance(type); 
 modelBuilder.Configurations.Add(configurationInstance); 
 } 
 //...or do it manually below. For example, 
 //modelBuilder.Configurations.Add(new LanguageMap()); 



 base.OnModelCreating(modelBuilder); 
} 

此方法是繼承自DbContext。並在系統啟動時調用,建立數據表與實體的對應關系。

在類型依賴注冊類Nop.Web.Framework.DependencyRegistrar中實現數據庫工廠的創建、數據庫的加載。如下代碼:

//data layer 
var dataSettingsManager = new DataSettingsManager(); 
var dataProviderSettings = dataSettingsManager.LoadSettings(); 
builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>(); 
builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency(); 


builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency(); 
builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IEfDataProvider>().InstancePerDependency(); 

if (dataProviderSettings != null && dataProviderSettings.IsValid()) 
{ 
 var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings()); 
 var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider(); 
 dataProvider.InitConnectionFactory(); 

 builder.Register<IDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest(); 
} 
else 
{ 
 builder.Register<IDbContext>(c => new NopObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest(); 
} 


builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest(); 

接口IEfDataProvider 的實體類SqlServerDataProvider的數據庫初始化方法如下:

/// <summary> 
/// Set database initializer 
/// </summary> 
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<string>(); 
 //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests 
 customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/SqlServer.Indexes.sql"), false)); 
 //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests 
 customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/SqlServer.StoredProcedures.sql"), false)); 
 
 var initializer = new CreateTablesIfNotExist<NopObjectContext>(tablesToValidate, customCommands.ToArray()); 
 Database.SetInitializer(initializer); 
} 

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved