主页 > 知识库 > NopCommerce架构分析之(三)EntityFramework数据库初试化及数据操作

NopCommerce架构分析之(三)EntityFramework数据库初试化及数据操作

热门标签:百度竞价排名 硅谷的囚徒呼叫中心 集中运营管理办法 科大讯飞语音识别系统 阿里云 网站排名优化 地方门户网站 服务器配置

系统启动时执行任务:IStartupTask,启动时执行的任务主要是数据库的初始化和加载。

IStartupTask调用IEfDataProvider进行数据库的初始化。

IEfDataProvider,SqlCeDataProvider:获取数据连接工厂,不同类型数据库,连接工厂不同。

接口IStartupTask的实体类EfStartUpTask的实现如下:

public class EfStartUpTask : IStartupTask 
{ 
 public void Execute() 
 { 
  var settings = EngineContext.Current.ResolveDataSettings>(); 
  if (settings != null  settings.IsValid()) 
  { 
   var provider = EngineContext.Current.ResolveIEfDataProvider>(); 
   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系统数据库操作上下文。加载所有数据库映射类:EntityTypeConfigurationTEntityType>。代码如下:

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()).AsDataSettings>(); 
builder.Register(x => new EfDataProviderManager(x.ResolveDataSettings>())).AsBaseDataProviderManager>().InstancePerDependency(); 


builder.Register(x => (IEfDataProvider)x.ResolveBaseDataProviderManager>().LoadDataProvider()).AsIDataProvider>().InstancePerDependency(); 
builder.Register(x => (IEfDataProvider)x.ResolveBaseDataProviderManager>().LoadDataProvider()).AsIEfDataProvider>().InstancePerDependency(); 

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

 builder.RegisterIDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest(); 
} 
else 
{ 
 builder.RegisterIDbContext>(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 Liststring>(); 
 //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 CreateTablesIfNotExistNopObjectContext>(tablesToValidate, customCommands.ToArray()); 
 Database.SetInitializer(initializer); 
} 

另外,EntityFramework本事是ORM框架,通过数据库访问上下文建立与数据库的连接及实体与数据表的对应广西。并通过创建IRepositoryT>的泛型实体类来实现对每一种数据的处理,也就是所谓的Dao层。业务逻辑层通过每种实体的数据访问仓库RepositoryT>来进行数据库操作。

您可能感兴趣的文章:
  • 详解如何在ASP.NET Core中应用Entity Framework
  • EntityFramework 6.x学习之多个上下文迁移实现分布式事务详解

标签:西双版纳 甘孜 广西 威海 乌兰察布 梧州 开封 随州

巨人网络通讯声明:本文标题《NopCommerce架构分析之(三)EntityFramework数据库初试化及数据操作》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266