In-depth analysis of byteart retail case: application configuration

Source: Internet
Author: User

Byteart retail provides a relatively simple configuration method: For applicationsProgramAll the required parameters are configured through the Web. config file. This is different from the configuration of the framework system. The framework system should provide at least two Configuration Methods: writing directlyCodeAnd the Web/APP. config method, which is designed to cater to different use scenarios. As a framework, it provides a support platform for the proper running of applications, so it needs to provide developers with a configuration method for writing code directly to meet the needs of testing. At the same time, in the production environment, you should also be able to use the configuration file (. the standard practice of net is to use web/app. config file. As a demo of an enterprise-level application, byteart retail does not design too much in the configuration section. It only shows how to configure a common. NET application.

Review the standard practices of. NET application configuration

In. in. NET applications, the standard configuration methods are Web/app. config File, specifically web. config or app. the main program type is web application or common Windows application. If you only need to provide some key-Value Pair configuration information for the application, you can add these key-value pairs directly under the <etettings> node in Web/APP. config using the Add Tag. It is also very convenient to use, such:

 
VaR value = system. configuration. configurationmanager. receivettings ["key"];

Remember to add a reference to the system. Configuration assembly before use.

Another complicated implementation method is the configuration section. configuration. configurationsection inherits a type (for example, byteartretailconfigsection). In this type, you can use configurationpropertyattribute to mark some attributes, indicating that these attributes correspond to a node in the configuration section. Of course, these property types can be simple data types, can be subclasses of configurationelement, or can be subclasses of configurationelementcollection. Finally, declare the new configuration section under the <configsections> node in Web/APP. config to configure it. You can use system. configuration. configurationmanager. getsection to read the configuration information.

The above two methods are the most common. Of course there are some more complicated declaration and definition methods in the configuration section, so we will not discuss them here. There are very detailed documents on msdn for reference. Next, let's take a look at how the configuration information is defined in byteart retail.

Implementation of configuration in byteart retail

The configuration section of byteart retail defines the following information: 1. Permission key value of the role (this will be introduced in another section ); 2. display parameters of data tables on the home page (mainly paging parameters); 3. Email configuration. It roughly indicates the configuration information structure defined by byteart retail:

Next, we can develop our configuration part based on the structure design of the configuration information.

Configuration section of the development case using the configuration section designer plug-in

I think laziness is not a bad thing. Clever and flexible use of tools in daily work can not only reduce the workload of repetitive work, but also reduce the chance of errors and get twice the result with half the effort. Test. the implementation of the application configuration section under. NET is not difficult to understand: in fact, configuration element and configuration element collection are implemented throughout the development, and then the configuration section is used to organize the entire configuration information structure. In this process, we need to manually define many classes inherited from configurationelement and configurationelementcollection, add many attributes to these classes, and implement many methods. The configuration information structure of byteart retail is relatively simple. It only contains three configuration elements and one configuration element collection. If some frameworks or applications require a large amount of configuration information input, the development of such a Configuration component requires a lot of manpower.

Another problem with coding manually is that our configuration information tends to be in a tree structure. Therefore, once the configuration structure becomes complex, the Code maintenance phase will be dazzled. To understand how the configuration information of an application is organized, the programmer may need to begin with the implementation of the configuration section and gradually clarify the associations between various parts, this undoubtedly puts a lot of burden on code reading.

Therefore, in the byteart retail case, I chose a set of Visual Studio extensions named configuration section designer (http://csd.codeplex.com), not only free, in addition, Visual Studio 2005/2008/2010/2012 and other versions are supported. A long time ago, I have been introducing a useful version based on.. Net configuration file design tool.

In the byteartretail. Infrastructure. config directory, there is a byteartretailconfigsection. CSD file with the following structure:

This is the design file of configuration section designer, and the rest are background associated files, including the following four types of files:

    • Byteartretailconfigsection. CSD. config: This is a configuration file for testing. After designing your configuration information structure, you can open this file and enter some content for testing. Of course, it is basically useless.
    • Byteartretailconfigsection. CSD. CS: automatically generated C # code file. It defines configurationsection, configurationelement, and configurationelementcollection according to your design.
    • Byteartretailconfigsection. CSD. digoal: the designer's graphic file defines some metadata used by the designer. You can simply double-click the. CSD file to open the. CSD file.
    • Byteartretailconfigsection. CSD. XSD: The generated XSD file. You can apply the XSD file in the configuration file. Therefore, when you edit the configuration section, Visual Studio automatically enables intelliisense to help you simplify editing.

Double-click the byteartretailconfigsection. CSD file to view the following design interface:

Is the structure of this design the same as the configuration information structure discussed above?

Note that if your Visual Studio does not have configuration section designer installed, you can still compile the entire byteart retail solution. The difference is that you will not be able to open such a designer.

In fact, this is another application of "domain-specific language (DSL)" (a kind of application that has been introduced in my previous blog, that is, fluent Interface ): this CSD file contains the definition of such a language: This language is used to describe.. NET application configuration. Developers only need to know. net, you can use this domain-specific language to define a configuration information structure. The rest of the work is through template conversion, translate the language into C # code.

Use configuration information in applications

To simplify the reading of configuration information, I implemented a single-piece type in byteart retail: byteartretail. Infrastructure. config. byteartretailconfigurationreader. The implementation of this single piece type is also very simple, that is, what configuration data the application needs to read and what type of attribute values this type returns. One advantage of using a single piece is that you do not need to read the configuration file repeatedly, but you can also use static fields.

Summary

This article introduces the implementation of the configuration section in byteart retail, and also introduces the use of a DSL tool for configuration information structure design, it aims to let readers know the advantages of DSL and its importance in the software development field. We hope that this article will broaden your horizons and help you improve development efficiency in your actual work.

Note

In-depth analysis byteart retail SeriesArticleThe application layer is not introduced yet. I 'd like to learn about it here to see which parts of this case are still of interest. Please leave a message here. However, we try to use byteart retail as the case. I will reply to you directly for some technical questions about architecture design.

In addition, I am sorting out the PDF files of this series of articles. After the full series is complete, I Will synchronously publish this PDF file for you to download. For friends who want to pass the time on the bus or subway, I think you should like this PDF file.

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.