using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Csla; using Volian.Object.Library; namespace SimpleTest { public partial class frmUser : Form { User _user; public User User { get { return _user; } } private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public frmUser(int uid) { InitializeComponent(); _user = User.Get(uid); _user.BeginEdit(); //_user.PropertyChanged += new PropertyChangedEventHandler(_user_PropertyChanged); //_user.UserMemberships.ListChanged += new ListChangedEventHandler(UserMemberships_ListChanged); userBindingSource.DataSource = _user; } void UserMemberships_ListChanged(object sender, ListChangedEventArgs e) { LogDirtyStatus("List Changed - " + e.ListChangedType.ToString()); } void _user_PropertyChanged(object sender, PropertyChangedEventArgs e) { LogDirtyStatus("Property Changed - " + e.PropertyName); } private void LogDirtyStatus(string s) { log.InfoFormat("--------{0}-------",s); log.InfoFormat("IsDirty = {0}",_user.IsDirty); foreach(UserMembership um in _user.UserMemberships){ log.InfoFormat("{0} IsDirty = {1}",um.Group_GroupName,um.IsDirty); } } private void Save(bool rebind) { this.userBindingSource.RaiseListChangedEvents = false; this.userMembershipsBindingSource.RaiseListChangedEvents = false; // do the save this.userBindingSource.EndEdit(); this.userMembershipsBindingSource.EndEdit(); try { User temp = _user.Clone(); _user = temp.Save(); _user.BeginEdit(); if (rebind) { // rebind the UI this.userBindingSource.DataSource = null; this.userMembershipsBindingSource.DataSource = this.userBindingSource; this.userBindingSource.DataSource = _user; // ApplyAuthorizationRules(); } } catch (Csla.DataPortalException ex) { MessageBox.Show(ex.BusinessException.ToString(), "Error saving", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error Saving", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { this.userBindingSource.RaiseListChangedEvents = true; this.userMembershipsBindingSource.RaiseListChangedEvents = true; this.userBindingSource.ResetBindings(false); this.userMembershipsBindingSource.ResetBindings(false); } } private void btnSave_Click(object sender, EventArgs e) { LogDirtyStatus("Before Save"); Save(true); LogDirtyStatus("After Save"); } private void btnCancel_Click(object sender, EventArgs e) { LogDirtyStatus("Before Cancel"); userBindingSource.RaiseListChangedEvents=false; userBindingSource.CancelEdit(); userBindingSource.RaiseListChangedEvents = true; // _user.CancelEdit(); _user.BeginEdit(); LogDirtyStatus("After Cancel"); } } }