Configuration implementation of various login methods in the hybrid development framework mode, framework mode

Source: Internet
Author: User

Configuration implementation of various login methods in the hybrid development framework mode, framework mode

During the login process of many of our programs, some configuration management dialog boxes may be available for some complicated content. What is the hybrid development framework introduced in this article, here is a definition.

Hybrid development framework is a combination of the features of the traditional Winform development framework and the WCF development framework. You can switch between them freely, unifies the call mode of business services at the system interface layer. All component modules implement two call methods, which is a very flexible framework application, it can be used for stand-alone software, lan-based application software, or distributed Internet environment applications.

For this hybrid mode, there are two methods mentioned above: one is the conventional way to access the database, the other is the way to access the WCF Service, and the other is to access the WCF Service, there may be LAN (Intranet) or Internet (Internet) modes, so we may configure three login modes. If you need to manually modify the configuration file each time, it may be troublesome, if we can implement interface configuration of parameters through a program, it will be much easier to work, and it will not be prone to errors.

1. configuration interface and configuration content

For configuration management, I certainly recommend the SettingsProvider.net component first. You need to know how to use it, you can refer to the parameter configuration management function implementation of Winform development framework-build based on SettingsProvider.net. Here you can use the FireFoxDialog interface to achieve user-friendly configuration management, as shown in the following figure.

In the WCF configuration file, we can see a lot of IP address configurations. In general, we put the WCF configuration information in an independent configuration file for convenient management.

The following figure shows the configuration information of the WCF client on the LAN.

For the internet, you only need to replace these IP addresses with public IP addresses. Generally, these are independent configuration files and we usually have a master configuration file **. exe. config is used to configure some common configuration information of our framework.

As shown in the following figure, you can save the main database connection information in the main configuration file when you need to access the local database in hybrid mode.

 

2. Design and Implementation of the configuration dialog box

We have introduced some information about parameter configuration and the WCF configuration file. Based on these considerations, we should design the relevant interface in the login interface of the hybrid mode, this allows users to easily configure and access different data sources, so as to achieve truly powerful hybrid functions and convenience.

First, let's introduce my hybrid framework and login interface design.

Click the parameter Settings button to enter the logon access parameters interface dialog box, in which you can configure relevant WCF access parameters in a friendly and intuitive manner, as shown in the following interface, the interface components similar to FireFox and background file storage are used mainly based on local XML storage, and the SettingsProvider.net component is used for data storage and management.

If you select"Standalone Edition", Then we set the correspondingNetwork ModeThis setting is not optional, so it is better to reflect the exclusive local mode.

These interfaces basically show you how to handle parameter configuration content of the hybrid development framework. What are the specific implementation operations? Next we will analyze and introduce it step by step.

First, we define an object class for saving and loading parameters to store the corresponding parameter information.

/// <Summary> /// connection method and access method of user logon /// </summary> public class LoginParameter {// <summary> // The Last Logon OF THE SYSTEM account /// </summary> [DefaultValue ("admin")] public string LoginId {get; set ;}/// <summary> /// logon Password /// </summary> [ProtectedString] public string Password {get; set ;} /// <summary> // remember the password /// </summary> [DefaultValue (false)] public bool RememberPassword {get; set ;} /// <summary> /// whether the connection mode of the local database is used. Otherwise, the WCF Service Mode (intranet and Internet) is used. /// </summary> [DefaultValue (false)] public bool IsLocalDatabase {get; set ;}/// <summary> /// Intranet WCF host address /// </summary> [DefaultValue ("192.168.1.10")] public string InternalWcfHost {get; set ;}/// <summary> /// Intranet WCF port /// </summary> [DefaultValue (8000)] public int InternalWcfPort {get; set ;}/// <summary> // host address of the Internet WCF /// </summary> [DefaultValue ("183.6.161.193")] public string ExternalWcfHost {get; set ;}/// <summary> // port of the external network WCF /// </summary> [DefaultValue (8000)] public int ExternalWcfPort {get; set ;}}

In this way, we can use the entity class as the carrier to obtain and save data.

The code for loading and saving configuration information is as follows. These save operations are based on SettingsProvider.net.

/// <Summary> /// load the parameter information from the local XML file /// </summary> private void LoadParameter () {store = new PortableStorage (); // save it on the local computer settings = new SettingsProvider (store); parameter = settings. getSettings <LoginParameter> (); if (parameter! = Null) {this. cmbzhanhao. text = parameter. loginId; this. chkRemember. checked = parameter. rememberPassword; if (parameter. rememberPassword) {this. tbPass. text = parameter. password;} else {this. tbPass. text = "";} this. chkLocalVersion. checked = parameter. isLocalDatabase; // ensure that the access method is correct. The network version is still a standalone version of SetAccessType (this. chkLocalVersion. checked) ;}/// <summary> /// Save the user information to the local XML file /// </summary> privat E void SaveParameter () {store = new PortableStorage (); // save it on the local computer settings = new SettingsProvider (store); parameter = settings. getSettings <LoginParameter> (); if (parameter! = Null) {parameter. loginId = this. cmbzhanhao. text; if (parameter. rememberPassword) {parameter. password = this. tbPass. text;} else {parameter. password = "";} parameter. isLocalDatabase = this. chkLocalVersion. checked; settings. saveSettings <LoginParameter> (parameter );}}

If you select the standalone mode, you need to set the corresponding parameters to tell the hybrid framework to adopt the local database mode. The specific code is as follows.

/// <Summary> // check box event processing in standalone mode // </summary> private void chkLocalVersion_CheckedChanged (object sender, EventArgs e) {SetAccessType (this. chkLocalVersion. checked );} /// <summary> /// set it to the network mode or standalone mode. /// </summary> /// <param name = "localType"> whether it is in standalone mode </ param> private void SetAccessType (bool localType) {this. lblNetType. enabled =! LocalType; this. radNetType. Enabled =! LocalType; AppConfig config = new AppConfig (); config. AppConfigSet ("CallerType", localType? "Win": "wcf"); ConfigurationManager. RefreshSection ("etettings ");}

For the network mode, you also need to distinguish the Intranet mode from the Internet mode. modify the configuration file according to the parameters. The specific logic code is as follows.

/// <Summary> /// handle event switching in the network mode (intranet and Internet) /// </summary> private void radNetType_SelectedIndexChanged (object sender, EventArgs e) {ChangeConfig () ;}/// <summary> /// modify the configuration file /// </summary> private void ChangeConfig () {if (parameter! = Null) {bool isIntranet = radNetType. editValue. toString () = "intranet"; if (isIntranet) {UpdateConfig (parameter. internalWcfHost, parameter. internalWcfPort);} else {UpdateConfig (parameter. externalWcfHost, parameter. externalWcfPort);} else {MessageDxUtil. showError ("failed to get parameter information"); }}/// <summary> // In WCF mode, modify the host address and port in the configuration file /// </summary> /// <param name = "serverIPAddress"> host address information </param> /// <param Name = "serverPort"> port </param> private void UpdateConfig (string serverIPAddress, int serverPort) {string basePath = System. environment. currentDirectory; UpdateConfigFile (serverIPAddress, serverPort, Path. combine (basePath, "BaseWcfConfig. config "); UpdateConfigFile (serverIPAddress, serverPort, Path. combine (basePath, "WcfConfig. config ") ;}/// <summary> /// Replace the host information and port parameters with regular identifiers. /// </summary> /// <Param name = "serverIPAddress"> host address information </param> /// <param name = "serverPort"> port </param> /// <param name = "exeFilePath "> Configuration File address </param> private void UpdateConfigFile (string serverIPAddress, int serverPort, string exeFilePath) {string address = File. readAllText (exeFilePath, System. text. encoding. UTF8); string pattern = "://. *? /"; String replacement = string. format (": // {0 }:{ 1}/", serverIPAddress, serverPort); address = Regex. replace (address, pattern, replacement); File. writeAllText (exeFilePath, address, System. text. encoding. UTF8 );}

The above is an example of how to manage the configuration information of login methods and login parameters of the hybrid development framework. I hope it will help you.

 

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.