Use MEF to set common parameters and mef universal Parameters

Source: Internet
Author: User

Use MEF to set common parameters and mef universal Parameters

The necessary functional modules of the general background management system include log management, permission management, data dictionary, and parameter configuration. Parameter settings are mainly used to set basic configuration items required for system operation, such as redis cache, mq message queue, and system version. The following parameters must be set:1. Easy to use2. Powerful functions to facilitate expansion3. Beautiful interface.This article will show you how to set common parameters. Before reading this article, you need to learn about ASP. net mvc, Entity Framework, and MEF. Online Preview address: http://config.myscloud.cn

Reading directory

  • Add and use configuration items
  • Implementation
  • Key code parsing
  • Summary
Go back to the top to add and use configuration items

To verify that the system has achieved these goals1. Easy to use2. Powerful functions to facilitate expansion3. Beautiful InterfaceThe following example shows how to add a configuration item and how to use it.

1. Add a configuration item Group

You only need to add oneConfigOptionThe class of the abstract class, which is inherited fromConfigOptionClass isConfiguration item Group.

/// <Summary> /// test configuration information /// </summary> [Export (typeof (ConfigOption)] [ConfigType (Group = "TestConfig ", groupCn = "test configuration item", ImmediateUpdate = true)] public class TestConfig: configOption {/// <summary> /// whether to record execution SQL // </summary> [Config (Name = "record execution SQL", DefaultValue = false)] public static bool IfLogSQL {get; set ;}}
Note: inherit from the ConfigOption abstract class and add the ConfigTypeAttribute description (Group: Group GroupCn: Group name, which is displayed on the interface) 2. Add configuration itemsEach static attribute in the configuration item group is called Configuration item
/// <Summary> /// whether to record execution SQL // </summary> [Config (Name = "record execution SQL", DefaultValue = false)] public static bool IfLogSQL {get; set ;}
Note: In the ConfigAttribute description, Name: ultvalue: Default Value: ConfigValueType: the bool type is displayed as single-choice radio. For more settings, see the ConfigAttribute class.    3. Optional steps for personalized business
/// <Summary> /// check before saving /// </summary> /// <param name = "value"> </param> /// <returns> bool </returns> public override bool BeforeSave (OptionViewModel value) {foreach (var item in value. listOptions) {switch (item. key) {case "IfLogSQL": if (string. isNullOrEmpty (item. value) {return false;} break; default: break;} return base. beforeSave (value );}
Public override void AfterSave (List <Options> ListOptions) {foreach (Options item in ListOptions) {switch (item. key) {case "IfLogSQL": // enable or disable EF logging bool curValue = Convert. toBoolean (item. value); if (curValue! = TestConfig. IfLogSQL) {// EfLogConfig. ManagerEFLog (curValue);} break; default: break ;}}}

 

Use the BeforeSave and AfterSave methods to personalize your business.

4. Parameter usage

        public ActionResult Index()        {            ViewBag.Title = SystemConfig.SystemTitle;            return View();        }    

Because static attributes are defined, you can directly use

  Summary:You only need to add a configuration item class, and you do not need to modify anything else. The required storage logic and interface are complete. Here is a question: How do I know the frontend interface should be used for rendering? Radio, text, passwordWhich of the following controls? Back to the top implementation

General Configuration Management meets the following objectives:

1. Easy to use

Add a configuration item class to complete the work without additional operations

2. Powerful functions to facilitate expansion

Other work such as the interface has been completed by the Framework. for personalized configuration such as verification or additional work, you can expand through beforesave and aftersave.

3. Beautiful Interface

Based on bootstrap, the overall effect is quite good.

System class diagram

System Main Process

Go back to top key code parsing

1. Initialization (Global. asax. cs)

// 1. MEF initializes MefConfig. init (); // 2. EF initializes EFInitializer. unSafeInit (); // 3. initialize the system parameter configuration ConfigManager configManager = MefConfig. tryResolve <ConfigManager> (); configManager. init ();
MefConfig. Init () method initialization combination container EFInitializer. UnSafeInit () Entity Framework database connection and type initialization configManager. Init () read all configuration items read all configuration item values from the database assign values 2. ConfigManager
/// <Summary> /// all system configuration information /// </summary> [ImportMany many (typeof (ConfigOption)] public IEnumerable <ConfigOption> AllConfig {get {return _ allConfig;} set {if (_ allConfig = null) {_ allConfig = value ;}}}
Read all configuration item groups through the MEF importer and store them in static variable _ allConfig
/// <Summary> /// initialize system parameter configuration information /// </summary> public void Init () {// List of all option values <Options> listOption = ConfigService. getAllOptions (); ConfigDescription desc = null; // code the existing configuration item foreach (ConfigOption item in AllConfig) {// The reflection reads the ConfigTypeAttribute ConfigAttribute information desc = ConfigDescriptionCache. getTypeDiscription (item. getType (); // set the GroupType desc of the current configuration item. groupTypePropertyInfo. setValue (item, Convert. changeType (desc. group, desc. groupTypePropertyInfo. propertyType), null); // List of each value <Options> itemOptions = listOption. where (e => e. optionType. equals (desc. group, StringComparison. ordinalIgnoreCase )). toList (); Options op = null; ConfigAttribute ca = null; foreach (PropertyInfo prop in desc. staticPropertyInfo) {op = itemOptions. firstOrDefault (e1 => e1.Key. equals (prop. name, StringComparison. ordinalIgnoreCase); ca = desc. memberDict [prop. name]; if (op = null) {// sets the default value prop. setValue (null, Convert. changeType (ca. defaultValue, prop. propertyType), null);} else {prop. setValue (null, Convert. changeType (op. value, prop. propertyType), null );}}}}

ConfigService. GetAllOptions () reads all option values from the database, and reads the values of all static attributes through ConfigDescriptionCache. GetTypeDiscription (item. GetType () Reflection

Ing between property types and foreground controls

/// <Summary> /// set the default value type of the item-convert it based on the attribute type /// </summary> /// <param name = "configAttr"> </ param> // <param name = "propertyType"> attribute type </param> private static void SetConfigValueType (ConfigAttribute configAttr, type propertyType) {switch (propertyType. toString () {case "System. string ": configAttr. valueType = ConfigValueType. string; // text box break; case "System. boolean ": configAttr. valueType = ConfigValueType. bool; // corresponding to the foreground radio break; case "System. int32 ": case" System. double ": configAttr. valueType = ConfigValueType. number; // corresponding value input box break; default: configAttr. valueType = ConfigValueType. string; break ;}}

You can specify the password type.

/// <Summary> // MQ connection password /// </summary> [Config (Name = "password", ValueType = ConfigValueType. password)] public static string Password {get; set ;}

 Provided interfaces for obtaining configuration items

/// <Summary> /// obtain all configuration information /// </summary> /// <returns> all configuration information </returns> public List <OptionViewModel> GetAllOption (string GroupType = "")
/// <Summary> /// obtain the configuration information of the specified item /// </summary> /// <param name = "GroupType"> group item </param> // /<returns> all configuration information </returns> public OptionViewModel GetOptionByGroup (string GroupType)
/// <Summary> /// obtain the configuration information of the specified item /// </summary> /// <param name = "GroupType"> group item </param> // /<returns> all configuration information </returns> public Options GetOptionByGroupAndKey (string GroupType, string key ){}

Save method implementation

/// <Summary> // Save the configuration information /// </summary> /// <param name = "value"> Configuration Information </param> public ApiResult <string> Save (OptionViewModel value) {ApiResult <string> result = new ApiResult <string> (); result. hasError = true; string GroupType = value. group. groupType; if (value. group = null | string. isNullOrEmpty (GroupType) | value. listOptions = null) {result. message = "An exception occurred when saving the parameter configuration"; return result ;}// call the ConfigOption curConfigOption = AllConfig before saving the pre-processing event. firstOrDefault (e => e. groupType. equals (GroupType, StringComparison. ordinalIgnoreCase); if (curConfigOption = null) {// if no matching result is found. message = string. format ("currently saved configuration information {0} does not correspond to the background task configuration class", GroupType); return result ;}
// Call the pre-save verification event of the configuration itemIf (! CurConfigOption. beforeSave (value) {result. message = "the current configuration item cannot be saved"; return result;} // Save the data try {using (CommonDbContext cdb = new CommonDbContext () {var dbSet = cdb. set <Options> (); var delObjs = dbSet. where (e => e. optionType = GroupType ). toList (); // Delete the original data foreach (var item in delObjs) {cdb. entry (item ). state = EntityState. deleted;} // Save the data foreach (var item in value. listOptions) {item. optionId = Guid. newGuid (). toString ("N");} dbSet. addRange (value. listOptions); cdb. saveChanges () ;}} catch (Exception ex) {result. message = ex. message; return result;} // assign SetValue (curConfigOption, value. listOptions); result. hasError = false; return result ;} /// <summary> /// assign a value to the current configuration item while saving /// </summary> /// <param name = "item"> current configuration item </param> /// <param name = "ListOptions"> configuration item value </param> public void SetValue (ConfigOption item, list <Options> ListOptions ){// Call the Save post-processing eventItem. afterSave (ListOptions); var desc = ConfigDescriptionCache. getTypeDiscription (item. getType (); Options option = null; foreach (PropertyInfo prop in desc. staticPropertyInfo) {option = ListOptions. first (e => e. key. equals (prop. name, StringComparison. ordinalIgnoreCase); if (option = null) {// This configuration item does not exist, the current value prop is cleared. setValue (null, Convert. changeType (null, prop. propertyType), null);} else {prop. setValue (null, Convert. changeType (option. value, prop. propertyType), null );}}}
  Foreground rendering logic (config. js)
// Initialize data initData = function () {$. ajax ({type: "get", url: "/Config/GetAllOption", // call the dataType: "json", beforeSend: function () interface for obtaining all configuration items in the background () {// load the wait layer index = layer. load (0) ;}, complete: function () {layer. close (index) ;}, success: function (data) {BaseData = data; drawConfig (BaseData); // draw the interface }});}

 

Back to TopSummary

This parameter configuration can be easily transplanted to your system. The parameter configuration function used in TaskManagerV2.0 is the code of this system. In addition, remember to modify the database connection string in Web. config. This article will be completed here. I will introduce the content irrelevant to this article. The Reward function I added to my blog is located at the end of the announcement and article, respectively,Thank you for your support for continuous writing!Next, we will introduce the use of the Message Queue RabbitMQ in combination with parameter configuration!

Source code: Release.

 

If you believe that reading this blog has some benefits, click 【Recommendation] Button.
If you want to discover my new blog more easily, click 【Follow me].

If you want to give me more encouragement, please

Because my enthusiasm for writing is inseparable from your support.

Thank you for reading this article. If you are interested in the content of my blog, continue to follow up on my blog.

Related Article

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.