Perform tasks at system startup: Istartuptask, the task performed at startup is primarily the initialization and loading of the database.
Istartuptask calls Iefdataprovider for initialization of the database.
Iefdataprovider,sqlcedataprovider: Get data connection factory, different type database, Connection factory is different.
The implementation of the entity class Efstartuptask for Interface Istartuptask is as follows:
[CSharp]View PlainCopy
- 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 initializes the database.
Idbcontext,nopobjectcontext The System database operation context. Load all database mapping classes:entitytypeconfiguration<tentitytype>. The code is as follows:
[CSharp]View PlainCopy
- 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);
- }
This method is inherited from DbContext. and is called when the system starts, and establishes the correspondence between the data table and the entity.
The creation of the database factory and the loading of the database are implemented in the type-dependent registration class Nop.Web.Framework.DependencyRegistrar. The following code:
[HTML]View PlainCopy
- 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 ();
The database initialization method for the entity class Sqlserverdataprovider of the interface Iefdataprovider is as follows:
[CSharp]View PlainCopy
- <summary>
- // Set database initializer
- // </summary>
- public override void Setdatabaseinitializer ()
- {
- //pass Some table names to ensure so 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);
- }
In addition, EntityFramework is an ORM framework that establishes a connection to the database and the corresponding entity to the data table through the database access context Guangxi. and the processing of each kind of data is realized by creating the generic entity class of irepository<t>, which is called the DAO layer. The business logic layer carries out database operations through the data Access warehouse repository<t> for each entity. Such as:
Three---of nopcommerce architecture analysis and data manipulation of database