Blogengine.net-provider (Policy Mode)

Source: Internet
Author: User

Actually, after I learned about the business object class businessbase, I still have a blank brain and I am not doing enough internal work. Check how blogengine.net processes data.
The provider mode is actually the policy mode: defines the algorithm family and encapsulates them separately so that they can replace each other. This mode makes the algorithm changes independent of the customers who use the algorithm.
Let's look at how blogengine.net is implemented.

 

Looking at the face-to-face class diagram, two provider classes, one operation XML, and one operation database are implemented. The policy pattern is defined as an algorithm encapsulated separately and can be replaced with each other.
How is that independent of customers who use algorithms?

Blogengine.net defines a static class for the client to use. Here, a loadproviders method is used to load the provider (XML... or DB ..)

Loadproviders

 1  ///<summary>
2 /// Load the providers from the web.config.
3 ///</summary>
4 private static void LoadProviders()
5 {
6 // Avoid claiming lock if providers are already loaded
7 if (_provider == null)
8 {
9 lock (TheLock)
10 {
11 // Do this again to make sure _provider is still null
12 if (_provider == null)
13 {
14 // Get a reference to the <blogProvider> section
15 var section = (BlogProviderSection)WebConfigurationManager.GetSection("BlogEngine/blogProvider");
16
17 // Load registered providers and point _provider
18 // to the default provider
19 _providers = new BlogProviderCollection();
20 ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(BlogProvider));
21 _provider = _providers[section.DefaultProvider];
22
23 if (_provider == null)
24 {
25 throw new ProviderException("Unable to load default BlogProvider");
26 }
27 }
28 }
29 }
30 }
Blogprovider

<BlogEngine>
<blogProvider defaultProvider="XmlBlogProvider">
<providers>
<add name="XmlBlogProvider" type="BlogEngine.Core.Providers.XmlBlogProvider, BlogEngine.Core"/>
<add name="DbBlogProvider" type="BlogEngine.Core.Providers.DbBlogProvider, BlogEngine.Core" connectionStringName="BlogEngine"/>
</providers>
</blogProvider>
</BlogEngine>

You can see that the configuration file is read for loading.
Now we know that the client does not need to know whether the provider in blogservice is xmlblogprovider or dbblogprovider. We only need to call its method for crud. The data source will be changed later,
Add xxxblogprovider and modify the configuration file. No client code is required.

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.