Three---of nopcommerce architecture analysis and data manipulation of database

Source: Internet
Author: User
Tags nopcommerce

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
  1. Public class Efstartuptask:istartuptask
  2. {
  3. public void Execute ()
  4. {
  5. var settings = enginecontext.current.resolve<datasettings> ();
  6. if (settings ! = null && settings. IsValid ())
  7. {
  8. var Provider = enginecontext.current.resolve<iefdataprovider> ();
  9. if (Provider = = null)
  10. throw New Nopexception ("No Efdataprovider found");
  11. Provider. Setdatabaseinitializer ();
  12. }
  13. }
  14. public int Order
  15. {
  16. //ensure that this task is run first
  17. get { return-1000;}
  18. }
  19. }



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
  1. Protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
  2. {
  3. //dynamically Load all configuration
  4. //system.type configtype = typeof (Languagemap); Any of your configuration classes here
  5. //var typestoregister = assembly.getassembly (Configtype). GetTypes ()
  6. var typestoregister = assembly.getexecutingassembly (). GetTypes ()
  7. . Where (type =!) String.IsNullOrEmpty (type. Namespace))
  8. . Where (Type = = Type. BaseType ! = null && type. Basetype.isgenerictype && type.  Basetype.getgenerictypedefinition () = = typeof (entitytypeconfiguration<>));
  9. foreach (var type in typestoregister)
  10. {
  11. Dynamic configurationinstance = Activator.CreateInstance (type);
  12. MODELBUILDER.CONFIGURATIONS.ADD (configurationinstance);
  13. }
  14. //...or do it manually below. For example,
  15. //modelbuilder.configurations.add (New Languagemap ());
  16. base.  Onmodelcreating (ModelBuilder);
  17. }


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
  1. Data layer
  2. var datasettingsmanager = new Datasettingsmanager ();
  3. var dataprovidersettings = datasettingsmanager.loadsettings ();
  4. Builder. Register (c => Datasettingsmanager.loadsettings ()).  As<datasettings> ();
  5. Builder. Register (x => New Efdataprovidermanager (x.resolve<datasettings> ())). As<Basedataprovidermanager> ().  Instanceperdependency ();
  6. Builder. Register (x => (iefdataprovider) x.resolve<basedataprovidermanager> (). Loaddataprovider ()). As<idataprovider> ().  Instanceperdependency ();
  7. Builder. Register (x => (iefdataprovider) x.resolve<basedataprovidermanager> (). Loaddataprovider ()). As<iefdataprovider> ().  Instanceperdependency ();
  8. if (dataprovidersettings! = null && dataprovidersettings.isvalid ())
  9. {
  10. var efdataprovidermanager = new Efdataprovidermanager (Datasettingsmanager.loadsettings ());
  11. var dataprovider = (iefdataprovider) efdataprovidermanager.loaddataprovider ();
  12. Dataprovider.initconnectionfactory ();
  13. Builder. Register<idbcontext> (c => New Nopobjectcontext ( dataprovidersettings.dataconnectionstring)).  Instanceperhttprequest ();
  14. }
  15. Else
  16. {
  17. Builder. Register<idbcontext> (c => New Nopobjectcontext (datasettingsmanager.loadsettings). dataconnectionstring)).  Instanceperhttprequest ();
  18. }
  19. 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
  1. <summary>
  2. // Set database initializer
  3. // </summary>
  4. public override void Setdatabaseinitializer ()
  5. {
  6. //pass Some table names to ensure so we have Nopcommerce 2.X installed
  7. var tablestovalidate = new[] {"Customer", "Discount", "Order", "Product", "Shoppingcartitem"};
  8. //custom commands (stored proedures, indexes)
  9. var customcommands = new list<string> ();
  10. //use Webhelper.mappath instead of Hostingenvironment.mappath which is not available in unit tests
  11. Customcommands.addrange (Parsecommands (Hostingenvironment.mappath ("~/app_data/sqlserver.indexes.sql"),   false));
  12. //use Webhelper.mappath instead of Hostingenvironment.mappath which is not available in unit tests
  13. Customcommands.addrange (Parsecommands (Hostingenvironment.mappath ("~/app_data/  SqlServer.StoredProcedures.sql "), false));
  14. var initializer = new createtablesifnotexist<nopobjectcontext> (Tablestovalidate, Customcommands.toarray ()  );
  15. Database.setinitializer (initializer);
  16. }



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.