系统启动时执行任务: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>。代码如下:
/// <summary>
/// DbModelBuilder 用于将 CLR 类映射到数据库架构。 此以代码为中心的方法称作“Code First”,可用于生成实体数据模型 (EDM) 模型。
/// </summary>
/// <param name="modelBuilder"></param>
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 => x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency(); if (dataProviderSettings != null && dataProviderSettings.IsValid()) { var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings()); var dataProvider = efDataProviderManager.LoadDataProvider(); dataProvider.InitConnectionFactory(); builder.Register<IDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerLifetimeScope(); } else { builder.Register<IDbContext>(c => new NopObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerLifetimeScope(); } builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
接口IEfDataProvider 的实体类SqlServerDataProvider的数据库初始化方法如下:
/// <summary> /// Set database initializer /// </summary> public virtual 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/Install/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/Install/SqlServer.StoredProcedures.sql"), false)); var initializer = new CreateTablesIfNotExist<NopObjectContext>(tablesToValidate, customCommands.ToArray()); Database.SetInitializer(initializer); }