Click here to Skip to main content
15,886,737 members
Articles / Desktop Programming / Windows Forms

Visual Application Launcher

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
23 Jan 2012CPOL23 min read 107.1K   3.7K   116  
A WinForms UI using WCF services, Entity Framework, repository data access, repository caching, Unit of Work, Dependency Injection, and every other buzz work you can think of!
namespace VAL
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ServiceModel;
    using VAL.Controls;
    using VAL.Common;
    using VAL.Contracts;
    using VAL.Contracts.ServiceFaults;
    using VAL.Model;
    using System.ServiceModel.Security;

    public partial class UserMaintenance : PersistedForm
    {
        private User userInstance;
        private ListViewItem selectedItem;

        #region Construct and Load

        /// <summary>
        /// Default ctor
        /// </summary>
        public UserMaintenance(IAdministratorService service)
        {
            InitializeComponent();
            this.Service = service;

            LoadUsers();
        }

        private void UserMaintenance_Load(object sender, EventArgs e)
        {
            this.SetFormEditMode(false);
        }

        #endregion

        private IAdministratorService Service { get; set; }

        private void SetFormEditMode(bool editing)
        {
            this.detailsBox.Enabled = editing;
            this.applyButton.Enabled = editing;
            this.usersList.Enabled = !editing;

            this.userSearchButton.Enabled = (!string.IsNullOrEmpty(Session.Configuration.ActiveDirectoryDomain));

            if (!editing)
            {
                base.ClearEditFields(this);
            }            
        }

        private void BindObjectFields()
        {
            BindingHelper.BindField(this.windowsIdentityText, BindingProperties.Text, userInstance, "WindowsIdentityName");
            BindingHelper.BindField(this.forenameText, BindingProperties.Text, userInstance, "Forename");
            BindingHelper.BindField(this.surnameText, BindingProperties.Text, userInstance, "Surname");
            BindingHelper.BindField(this.activeCheckBox, BindingProperties.Checked, userInstance, "IsActive");
        }

        private void LoadUsers()
        {
            var users = Service.GetUsers();
            try
            {
                this.usersList.BeginUpdate();
                this.usersList.Items.Clear();

                ListViewItem[] viewItems = new ListViewItem[users.Length];

                for (int i = 0; i < viewItems.Length; ++i)
                {
                    var user = users[i];

                    string[] items = new string[]{ user.FullName, user.WindowsIdentityName};
                    viewItems[i] = new ListViewItem(items);

                    viewItems[i].Tag = user;
                    viewItems[i].ImageIndex = (user.IsActive ? 0 : 1);       
                }

                this.usersList.Items.AddRange(viewItems);
            }
            finally
            {
                this.usersList.EndUpdate();
            }
           
        }

        private void userSearchButton_Click(object sender, EventArgs e)
        {
            using (var picker = new UserPicker(Session.Configuration.ActiveDirectoryDomain))
            {
                if (picker.ShowDialog(this) == DialogResult.OK)
                {
                    ActiveDirectoryUser user = picker.SelectedUser;

                    userInstance.WindowsIdentityName = user.DomainAccount;
                    userInstance.Forename = user.Forename;
                    userInstance.Surname = user.Surname;

                    BindObjectFields();
                }
            }
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            if (!this.userInstance.IsValid)
            {
                Messaging.ShowError(userInstance.ErrorMessage);
                return;
            }

            bool isnew = (userInstance.Id == 0);

            try
            {
                userInstance = Service.SaveUser(userInstance);    
            }
            catch (FaultException<UserFault> ex)
            {
                Messaging.ShowError(ex.Message);
                return;
            }

            ListViewItem lvi;
            if (isnew)
                lvi = new ListViewItem();
            else
                lvi = selectedItem;

            lvi.Text = userInstance.FullName;
            lvi.SubItems.Add(userInstance.WindowsIdentityName);
            lvi.Tag = userInstance;
            lvi.ImageIndex = (userInstance.IsActive ? 0 : 1);

            if (selectedItem == null)
            {
                this.usersList.Items.Add(lvi);
            }

            SetFormEditMode(false);            
        }


        private void EditUser()
        {
            if (this.usersList.SelectedItems.Count == 0) return;
            selectedItem = usersList.SelectedItems[0];

            this.userInstance = (User)selectedItem.Tag;

            // Set up object binding and switch to edit mode
            BindObjectFields();
            SetFormEditMode(true);
        }

        private void usersList_DoubleClick(object sender, EventArgs e)
        {
            EditUser();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            if (!this.usersList.Enabled)
            {
                SetFormEditMode(false);
            }
            else
            {
                this.Close();
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.userInstance = new User();
            userInstance.IsActive = true;

            BindObjectFields();
            SetFormEditMode(true);
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditUser();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!TryGetListViewItem(this.usersList, out selectedItem))
                return;

            var user = (User)selectedItem.Tag;

            if (Messaging.AskQuestion(string.Format("Delete user {0} from the database? They will also be removed from all group defintions", user.FullName)) == System.Windows.Forms.DialogResult.Yes)
            {
                Service.DeleteUser(user.Id);
                this.usersList.Items.Remove(selectedItem);
                this.selectedItem = null;
            }     
        }
    }
}

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
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions