zoukankan      html  css  js  c++  java
  • 如何使用命令模式去调用web服务

    //------------------------------------------------------------程序如何实现一个命令模型------------------------------------------------

    开始的准备:

    [code language="c#"]
          private System.ComponentModel.BackgroundWorker worker;

    [/code]

    1.第一步。也是非常常见的 实现btnOK_Click的功能(也就是点击登陆的功能)
                    private void btnOK_Click(object sender, EventArgs e)
                    {
                            this.Login();
                    }
    2.第二步。实现Login()函数
                     private void Login()
                    {
                            string[] credential = new string[] { txtUserName.Text, txtPassword.Text };

                            lblProgress.Visible = true;
                            lblProgress.Refresh();

                            loginProgress.Visible = true;
                            loginProgress.Value = loginProgress.Minimum;

                            worker.RunWorkerAsync(credential);   //这里采用BackGroundWork,去了解这个控件,到这一步的时候,它会去执行DoWork这个操作
                    }
    3。第三步:  实现DoWork函数
                    private void worker_DoWork(object sender, DoWorkEventArgs e)
                    {
                            // This method will run on a thread other than the UI thread.
                            // Be sure not to manipulate any Windows Forms controls created
                            // on the UI thread from this method.

                            string[] credential = (string[])e.Argument;
                            string userName = credential[0];
                            string password = credential[1];

                            this.Authenticate(userName, password); //这里我们调用验证函数
                 }
    4.第四步:  //在改使用类下新建函数来进行使用该验证
                    private void Authenticate(string userName, string password)
                    {
                            AuthenticateCommand command = new AuthenticateCommand(userName, password);  //注意这里,这里我们已经去调用命令模式里面的东西了
                            ((ICommand)command).Execute(); //执行操作

                            worker.ReportProgress(1, "Loading...");
                    }
    5.第 五步: //这是 命令模式里面的东西。。。。
                    public class AuthenticateCommand : ICommand       
                            {
                    public AuthenticateCommand( string userName, string password )
                                    {
                                            this._UserName = userName;
                                            this._Password = password;
                                    }

                                    void ICommand.Execute()                            //this is user for Execute The Application
                                    {
                                            UserInfo user = _Application.Settings.UserInfo;   //这个是在ObjectModel里面建立的模型
                                            user.UserName = this._UserName;
                                            string passwordHash = HashStringMD5(this._Password);
                                            user.UserPassword = passwordHash;

                                            using (SecurityService service = ServiceProxyFactory.GetSecurityService())
                                            {
                                    user.SecurityKey = service.AuthenticateUser();    //这里才是关键,归根到底是调用WebService的
                   
                                    if (null != user.SecurityKey && user.SecurityKey.Length > 0)
                                    {
                                        this._IsSuccess = true;
                                    }


                                            }
                                    }
                           }
    //------------------------------------------------------------------------------------------the eth-----------------------------------------------

  • 相关阅读:
    OO ALV-单击事件,双击事件,添加自定义按钮事件(EVENT)实例
    SWM0-Excel模板上载以及模板下载程序
    linux ubuntu安装Jupyter Notebook打开运行. ipynb文件
    主机ping不通ubuntu虚拟机的解决方法
    解决word中使用mathtype编辑的公式和正文不在一行的问题
    fis3实现前端网页实时刷新(自动刷新)
    解决代理服务器导致不能上网问题
    python常用函数总结
    使用python写机器学习算法遇到的问题
    windows---Apache环境搭建
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/1442207.html
Copyright © 2011-2022 走看看