Click here to Skip to main content
15,891,828 members
Articles / Programming Languages / C#

Attribute-flavored Domain Driven Design

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
6 Apr 2008CPOL8 min read 39.2K   231   25  
A centralized business domain with a common base
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Codebasement.DomainDrivenDesign.Basement.Utilities;
using Codebasement.DomainDrivenDesign.Basement.Enums;

namespace Codebasement.DomainDrivenDesign.Basement.Utilities
{
    /// <summary>
    /// Summary description for PasswordDialog.
    /// </summary>
    internal class PasswordDialog : Form
    {
        private Button _btnCancel;
        private Button _btnOk;
        private bool _confirmationError;
        private string _keyname;
        private Label _lblConfirm;
        private Label _lblEnter;
        private Label _lblError;
        private Label _lblHelp;
        private Label _lblStrength;
        private string _password;
        private IWindowsFormsEditorService _service;
        private string _title;
        private TextBox _txtConfirmation;
        private TextBox _txtEnter;
        private PasswordUtility _utility;
        private bool _validationError;
        private PasswordProgressBar passwordProgressBar1;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private Container components = null;

        public PasswordDialog()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        /// <summary>
        /// Gets or sets the name of the key.
        /// </summary>
        /// <value>The name of the key.</value>
        public string Keyname
        {
            get { return _keyname; }
            set { _keyname = value; }
        }

        /// <summary>
        /// Gets or sets the windows forms editor service.
        /// </summary>
        /// <value>The windows forms editor service.</value>
        public IWindowsFormsEditorService WindowsFormsEditorService
        {
            get { return _service; }
            set { _service = value; }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (_validationError || _confirmationError)
            {
                return;
            }

            Password = _txtEnter.Text;
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters",
            MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]
        private void PasswordDialog_Load(object sender, EventArgs e)
        {
            Text = Title;
            _txtEnter.PasswordChar = RegexExpressions.PasswordChar;
            _txtConfirmation.PasswordChar = RegexExpressions.PasswordChar;

            if (!String.IsNullOrEmpty(Keyname))
            {
                _lblEnter.Text = "Enter " + Keyname + ":";
                _lblConfirm.Text = "Confirm " + Keyname + ":";
            }
            _lblError.Text = String.Empty;
            _lblHelp.Text = String.Empty;
            _btnOk.Enabled = false;

            _utility = new PasswordUtility();
        }

        private void txtEnter_TextChanged(object sender, EventArgs e)
        {
            ValidatePasswords();
        }

        private void txtConfirmation_TextChanged(object sender, EventArgs e)
        {
            ValidatePasswords();
        }

        private void ValidatePasswords()
        {
            PasswordValidity validity = _utility.ValidatePassword(_txtEnter.Text, PasswordPolicy.Relaxed);

            passwordProgressBar1.Value = validity.PasswordStrengthPercentage;
            passwordProgressBar1.StartColor = validity.PasswordStrengthColor;
            passwordProgressBar1.EndColor = passwordProgressBar1.StartColor;
            _lblStrength.Text = validity.PasswordStrengthIndicator.ToString();
            //label2.Text = validity.PasswordStrengthNumerical.ToString();

            if (!validity.IsValid)
            {
                _lblError.Text = validity.ErrorText;
                _lblError.ForeColor = Color.Red;
                _validationError = true;
            }
            else
            {
                _lblError.Text = "Password is compliant with security policy.";
                _lblError.ForeColor = Color.DarkGreen;
                _validationError = false;
            }

            if (!(_txtConfirmation.Text == _txtEnter.Text))
            {
                string keyName = !String.IsNullOrEmpty(Keyname) ? Keyname : "value";
                _lblHelp.Text = "Entered " + keyName + " and confirmation differ.";
                _lblHelp.ForeColor = Color.Red;
                _confirmationError = true;
            }
            else
            {
                _lblHelp.Text = "Confirmation OK.";
                _lblHelp.ForeColor = Color.DarkGreen;
                _confirmationError = false;
            }

            _btnOk.Enabled = (!_validationError && !_confirmationError);
        }

        private void PasswordDialog_Closed(object sender, EventArgs e)
        {
            _service.CloseDropDown();
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization",
            "CA1303:DoNotPassLiteralsAsLocalizedParameters",
            MessageId = "System.Windows.Forms.Control.set_Text(System.String)")]
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PasswordDialog));
            this._btnOk = new System.Windows.Forms.Button();
            this._btnCancel = new System.Windows.Forms.Button();
            this._txtEnter = new System.Windows.Forms.TextBox();
            this._txtConfirmation = new System.Windows.Forms.TextBox();
            this._lblEnter = new System.Windows.Forms.Label();
            this._lblConfirm = new System.Windows.Forms.Label();
            this._lblError = new System.Windows.Forms.Label();
            this._lblStrength = new System.Windows.Forms.Label();
            this._lblHelp = new System.Windows.Forms.Label();
            this.passwordProgressBar1 = new Codebasement.DomainDrivenDesign.Basement.Utilities.PasswordProgressBar();
            this.SuspendLayout();
            // 
            // _btnOk
            // 
            this._btnOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this._btnOk.Location = new System.Drawing.Point(234, 105);
            this._btnOk.Name = "_btnOk";
            this._btnOk.Size = new System.Drawing.Size(75, 23);
            this._btnOk.TabIndex = 6;
            this._btnOk.Text = "&Ok";
            this._btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // _btnCancel
            // 
            this._btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this._btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this._btnCancel.Location = new System.Drawing.Point(314, 105);
            this._btnCancel.Name = "_btnCancel";
            this._btnCancel.Size = new System.Drawing.Size(75, 23);
            this._btnCancel.TabIndex = 5;
            this._btnCancel.Text = "&Cancel";
            this._btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // _txtEnter
            // 
            this._txtEnter.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._txtEnter.Location = new System.Drawing.Point(118, 8);
            this._txtEnter.Name = "_txtEnter";
            this._txtEnter.Size = new System.Drawing.Size(191, 20);
            this._txtEnter.TabIndex = 2;
            this._txtEnter.TextChanged += new System.EventHandler(this.txtEnter_TextChanged);
            // 
            // _txtConfirmation
            // 
            this._txtConfirmation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._txtConfirmation.Location = new System.Drawing.Point(118, 32);
            this._txtConfirmation.Name = "_txtConfirmation";
            this._txtConfirmation.Size = new System.Drawing.Size(191, 20);
            this._txtConfirmation.TabIndex = 4;
            this._txtConfirmation.TextChanged += new System.EventHandler(this.txtConfirmation_TextChanged);
            // 
            // _lblEnter
            // 
            this._lblEnter.Location = new System.Drawing.Point(8, 8);
            this._lblEnter.Name = "_lblEnter";
            this._lblEnter.Size = new System.Drawing.Size(93, 23);
            this._lblEnter.TabIndex = 1;
            this._lblEnter.Text = "Enter password:";
            // 
            // _lblConfirm
            // 
            this._lblConfirm.Location = new System.Drawing.Point(8, 32);
            this._lblConfirm.Name = "_lblConfirm";
            this._lblConfirm.Size = new System.Drawing.Size(93, 23);
            this._lblConfirm.TabIndex = 3;
            this._lblConfirm.Text = "Confirm password:";
            // 
            // _lblError
            // 
            this._lblError.ForeColor = System.Drawing.Color.Red;
            this._lblError.Location = new System.Drawing.Point(9, 62);
            this._lblError.Name = "_lblError";
            this._lblError.Size = new System.Drawing.Size(368, 16);
            this._lblError.TabIndex = 7;
            this._lblError.Text = "Validation error";
            // 
            // _lblStrength
            // 
            this._lblStrength.AutoSize = true;
            this._lblStrength.Location = new System.Drawing.Point(326, 35);
            this._lblStrength.Name = "_lblStrength";
            this._lblStrength.Size = new System.Drawing.Size(47, 13);
            this._lblStrength.TabIndex = 8;
            this._lblStrength.Text = "Strength";
            // 
            // _lblHelp
            // 
            this._lblHelp.ForeColor = System.Drawing.Color.DarkGreen;
            this._lblHelp.Location = new System.Drawing.Point(10, 81);
            this._lblHelp.Name = "_lblHelp";
            this._lblHelp.Size = new System.Drawing.Size(368, 16);
            this._lblHelp.TabIndex = 13;
            this._lblHelp.Text = "Help text";
            // 
            // passwordProgressBar1
            // 
            this.passwordProgressBar1.BackColor = System.Drawing.Color.Transparent;
            this.passwordProgressBar1.Location = new System.Drawing.Point(316, 8);
            this.passwordProgressBar1.Name = "passwordProgressBar1";
            this.passwordProgressBar1.Size = new System.Drawing.Size(73, 19);
            this.passwordProgressBar1.TabIndex = 14;
            // 
            // PasswordDialog
            // 
            this.AcceptButton = this._btnOk;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this._btnCancel;
            this.ClientSize = new System.Drawing.Size(402, 135);
            this.Controls.Add(this.passwordProgressBar1);
            this.Controls.Add(this._lblHelp);
            this.Controls.Add(this._lblStrength);
            this.Controls.Add(this._lblError);
            this.Controls.Add(this._lblConfirm);
            this.Controls.Add(this._lblEnter);
            this.Controls.Add(this._txtConfirmation);
            this.Controls.Add(this._txtEnter);
            this.Controls.Add(this._btnCancel);
            this.Controls.Add(this._btnOk);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "PasswordDialog";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Password";
            this.Load += new System.EventHandler(this.PasswordDialog_Load);
            this.Closed += new System.EventHandler(this.PasswordDialog_Closed);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
Software engineer & architect.

Comments and Discussions