Blogengine. NET architecture and source code analysis series Part3: Data Storage-implementation based on the provider Mode

Source: Internet
Author: User
Tags blogengine

In the previous article, we mainly analyzed the overall design of blogengine. net. In the second part, we also made some in-depth discussions on the State maintenance of business objects in business. In this article, I will guide you through {
Tagshow (Event)
} "> Storage design ideas and implementation details analysis.

The data storage in blogengine. NET is mainly implemented in the provider mode, so let's first understand the provider mode.

The provider mode should be {
Tagshow (Event)
} "> Design patterns are used to solve software changes. However, its focus (or dimension) is on the separation of function declaration and function implementation. In general, the system may have relatively stable requirements for a certain function (for example, each system requires {
Tagshow (Event)
} "> Verification, this is relatively stable), and the specific implementation of this function can be diverse (for example, you can go to the database to match the user for verification, you can also match it in an XML file ). Provider mode in. the design of the net class library can be seen everywhere, such as membershipprovider and sitemapprovider. Its appearance makes our applications more scalable. Note that the provider can be a provider of a Data factory, it can also be a provider of logical processing. What we see in blogengine. NET is the data factory provider. For the logic processing provider, you can refer to the implementation of the encrypted block in the Microsoft Enterprise Library applicationblock.
In. net, it is quite simple to implement this mode, because it has already implemented a part for us. We only need to implement the following three steps:
1. Define a class and abstract the operations we need. Its base class is providerbase.
2. Implement a section {
Tagshow (Event)
} "> The configuration file reads the relevant configurations of the provider. This class inherits from the configurationsection.
3. Read the configuration file and load the specified provider during the call.

What do I think of the data storage part in blogengine. Net )?

Blogengine. Net supports multiple types of data storage. in its current version, we can see the XML (default, mainly {
Tagshow (Event)
} "> Plug-and-play during installation) and database storage methods. Blogengine.. NET data storage provider is only for how to store part of the data (does not involve some logic processing and operations), so the database requirements are very low, you only need to support SQL statements and store data. In fact, it only has some tables in the database. From the implementation of its provider, it does not use the primary key cascade deletion function of the database, the full use of multiple embedded SQL statements can enable blogengine. net supports more databases. For database storage blogengine. net only uses one dbblogprovider, but does not specifically distinguish which database is used. We only need to set providername in the configuration file based on the link string to specify the specific database storage. Blogengine. NET is not recommended for data processing methods in systems with complicated business logic.

Let's take a look at how providerbase is applied in blogengine. Net to complete data storage.

First, let's look at an inheritance relationship diagram:

Attachment: provider.jpg

We can see that both dbblogprovider and xmlblogprovider inherit the abstract class blogprovider (including some operation declarations of business classes or other classes), while blogprovider directly inherits from providerbase, and providerbase is Microsoft's standard provider {
Tagshow (Event)
} "> Framework, we only need to follow this {
Tagshow (Event)
} "> Model development is enough. This standard provider framework has solved many problems, such as dependency injection. To use.. NET platform has resources for the role and member blogengine. net. NET provides membershipprovider and roleprovider, blogengine. in net, dbmembershipprovider and xmlmembershipprovider inherit from membershipprovider, dbroleprovider, and xmlroleprovider inherit from roleprovider ,. net's membershipprovider and roleprovider also inherit from providerbase. The class blogprovidersection is used to solve the configuration problem, that is, the second step in the provider mode implementation in. net. This effectively solves the dependency injection problem. Blogprovider defines standard operation methods for some business classes:

  1. 1 // page
  2. 2 /// <summary>
  3. 3 // retrieves a page from the provider based on the specified ID.
  4. 4 /// </Summary>
  5. 5 public abstract page selectpage (guid ID );
  6. 6 /// <summary>
  7. 7 // inserts a new page into the data store specified by the provider.
  8. 8 /// </Summary>
  9. 9 public abstract void insertpage (page );
  10. 10 /// <summary>
  11. 11 // updates an existing page in the Data Store specified by the provider.
  12. 12 /// </Summary>
  13. 13 public abstract void updatepage (page );
  14. 14 /// <summary>
  15. 15 // deletes a page from the data store specified by the provider.
  16. 16 /// </Summary>
  17. 17 public abstract void deletepage (page );
  18. 18 /// <summary>
  19. 19 // Retrieves all pages from the provider and returns them in a list.
  20. 20 /// </Summary>
  21. 21 public abstract list <page> fillpages ();

Copy code

Subclass only needs to override these methods.

In addition, in blogengine. net also has a relatively recommended processing, because blogprovider needs to process a lot of business data operations, there will be a lot of method members, so the implementation of xmlblogprovider adopts the partial class solution.

Dbblogprovider overrides the initialize method of providerbase to obtain the link string, table prefix, and field prefix. This design may be useful due to blogengine. net databases only support data storage. Users may directly deploy the database to an existing database. to distinguish it from other tables in the existing database, we can specify a table name prefix. The Processing Method for dbroleprovider and dbmembershipprovider is similar. In implementation, blogengine. net seems to have considered mono, and some code may have a judgment on Mono runtime, but I have not tried whether it can be installed across platforms. If you are interested, you can study it!

How does the client use blogprovider?

The use of membershipprovider and roleprovider is not described here. You can refer to the msdn document, which is also used by blogengine. net. Here we will mainly discuss the use of blogprovider. blogengine. NET provides a static class of blogservice, which provides a unified data access to the outside world {
Tagshow (Event)
} "> Interface, which calls blogprovider for internal static methods. There is a loadproviders method in blogservice to dynamically load blogprovider according to the configuration file:

Blogservice

  1. 1 Provider Model # region Provider Model
  2. 2
  3. 3 Private Static blogprovider _ provider;
  4. 4 Private Static blogprovidercollection _ providers;
  5. 5 private static object _ Lock = new object ();
  6. 6
  7. 7/*** // <summary>
  8. 8 // gets the current provider.
  9. 9 /// </Summary>
  10. 10 public static blogprovider
  11. 11 {
  12. 12 get {loadproviders (); Return _ provider ;}
  13. 13}
  14. 14
  15. 15/*** // <summary>
  16. 16 // gets a collection of all registered providers.
  17. 17 /// </Summary>
  18. 18 public static blogprovidercollection providers
  19. 19 {
  20. 20 get {loadproviders (); Return _ providers ;}
  21. 21}
  22. 22
  23. 23/** // <summary>
  24. 24 // load the providers from the Web. config.
  25. 25 /// </Summary>
  26. 26 Private Static void loadproviders ()
  27. 27 {
  28. 28 // avoid claiming lock if providers are already loaded
  29. 29 If (_ provider = NULL)
  30. 30 {
  31. 31 lock (_ Lock)
  32. 32 {
  33. 33 // do this again to make sure _ provider is still null
  34. 34 if (_ provider = NULL)
  35. 35 {
  36. 36 // get a reference to the <blogprovider> section
  37. 37 blogprovidersection section = (blogprovidersection) webconfigurationmanager. getsection ("blogengine/blogprovider ");
  38. 38
  39. 39 // load registered providers and point _ provider
  40. 40 // to the default provider
  41. 41 _ providers = new blogprovidercollection ();
  42. 42 providershelper. instantiateproviders (section. Providers, _ providers, typeof (blogprovider ));
  43. 43 _ provider = _ providers [section. defaultprovider];
  44. 44
  45. 45 if (_ provider = NULL)
  46. 46 throw new providerexception ("unable to load default blogprovider ");
  47. 47}
  48. 48}
  49. 49}
  50. 50}
  51. 51
  52. 52 # endregion

Copy code

Finally, let's take a look at the corresponding configuration of Web. config.

Blogproviderconfig

  1. 1 <configsections>
  2. 2 <sectiongroup name = "blogengine">
  3. 3 <section name = "blogprovider" requirepermission = "false" type = "blogengine. Core. providers. blogprovidersection, blogengine. Core" allowdefinition = "machinetoapplication" restartonexternalchanges = "true"/>
  4. 4 </sectiongroup>
  5. 5 </configsections>
  6. 6 <blogengine>
  7. 7 <blogprovider defaprovider provider = "xmlblogprovider">
  8. 8 <providers>
  9. 9 <Add name = "xmlblogprovider" type = "blogengine. Core. providers. xmlblogprovider, blogengine. Core"/>
  10. 10 <Add name = "dbblogprovider" type = "blogengine. Core. providers. dbblogprovider, blogengine. Core" connectionstringname = "blogengine"/>
  11. 11 </providers>
  12. 12 </blogprovider>
  13. 13 </blogengine>

Copy code

Membershipproviderconfig and roleproviderconfig

  1. 1 <membership defaultprovider = "xmlmembershipprovider">
  2. 2 <providers>
  3. 3 <clear/>
  4. 4 <Add name = "xmlmembershipprovider" type = "blogengine. Core. providers. xmlmembershipprovider, blogengine. Core" Description = "XML membership provider" passwordformat = "hashed"/>
  5. 5 <Add name = "sqlmembershipprovider" type = "system. Web. Security. sqlmembershipprovider" connectionstringname = "blogengine" applicationname = "blogengine"/>
  6. 6 <Add name = "dbmembershipprovider" type = "blogengine. Core. providers. dbmembershipprovider, blogengine. Core" passwordformat = "hashed" connectionstringname = "blogengine"/>
  7. 7 </providers>
  8. 8 </Membership>
  9. 9
  10. 10 <rolemanager defaultprovider = "xmlroleprovider" enabled = "true" cacherolesincookie = "true" cookiename = ". blogengineroles">
  11. 11 <providers>
  12. 12 <clear/>
  13. 13 <Add name = "xmlroleprovider" type = "blogengine. Core. providers. xmlroleprovider, blogengine. Core" Description = "XML role provider"/>
  14. 14 <Add name = "sqlroleprovider" type = "system. Web. Security. sqlroleprovider" connectionstringname = "blogengine" applicationname = "blogengine"/>
  15. 15 <Add name = "dbroleprovider" type = "blogengine. Core. providers. dbroleprovider, blogengine. Core" connectionstringname = "blogengine"/>
  16. 16 </providers>
  17. 17 </rolemanager>

Copy code

Summary

Blogengine. the provider mode is applied to the implementation of net data storage. NET platform has good support and cases, this design idea is worth learning from, especially the blogservice design is very clever, but also a little service-oriented taste. However, when some business logic is very complex, especially when some systems are applied to database logic processing, this design may not meet the requirements, we must also adhere to the principle that the closer the data is to the better.

Take its essence!

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.