码迷,mamicode.com
首页 > 其他好文 > 详细

UserLogin

时间:2014-07-01 00:43:02      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

DAL:

IUserDAL

bubuko.com,布布扣
namespace Dal
{
    /// <summary>
    /// This interface is defined for user functions.
    /// </summary>
    public interface IUserDal
    {
        #region Returns Model

        /// <summary>
        /// Gets user by username.
        /// </summary>
        /// <param name="username">The user‘s name.</param>
        /// <returns>Returns user model.</returns>
        User RetrieveUserByUserName(string userName);

        #endregion

        #region Public int

        /// <summary>
        /// Updates the password of the user.
        /// </summary>
        /// <param name="newPassword">The new password.</param>
        /// <param name="userName">The user‘s name.</param>
        int UpdatePassword(string newPassword, string userName);

        #endregion
    }
}
View Code

UserDAL

bubuko.com,布布扣
namespace Dal
{
    /// <summary>
    /// This class is used for defining user functions.
    /// </summary>
    public class UserDal : IUserDal
    {
        #region Returns Model

        /// <summary>
        /// Gets user by username.
        /// </summary>
        /// <param name="userName">The user‘s name</param>
        /// <returns>Return user model.</returns>
        public User RetrieveUserByUserName(string userName)
        {

            User user = null;

            string sqlText = SqlText.GetUserByUserName;
            SqlParameter[] prams = new SqlParameter[] 
            { 
                new SqlParameter("@username",userName),
            };
            try
            {
                SqlDataReader sqlDataReader = SqlHelper.ExecureReader(sqlText, prams);
                if (sqlDataReader.Read())
                {
                    user = new User();
                    user.UserName = userName;
                    user.Password = Convert.ToString(sqlDataReader["password"]);
                    user.RoleType = Convert.ToString(sqlDataReader["role_type"]);
                    user.Telephone = Convert.ToString(sqlDataReader["telephone"]);
                    user.Gender = sqlDataReader["gender"].ToString();
                    user.Email = sqlDataReader["email"].ToString();
                    user.Address = sqlDataReader["address"].ToString();
                    user.ChineseName = (sqlDataReader["chinese_name"] is DBNull) ? string.Empty : sqlDataReader["chinese_name"].ToString();
                    user.Language = (sqlDataReader["language"] is DBNull) ? string.Empty : sqlDataReader["language"].ToString();
                }

                SqlHelper.CloseSqlDataReader(sqlDataReader);
            }
            catch(SqlException ex)
            {
                throw new UserException(UserException.RetrieveUserByUserName,ex);
            }

            return user;
        }

        #endregion

        #region Public int

        /// <summary>
        /// Updates the password of the user.
        /// </summary>
        /// <param name="newPassword">The new password.</param>
        /// <param name="userName">The user‘s name.</param>
        public int UpdatePassword(string newPassword, string userName)
        {
            int influenceNumber = 0;

            try
            {
                string sqlText = SqlText.UpdatePassword;
                SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter("@password", newPassword),
                    new SqlParameter("@userName", userName),
                };

                influenceNumber = SqlHelper.ExecuteNonQuery(sqlText, parms);
            }
            catch (SqlException ex)
            {
                throw new UserException(UserException.UpdatePasswordFailed, ex);
            }

            return influenceNumber;
        }

        #endregion
    }
}
View Code

BLL

IUserBLL

bubuko.com,布布扣
namespace Bll
{
    /// <summary>
    /// This interface is used to define user‘s functions.
    /// </summary>
    [ServiceContract(Namespace="MyCompanyService")]
    public interface IUserBll
    {
        #region Returns Model

        /// <summary>
        /// Gets the user model.
        /// </summary>
        /// <param name="userName">The user‘s name.</param>
        /// <returns>Returns user‘s model.</returns>
        [OperationContract]
        User RetrieveUserByUserName(string userName);

        #endregion

        #region Returns bool

        /// <summary>
        /// Updates user‘s password.
        /// </summary>
        /// <param name="newPassword">The new password.</param>
        /// <param name="userName">The user‘s name.</param>
        [OperationContract]
        bool UpdatePassword(string newPassword, string userName);

        #endregion
    }
}
View Code

UserBLL

bubuko.com,布布扣
namespace Bll
{
    /// <summary>
    /// This class is used for realizing user‘s functions.
    /// </summary>
    public class UserBll:IUserBll
    {
        #region Field

        IUserDal userDal = new UserDal();
        log4net.ILog log = log4net.LogManager.GetLogger(EqualsConst.GetServiceLoggerName);

        #endregion

        public UserBll() { }

        public UserBll(IUserDal userDal)
        {
            this.userDal = userDal;
        }

        #region Returns Model

        /// <summary>
        /// Gets the user by user‘s name.
        /// </summary>
        /// <param name="userName">The user‘s name.</param>
        /// <returns>Returns user model.</returns>
        public User RetrieveUserByUserName(string userName)
        {
            User user = null;

            try
            {
                user = userDal.RetrieveUserByUserName(userName);
            }
            catch (UserException ex)
            {
                log.Error(ExamException.RetrieveExamList, ex);
                throw new FaultException<MyExceptionContainer>(new MyExceptionContainer()
                {
                    ErrorMessage = ex.Message,
                    Description = ExamException.RetrieveExamList
                });
            }

            return user;
        }

        #endregion

        #region Public bool

        /// <summary>
        /// Updates user‘s password.
        /// </summary>
        /// <param name="newPassword">The new password.</param>
        /// <param name="userName">The user‘s name.</param>
        public bool UpdatePassword(string newPassword, string userName)
        {
            bool isUpdatePassworded = false;

            try
            {
                int i = userDal.UpdatePassword(newPassword, userName);

                if (i > 0)
                {
                    isUpdatePassworded = true;
                }
            }
            catch (UserException ex)
            {
                log.Error(UserException.UpdatePasswordFailed, ex);
                throw new FaultException<MyExceptionContainer>(new MyExceptionContainer()
                {
                    ErrorMessage = ex.Message,
                    Description = UserException.UpdatePasswordFailed
                });
            }

            return isUpdatePassworded;
        }

        #endregion
    }
}
View Code

Client

bubuko.com,布布扣
namespace OES
{

    /// <summary>
    /// This class is used for login.
    /// </summary>
    public partial class FormLogin : Form
    {
        #region Field

        private log4net.ILog log = log4net.LogManager.GetLogger(EqualsConst.GetLoggerName);
        public User user = null;

        #endregion

        #region Constructor

        public FormLogin()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Method

        /// <summary>
        /// The processing of btnSubmit.
        /// </summary>
        /// <param name="sender">The source object of event.</param>
        /// <param name="e">The parameter of event.</param>
        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            UserBllService.UserBllClient userBll = new UserBllService.UserBllClient();

            if (String.IsNullOrWhiteSpace(this.txtUsername.Text))
            {
                this.lblLoginResult.Text = Constant.UserException.UserNameIsNull;
            }
            else if (String.IsNullOrWhiteSpace(this.txtPassword.Text))
            {
                this.lblLoginResult.Text = Constant.UserException.PasswordIsNull;
            }
            else
            {
                string userName = CheckForParameter.ReplaceSqlChar(this.txtUsername.Text.Trim());
                string password = CheckForParameter.ReplaceSqlChar(this.txtPassword.Text.Trim());

                try
                {
                    BackgroundWorker bw = new BackgroundWorker();
                    bw.DoWork += new DoWorkEventHandler(
                        delegate
                        {
                            user = userBll.RetrieveUserByUserName(userName);
                        });

                    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                        delegate(object obj, RunWorkerCompletedEventArgs arg)
                        {
                            if (arg.Error == null)
                            {
                                if (null != user && user.Password.Equals(MD5Tool.MD5Tostring(password), StringComparison.InvariantCultureIgnoreCase))
                                {
                                    this.DialogResult = DialogResult.OK;
                                }
                                else
                                {
                                    this.lblLoginResult.Text = UserException.LoginedFailed;
                                }
                            }
                            else
                            {
                                log.Error(arg.Error);
                                MessageBox.Show(UIException.ConnectionWithWCFFail);
                            }
                        });

                    bw.RunWorkerAsync();
                }
                catch (FaultException<MyExceptionContainer> myException)
                {
                    log.Error(myException.Message, myException);
                }
                catch (FaultException faultException)
                {
                    log.Error(faultException.Message, faultException);
                }
                catch (Exception exception)
                {
                    log.Error(exception.Message, exception);
                }
            }
        }

        /// <summary>
        /// The processing of close the form.
        /// </summary>
        /// <param name="sender">The source object of event.</param>
        /// <param name="e">The parameter of event.</param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        #endregion
    }
}
View Code

 

UserLogin,布布扣,bubuko.com

UserLogin

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/chenyongblog/p/3817400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!