欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-19 21:54:16
系统启动时执行任务:istartuptask,启动时执行的任务主要是数据库的初始化和加载。 istartuptask调用iefdataprovider进行数据库的初始化。...

系统启动时执行任务: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>来进行数据库操作。

上一篇:

下一篇: