Implementation of the parameter configuration management function of the Winform development framework-Based on SettingsProvider.net,
In an earlier period, I wrote an article titled combining Control. firefoxDialog control, constructing excellent parameter configuration management module, describes the parameter configuration module functions integrated on the basis of my Winform framework, but the Configuration Management of the parameter module is not flexible enough, therefore, I have been searching for a better alternative to display it in combination with the FireFoxDialog interface. During this period, I carefully studied several articles on Configuration Management on the Codeproject website, but I always felt that it was not flexible or convenient. This article focuses on the combination of FireFoxDialog parameter configuration interface components and SettingsProvider.net technology to achieve more beautiful and flexible Winform program parameter configuration management.
There are two or three articles in CodeProject that introduce the configuration file very well. Below is a link for everyone to share:
Http://www.codeproject.com/Articles/25829/User-Settings-Applied
Http://www.codeproject.com/Articles/475498/Easier-NET-settings
Both of them are very good, but they do not feel very satisfied with my simple and efficient needs. By accident, I found a "SettingsProvider.net" on GitHub, which is very good, it also feels that the scalability is well done, so it is integrated with the FireFoxDialog interface to implement the parameter management function within the framework.
1. SettingsProvider.net function Introduction
This is a parameter configuration component on GitHub that can be used to store configuration files based on common configuration files, ProgramData directory files, and independent storage area files, it is mainly saved Based on the configuration in Json format, so we can store it in the database. Although there is no official example, we can easily implement this function through expansion, next I will introduce how to save the extension class for its database parameters.
It can also manage the configuration file through the Atrribute identifier, and set the parameter to encryption, default value, and name change. The following is an example of its configuration parameter class.
public class MySettings{ [DefaultValue("Jake")] [DisplayName("Your Name")] public string Name { get; set; } [DefaultValue(true)] [Description("Should Some App Remember your name?")] public bool RememberMe { get;set; } public List<Guid> Favourites { get;set; } [Key("OriginalName")] public string Renamed { get; set; } [ProtectedString] public string Encrypted { get; set; }}
The read operation is as follows:
var settingsProvider = new SettingsProvider(); //By default uses IsolatedStorage for storagevar mySettings = settingsProvider.GetSettings<MySettings>();Assert.True(mySettings.RememberMe);
The Save operation is as follows:
var settingsProvider = new SettingsProvider(); //By default uses IsolatedStorage for storagevar mySettings = new MySettings { Name = "Mr Ginnivan" };settingsProvider.Save(mySettings);
The operation code for obtaining parameter metadata is as follows:
var settingsProvider = new SettingsProvider();foreach (var setting in settingsProvider.ReadSettingMetadata<MySettings>()){ Console.WriteLine("{0} ({1}) - {2}", setting.DisplayName, setting.Description, setting.DefaultValue);}
2. parameter configuration interface
Based on the preceding SettingsProvider.net component, we can use the FireFoxDialog interface I introduced earlier to achieve better parameter configuration management functions, as shown in the following interface.
We can put different parameters in different storage media. For example, some common parameters can be configured in the local directory file and some personal information-related content, we can put it in the database, so that you do not need to reconfigure it on each client, which is very convenient.
In the above example, I will introduce two objects specifically, one is based on local file parameter storage, and the other is based on the Parameter Storage of database files.
3. Implementation of SettingsProvider.net-Based Integrated Operations
This article introduces the SettingsProvider.net-based functions and the final integration effect. Let's take a look at how it is integrated to save data of different file types.
First, we drag the corresponding interface in the design mode. Because the FireFoxDialog interface was originally used for parameter settings, some of the controls in them will not be described one by one, specific can refer to the corresponding article (http://www.codeproject.com/KB/miscctrl/ControlFirefoxDialog.aspx? Msg = 1856449 ).
Let's take a look at the design interface and background code of the above effect interface.
The design interface is as follows. Drag a parameter to configure the user control to the form and set the corresponding content.
The background code is as follows.
Public partial class frmsetent: BaseForm {public FrmSettings () {InitializeComponent ();} private void FrmSettings_Load (object sender, EventArgs e) {this. firefoxDialog1.ImageList = this. imageList1; this. firefoxDialog1.AddPage ("Report Settings", new PageReport (); // stores this based on local file parameters. firefoxDialog1.AddPage ("Mailbox Settings", new PageEmail (); // database-Based Parameter Storage // The following is the companion this. firefoxDialog1.AddPage ("SMS Settings", new PageEmail (); this. firefoxDialog1.AddPage ("sound settings", new PageEmail (); this. firefoxDialog1.AddPage ("system settings", new PageEmail (); this. firefoxDialog1.AddPage ("Backup Settings", new PageEmail (); this. firefoxDialog1.AddPage ("other settings", new PageEmail (); this. firefoxDialog1.Init ();}}
Here, the most representative components are PageReport and PageEmail.
1) report module configuration management
Based on the report parameters and mail parameters, we can define a parameter object class to facilitate the storage and acquisition of data carrying objects.
The report parameter object class is as follows (only a simple report path is processed ):
/// <Summary> /// Report Settings /// </summary> public class ReportParameter {// <summary> /// report file of the dispatch ticket /// </summary> [DefaultValue ("WHC. carDispatch. carSendBill2.rdlc ")] public string CarSendReportFile {get; set ;}}
The corresponding design interface is shown below, which is used to provide parameter configuration for a report file.
Let's take a look at the background components and how the parameters are saved and displayed.
First, we need to initialize the corresponding object. Here we use a file in the program running directory to save these settings.
Public partial class PageReport: PropertyPage {private SettingsProvider settings; private ISettingsStorage store; public PageReport () {InitializeComponent (); if (! This. DesignMode) {// PortableStorage: Create a setting file record parameter data store = new PortableStorage (); settings = new SettingsProvider (store );}}
Note: PortableStorage creates a setting file in the running program directory to record parameter data. After running, a file similar to "ReportParameter. settings file, its content format is as follows.
[{"Key":"CarSendReportFile","Value":"\"WHC.CarDispatch.CarSendBill2.rdlc\""}]
Let's take a look at how the parameters of the report component are initialized:
public override void OnInit() { ReportParameter parameter = settings.GetSettings<ReportParameter>(); if (parameter != null) { EnableOtherReport(false); string reportFile = parameter.CarSendReportFile; if (reportFile == "WHC.CarDispatch.CarSendBill2.rdlc") { this.radReport.SelectedIndex = 0; } else if (reportFile == "WHC.CarDispatch.CarSendBill.rdlc") { this.radReport.SelectedIndex = 1; } else { EnableOtherReport(true); this.radReport.SelectedIndex = 2; this.txtOtherReport.Text = reportFile; } } }
Through Parameter Storage object processing, we can obtain data through ReportParameter.
When saving a parameter, you also get a parameter object and set its value, and then save it. The specific code is as follows.
Public override bool OnApply () {bool result = false; try {ReportParameter parameter = settings. GetSettings <ReportParameter> (); if (parameter! = Null) {int otherType = 2; // 2 indicates other types if (this. radReport. selectedIndex <otherType) {parameter. carSendReportFile = this. radReport. properties. items [this. radReport. selectedIndex]. value. toString ();} else {parameter. carSendReportFile = this.txt OtherReport. text;} settings. saveSettings <ReportParameter> (parameter);} result = true;} catch (Exception ex) {LogTextHelper. error (ex); MessageDxUtil. showError (ex. message);} return result ;}
2) Mail parameter configuration management
The email parameter object class is as follows:
/// <Summary> /// mailbox settings /// </summary> public class EmailParameter {// <summary> /// email account /// </summary> // [DefaultValue ("wuhuacong@163.com")] public string Email {get; set ;}/// <summary> // POP3 server /// </summary> [DefaultValue ("pop.163.com")] public string Pop3Server {get; set ;}/// <summary> // POP3 port /// </summary> [DefaultValue (110)] public int Pop3Port {get; set ;}//< summary> /// SMTP server /// </summary> [DefaultValue ("smtp.163.com")] public string SmtpServer {get; set ;} /// <summary> /// SMTP port /// </summary> [DefaultValue (25)] public int SmtpPort {get; set ;} /// <summary> /// Logon account /// </summary> public string LoginId {get; set ;} /// <summary> /// logon password /// </summary>[ProtectedString]Public string Password {get; set ;}/// <summary> /// use SSL encryption /// </summary> [DefaultValue (false)] public bool UseSSL {get; set ;}}
The following figure shows the interface for displaying and saving parameters.
The interface Initialization is similar to the above, but the database storage class DatabaseStorage is used here, the content will be saved in the database, and we will perform targeted processing of the path through the user ID Create, different configuration files can be created for each user.
Public partial class PageEmail: PropertyPage {private SettingsProvider settings; private ISettingsStorage store; public PageEmail () {InitializeComponent (); if (! This. designMode) {// DatabaseStorage: in the database, save the parameter data string creator = Portal with the specified user ID. gc. loginUserInfo. name; store = new DatabaseStorage (creator); settings = new SettingsProvider (store );}}
The parameter Loading Code is as follows, that is, after the data is obtained, it is displayed on the interface.
public override void OnInit() { EmailParameter parameter = settings.GetSettings<EmailParameter>(); if (parameter != null) { this.txtEmail.Text = parameter.Email; this.txtLoginId.Text = parameter.LoginId; this.txtPassword.Text = parameter.Password; this.txtPassword.Tag = parameter.Password; this.txtPop3Port.Value = parameter.Pop3Port; this.txtPop3Server.Text = parameter.Pop3Server; this.txtSmtpPort.Value = parameter.SmtpPort; this.txtSmtpServer.Text = parameter.SmtpServer; this.txtUseSSL.Checked = parameter.UseSSL; } }
The data storage operation is also very simple, similar to the previous operation, as shown below.
EmailParameter parameter = settings.GetSettings<EmailParameter>(); if (parameter != null) { parameter.Email = this.txtEmail.Text; parameter.LoginId = this.txtLoginId.Text; parameter.Password = this.txtPassword.Text; parameter.Pop3Port = Convert.ToInt32(this.txtPop3Port.Value); parameter.Pop3Server = this.txtPop3Server.Text; parameter.SmtpPort = Convert.ToInt32(this.txtSmtpPort.Value); parameter.SmtpServer = this.txtSmtpServer.Text; parameter.UseSSL = this.txtUseSSL.Checked; settings.SaveSettings<EmailParameter>(parameter); }
Finally, we can see in the database table that the corresponding records have been saved and the user password is encrypted.
In this way, we can integrate the features of the two to achieve a better parameter configuration interface display and background storage processing, targeted, use different storage media to meet different needs.