Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Building WPF Applications with Self-Tracking Entity Generator - Project Setup

, 20 Feb 2012
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator for WPF/Silverlight.
SchoolSample_20111201.zip
SchoolSample
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
Assemblies
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
ComponentModel.Composition.Initialization.Desktop
Microsoft
ComponentModel
Composition
Hosting
Internal
Properties
System
ComponentModel
Composition
Hosting
System.Windows.Controls.Data.Input
Common
Properties
Settings.settings
themes
Validation
Database
SchoolSample
SchoolSample.csproj.user
Asset
Control
Properties
Settings.settings
View
SchoolSample.Common
Model
Properties
Resource
SchoolSample.Data
EntityModel
Properties
Validation
SchoolSample.Data.Wcf
EntityModel
SchoolModel.edmx
Properties
Validation
SchoolSample.Model
Properties
SchoolSample.ViewModel
Properties
SchoolSample.Wcf
SchoolSample.Wcf.csproj.user
Properties
Service
SchoolService.svc
SchoolSample.WCFService
SchoolSample.WCFService.csproj.user
Properties
Service References
SchoolService
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
Test.SchoolSample.Model
Properties
Test References
SchoolSample.Model.accessor
Test.SchoolSample.ViewModel
Properties
Test References
SchoolSample.ViewModel.accessor
SchoolSample_20120117.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
SchoolSample_20120216.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace SchoolSample.EntityModel
{
    [DataContract(IsReference = true)]
    [KnownType(typeof(Enrollment))]
    [KnownType(typeof(Instructor))]
    public partial class Course: IObjectWithChangeTracker, INotifyPropertyChanged
    {
        #region Primitive Properties
    
        [DataMember]
        public int CourseId
        {
            get { return _courseId; }
            set
            {
                if (_courseId != value)
                {
                    if (ChangeTracker.ChangeTrackingEnabled && ChangeTracker.State != ObjectState.Added)
                    {
                        throw new InvalidOperationException("The property 'CourseId' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.");
                    }
                    PropertySetterEntry("CourseId");
                    _courseId = value;
                    PropertySetterExit("CourseId", value);
                    OnPropertyChanged("CourseId");
                }
            }
        }
        private int _courseId;
    
        [DataMember]
        [Display(Name="Instructor", Description="instructor")]
        public int InstructorId
        {
            get { return _instructorId; }
            set
            {
                if (_instructorId != value)
                {
                    ChangeTracker.RecordOriginalValue("InstructorId", _instructorId);
                    if (!IsDeserializing)
                    {
                        if (Instructor != null && Instructor.PersonId != value)
                        {
                            Instructor = null;
                        }
                    }
                    PropertySetterEntry("InstructorId");
                    _instructorId = value;
                    PropertySetterExit("InstructorId", value);
                    OnPropertyChanged("InstructorId");
                }
            }
        }
        private int _instructorId;
    
        [DataMember]
        [Display(Name="Course Name", Description="course name,  this field is required")]
        [Required(ErrorMessage="This field is required.")]
        [StringLength(50, MinimumLength=0, ErrorMessage="The maximum length of this field is 50.")]
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    ChangeTracker.RecordOriginalValue("Title", _title);
                    PropertySetterEntry("Title");
                    _title = value;
                    PropertySetterExit("Title", value);
                    OnPropertyChanged("Title");
                }
            }
        }
        private string _title;
    
        [DataMember]
        public byte[] Version
        {
            get { return _version; }
            set
            {
                if (_version != value)
                {
                    ChangeTracker.RecordOriginalValue("Version", _version);
                    PropertySetterEntry("Version");
                    _version = value;
                    PropertySetterExit("Version", value);
                    OnPropertyChanged("Version");
                }
            }
        }
        private byte[] _version;

        #endregion
        #region Navigation Properties
    
        [DataMember]
        public TrackableCollection<Enrollment> Enrollments
        {
            get
            {
                if (_enrollments == null)
                {
                    _enrollments = new TrackableCollection<Enrollment>();
                    _enrollments.CollectionChanged += FixupEnrollments;
                }
                return _enrollments;
            }
            set
            {
                if (!ReferenceEquals(_enrollments, value))
                {
                    if (ChangeTracker.ChangeTrackingEnabled)
                    {
                        throw new InvalidOperationException("Cannot set the FixupChangeTrackingCollection when ChangeTracking is enabled");
                    }
                    if (_enrollments != null)
                    {
                        _enrollments.CollectionChanged -= FixupEnrollments;
                        // This is the principal end in an association that performs cascade deletes.
                        // Remove the cascade delete event handler for any entities in the current collection.
                        foreach (Enrollment item in _enrollments)
                        {
                            ChangeTracker.ObjectStateChanging -= item.HandleCascadeDelete;
                        }
                    }
                    _enrollments = value;
                    if (_enrollments != null)
                    {
                        _enrollments.CollectionChanged += FixupEnrollments;
                        // This is the principal end in an association that performs cascade deletes.
                        // Add the cascade delete event handler for any entities that are already in the new collection.
                        foreach (Enrollment item in _enrollments)
                        {
                            ChangeTracker.ObjectStateChanging += item.HandleCascadeDelete;
                        }
                    }
                    OnNavigationPropertyChanged("Enrollments");
                }
            }
        }
        private TrackableCollection<Enrollment> _enrollments;
    
        [DataMember]
        public Instructor Instructor
        {
            get { return _instructor; }
            set
            {
                if (!ReferenceEquals(_instructor, value))
                {
                    var previousValue = _instructor;
                    PropertySetterEntry("Instructor");
                    _instructor = value;
                    PropertySetterExit("Instructor", value);
                    FixupInstructor(previousValue);
                    OnNavigationPropertyChanged("Instructor");
                }
            }
        }
        private Instructor _instructor;

        #endregion
        #region ChangeTracking
    
        protected virtual void OnPropertyChanged(String propertyName)
        {
            if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
            {
                ChangeTracker.State = ObjectState.Modified;
            }
            if (_propertyChanged != null)
            {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        protected virtual void OnNavigationPropertyChanged(String propertyName)
        {
            if (_propertyChanged != null)
            {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged{ add { _propertyChanged += value; } remove { _propertyChanged -= value; } }
        private event PropertyChangedEventHandler _propertyChanged;
        private ObjectChangeTracker _changeTracker;
    
        [DataMember]
        public ObjectChangeTracker ChangeTracker
        {
            get
            {
                if (_changeTracker == null)
                {
                    _changeTracker = new ObjectChangeTracker();
                    _changeTracker.SetParentObject(this);
                    _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
                    _changeTracker.ObjectStateChanged += HandleObjectStateChanged;
                    _changeTracker.UpdateHasChanges += HandleUpdateHasChanges;
                }
                return _changeTracker;
            }
            set
            {
                if(_changeTracker != null)
                {
                    _changeTracker.ObjectStateChanging -= HandleObjectStateChanging;
                    _changeTracker.ObjectStateChanged -= HandleObjectStateChanged;
                    _changeTracker.UpdateHasChanges -= HandleUpdateHasChanges;
                }
                _changeTracker = value;
                _changeTracker.SetParentObject(this);
                if(_changeTracker != null)
                {
                    _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
                    _changeTracker.ObjectStateChanged += HandleObjectStateChanged;
                    _changeTracker.UpdateHasChanges += HandleUpdateHasChanges;
                }
            }
        }
    
        private void HandleObjectStateChanging(object sender, ObjectStateChangingEventArgs e)
        {
            if (e.NewState == ObjectState.Deleted)
            {
                ClearNavigationProperties();
            }
        }
    
        private void HandleObjectStateChanged(object sender, ObjectStateChangedEventArgs e)
        {
    #if (SILVERLIGHT || WPF)
            // update HasChanges property
            HasChanges = (ChangeTracker.State == ObjectState.Added) ||
                (ChangeTracker.ChangeTrackingEnabled &&
                (ChangeTracker.State != ObjectState.Unchanged ||
                 ChangeTracker.ObjectsAddedToCollectionProperties.Count != 0 ||
                 ChangeTracker.ObjectsRemovedFromCollectionProperties.Count != 0));
    #endif
        }
    
        private void HandleUpdateHasChanges(object sender, EventArgs e)
        {
    #if (SILVERLIGHT || WPF)
            // update HasChanges property
            HasChanges = (ChangeTracker.State == ObjectState.Added) ||
                (ChangeTracker.ChangeTrackingEnabled &&
                (ChangeTracker.State != ObjectState.Unchanged ||
                 ChangeTracker.ObjectsAddedToCollectionProperties.Count != 0 ||
                 ChangeTracker.ObjectsRemovedFromCollectionProperties.Count != 0));
    #endif
        }
    
        protected bool IsDeserializing { get; private set; }
    
        [OnDeserializing]
        public void OnDeserializingMethod(StreamingContext context)
        {
            IsDeserializing = true;
        }
    
        [OnDeserialized]
        public void OnDeserializedMethod(StreamingContext context)
        {
            IsDeserializing = false;
            ChangeTracker.ChangeTrackingEnabled = true;
        }
    
        protected virtual void ClearNavigationProperties()
        {
            Enrollments.Clear();
            Instructor = null;
        }

        #endregion
        #region Validation and Helper Method
    
        /// <summary>
        /// Loops through all related data annotation attributes as well as all related
        /// custom validation actions for the specified property name. If  propertyName
        /// is String.Empty/null, the validation is on the entity level.
        /// </summary>
        /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
        /// <param name="value">New value that needs to be validated for the specified property</param>
        public virtual void Validate(string propertyName, object value)
        {
            // process Data Annotation Attributes
            var validationResults = new Collection<ValidationResult>();
            if (string.IsNullOrEmpty(propertyName))
            {
                // this is top-level validation
                ValidationContext context = new ValidationContext(this, null, null);
                Validator.TryValidateObject(this, context, validationResults, true);
            }
            else
            {
                PropertyInfo property = GetType().GetProperty(propertyName);
                if (property != null)
                {
                    // this is validation for each property
                    ValidationContext context = new ValidationContext(this, null, null) { MemberName = propertyName };
                    Validator.TryValidateProperty(value, context, validationResults);
                }
            }
            if (validationResults.Count != 0)
            {
    #if (SILVERLIGHT || WPF)
                foreach (ValidationResult error in validationResults)
                {
                    if (error.MemberNames.Count() == 0)
                    {
                        // this is top-level validation error
                        AddError(string.Empty, error);
                    }
                    else
                    {
                        foreach (string memberName in error.MemberNames)
                        {
                            AddError(memberName, error);
                        }
                    }
                }
    #else
                FaultReason faultReason = new FaultReason(validationResults.First().ErrorMessage);
                throw new FaultException(faultReason);
    #endif
            }
            // next, process custom validation actions
            if (ValidationActions.Count == 0) InitializeValidationSettings();
            if (string.IsNullOrEmpty(propertyName))
            {
                // loop through all properties and call its list of validation actions
                foreach (string memberName in ValidationActions.Keys.ToList())
                {
                    List<Action<object>> actions = ValidationActions[memberName];
                    if (string.IsNullOrEmpty(memberName))
                    {
                        // this is top-level validation
                        actions.ForEach(action => action(this));
                    }
                    else
                    {
                        // this is validation for each property
                        object memberValue = GetType().GetProperty(memberName).GetValue(this, null);
                        actions.ForEach(action => action(memberValue));
                    }
                }
            }
            else
            {
                if (ValidationActions.ContainsKey(propertyName))
                {
                    List<Action<object>> actions = ValidationActions[propertyName];
                    actions.ForEach(action => action(value));
                }
            }
        }
    
    #if SILVERLIGHT
        /// <summary>
        /// Client-side validation function to loop through all data annotation
        /// attributes and all custom validation actions.  If any validation fails,
        /// this function will return false. Otherwise, true.
        /// </summary>
        /// <returns></returns>
        public virtual bool TryValidate()
        {
            // clear known errors for all property names.
            ClearErrors();
            // process Data Annotation Attributes
            ValidationContext context = new ValidationContext(this, null, null);
            var validationResults = new Collection<ValidationResult>();
            Validator.TryValidateObject(this, context, validationResults, true);
            if (validationResults.Count != 0)
            {
                foreach (ValidationResult error in validationResults)
                {
                    if (error.MemberNames.Count() == 0)
                    {
                        // this is top-level validation error
                        AddError(string.Empty, error);
                    }
                    else
                    {
                        foreach (string propertyName in error.MemberNames)
                        {
                            AddError(propertyName, error);
                        }
                    }
                }
            }
            // next, process custom validation actions
            if (ValidationActions.Count == 0) InitializeValidationSettings();
            // loop through all properties and call its list of validation actions
            foreach (string propertyName in ValidationActions.Keys.ToList())
            {
                List<Action<object>> actions = ValidationActions[propertyName];
                if (string.IsNullOrEmpty(propertyName))
                {
                    // this is top-level validation
                    actions.ForEach(action => action(this));
                }
                else
                {
                    // this is validation for each property
                    object value = GetType().GetProperty(propertyName).GetValue(this, null);
                    actions.ForEach(action => action(value));
                }
            }
            return !(((INotifyDataErrorInfo)this).HasErrors);
        }
    
        /// <summary>
        /// Client-side validation function for propertyName.
        /// If any validation fails, this function will return false.
        /// Otherwise, true.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public bool TryValidate(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return TryValidate();
            }
            PropertyInfo property = GetType().GetProperty(propertyName);
            if (property == null)
            {
                throw new ArgumentException(propertyName + " does not exist.");
            }
            // clear known errors for the property name
            ClearErrors(propertyName);
            // get the value of propertyName
            object value = GetType().GetProperty(propertyName).GetValue(this, null);
            Validate(propertyName, value);
            IEnumerable errors = ((INotifyDataErrorInfo)this).GetErrors(propertyName);
            if (errors == null) return true;
            int count = errors.Cast<object>().Count();
            return count == 0;
        }
    
        /// <summary>
        /// Client-side validation function to loop through the whole object
        /// graph.  If any validation fails, this function will return false. 
        /// Otherwise, true.
        /// </summary>
        /// <returns></returns>
        public bool TryValidateObjectGraph()
        {
            bool validated = true;
            var visitedGraph = new List<object>();
            TryValidateObjectGraph(ref validated, ref visitedGraph);
            return validated;
        }
    
        internal virtual void TryValidateObjectGraph(ref bool validated, ref List<object> visitedGraph)
        {
            // if already visited this object, just return
            if (visitedGraph.Any(n => ReferenceEquals(n, this))) return;
    
            if (!TryValidate()) validated = false;
            visitedGraph.Add(this);
    
            // call TryValidateObjectGraph() on all Navigation properties
            TryValidateOnNavigationProperties(ref validated, ref visitedGraph);
        }
    
        internal virtual void TryValidateOnNavigationProperties(ref bool validated, ref List<object> visitedGraph)
        {
            foreach (var item in Enrollments)
            {
                item.TryValidateObjectGraph(ref validated, ref visitedGraph);
            }
            if (Instructor != null)
            {
                Instructor.TryValidateObjectGraph(ref validated, ref visitedGraph);
            }
        }
    #elif WPF
        /// <summary>
        /// Client-side validation function to loop through all data annotation
        /// attributes and all custom validation actions.  If any validation fails,
        /// this function will return false. Otherwise, true.
        /// </summary>
        /// <returns></returns>
        public virtual bool TryValidate()
        {
            // clear known errors for all property names.
            ClearErrors();
            // First, process Data Annotation Attributes
            ValidationContext context = new ValidationContext(this, null, null);
            var validationResults = new Collection<ValidationResult>();
            Validator.TryValidateObject(this, context, validationResults, true);
            if (validationResults.Count != 0)
            {
                foreach (ValidationResult error in validationResults)
                {
                    if (error.MemberNames.Count() == 0)
                    {
                        // this is top-level validation error
                        AddError(string.Empty, error);
                    }
                    else
                    {
                        foreach (string propertyName in error.MemberNames)
                        {
                            AddError(propertyName, error);
                        }
                    }
                }
            }
            // Next, process custom validation actions
            if (ValidationActions.Count == 0) InitializeValidationSettings();
            // loop through all properties and call its list of validation actions
            foreach (string propertyName in ValidationActions.Keys.ToList())
            {
                List<Action<object>> actions = ValidationActions[propertyName];
                if (string.IsNullOrEmpty(propertyName))
                {
                    // this is top-level validation
                    actions.ForEach(action => action(this));
                }
                else
                {
                    // this is validation for each property
                    object value = GetType().GetProperty(propertyName).GetValue(this, null);
                    actions.ForEach(action => action(value));
                }
            }
            return (ValidationErrors.Count == 0);
        }
    
        /// <summary>
        /// Client-side validation function for propertyName.
        /// If any validation fails, this function will return false.
        /// Otherwise, true.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public bool TryValidate(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return TryValidate();
            }
            PropertyInfo property = GetType().GetProperty(propertyName);
            if (property == null)
            {
                throw new ArgumentException(propertyName + " does not exist.");
            }
            // clear known errors for the property name
            ClearErrors(propertyName);
            // get the value of propertyName
            object value = GetType().GetProperty(propertyName).GetValue(this, null);
            Validate(propertyName, value);
            return ValidationErrors.ContainsKey(propertyName);
        }
    
        /// <summary>
        /// Client-side validation function to loop through the whole object
        /// graph.  If any validation fails, this function will return false. 
        /// Otherwise, true.
        /// </summary>
        /// <returns></returns>
        public bool TryValidateObjectGraph()
        {
            bool validated = true;
            var visitedGraph = new List<object>();
            TryValidateObjectGraph(ref validated, ref visitedGraph);
            return validated;
        }
    
        internal virtual void TryValidateObjectGraph(ref bool validated, ref List<object> visitedGraph)
        {
            // if already visited this object, just return
            if (visitedGraph.Any(n => ReferenceEquals(n, this))) return;
    
            if (!TryValidate()) validated = false;
            visitedGraph.Add(this);
    
            // call TryValidateObjectGraph() on all Navigation properties
            TryValidateOnNavigationProperties(ref validated, ref visitedGraph);
        }
    
        internal virtual void TryValidateOnNavigationProperties(ref bool validated, ref List<object> visitedGraph)
        {
            foreach (var item in Enrollments)
            {
                item.TryValidateObjectGraph(ref validated, ref visitedGraph);
            }
            if (Instructor != null)
            {
                Instructor.TryValidateObjectGraph(ref validated, ref visitedGraph);
            }
        }
    #else
        /// <summary>
        /// Server-side validation function to loop through all data annotation
        /// attributes and all custom validation actions.  If any validation fails,
        /// it will throw an exception.
        /// </summary>
        public virtual void Validate()
        {
            // process Data Annotation Attributes
            ValidationContext context = new ValidationContext(this, null, null);
            var validationResults = new Collection<ValidationResult>();
            Validator.TryValidateObject(this, context, validationResults, true);
            if (validationResults.Count != 0)
            {
                ValidationResult error = validationResults.First();
                if (error.MemberNames.Count() == 0)
                {
                    // this is top-level validation error
                    FaultReason faultReason = new FaultReason(error.ErrorMessage);
                    throw new FaultException(faultReason);
                }
                else
                {
                    string propertyName = error.MemberNames.First();
                    FaultReason faultReason = new FaultReason(propertyName + " : " + error.ErrorMessage);
                    throw new FaultException(faultReason);
                }
            }
            // Next, process custom validation actions
            if (ValidationActions.Count == 0) InitializeValidationSettings();
            // loop through all properties and call its list of validation actions
            foreach (string propertyName in ValidationActions.Keys.ToList())
            {
                List<Action<object>> actions = ValidationActions[propertyName];
                if (string.IsNullOrEmpty(propertyName))
                {
                    // this is top-level validation
                    actions.ForEach(action => action(this));
                }
                else
                {
                    // this is validation for each property
                    object value = GetType().GetProperty(propertyName).GetValue(this, null);
                    actions.ForEach(action => action(value));
                }
            }
        }
    
        /// <summary>
        /// Server-side validation function to loop through the whole object
        /// graph.  If any validation fails, it will throw an exception.
        /// </summary>
        /// <returns></returns>
        public void ValidateObjectGraph()
        {
            var visitedGraph = new List<object>();
            ValidateObjectGraph(ref visitedGraph);
        }
    
        internal virtual void ValidateObjectGraph(ref List<object> visitedGraph)
        {
            // if already visited this object, just return
            if (visitedGraph.Any(n => ReferenceEquals(n, this))) return;
    
            Validate();
            visitedGraph.Add(this);
    
            // call ValidateObjectGraph() on all Navigation properties
            ValidateOnNavigationProperties(ref visitedGraph);
        }
    
        internal virtual void ValidateOnNavigationProperties(ref List<object> visitedGraph)
        {
            foreach (var item in Enrollments)
            {
                item.ValidateObjectGraph(ref visitedGraph);
            }
            if (Instructor != null)
            {
                Instructor.ValidateObjectGraph(ref visitedGraph);
            }
        }
    #endif
    	partial void PropertySetterEntry(string propertyName);
        partial void PropertySetterExit(string propertyName, object propertyValue);
        partial void InitializeValidationSettings();
    
        private Dictionary<string, List<Action<object>>> ValidationActions
        {
            get
            {
                if (_validationActions == null)
                {
                    _validationActions = new Dictionary<string, List<Action<object>>>();
                }
                return _validationActions;
            }
        }
        private Dictionary<string, List<Action<object>>> _validationActions;
    
        /// <summary>
        /// Add one validation action for the specified property name.
        /// </summary>
        /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
        /// <param name="validationAction">Function to carry out a specific validation action</param>
        private void AddPropertyValidationAction(string propertyName, Action<object> validationAction)
        {
            List<Action<object>> validationActionList;
            if (!ValidationActions.ContainsKey(propertyName))
            {
                validationActionList = new List<Action<object>>();
                ValidationActions.Add(propertyName, validationActionList);
            }
            else
                validationActionList = ValidationActions[propertyName];
    
            validationActionList.Add(validationAction);
        }

        #endregion
        #region Association Fixup
    
        private void FixupInstructor(Instructor previousValue)
        {
            if (IsDeserializing)
            {
                return;
            }
    
            if (previousValue != null && previousValue.Courses.Contains(this))
            {
                previousValue.Courses.Remove(this);
            }
    
            if (Instructor != null)
            {
                if (!Instructor.Courses.Contains(this))
                {
                    Instructor.Courses.Add(this);
                }
    
                InstructorId = Instructor.PersonId;
            }
            if (ChangeTracker.ChangeTrackingEnabled)
            {
                if (ChangeTracker.OriginalValues.ContainsKey("Instructor")
                    && (ChangeTracker.OriginalValues["Instructor"] == Instructor))
                {
                    ChangeTracker.OriginalValues.Remove("Instructor");
                }
                else
                {
                    ChangeTracker.RecordOriginalValue("Instructor", previousValue);
                }
                if (Instructor != null && !Instructor.ChangeTracker.ChangeTrackingEnabled)
                {
                    Instructor.StartTracking();
                }
            }
        }
    
        private void FixupEnrollments(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (IsDeserializing)
            {
                return;
            }
    
            if (e.NewItems != null)
            {
                foreach (Enrollment item in e.NewItems)
                {
                    item.Course = this;
                    if (ChangeTracker.ChangeTrackingEnabled)
                    {
                        if (!item.ChangeTracker.ChangeTrackingEnabled)
                        {
                            item.StartTracking();
                        }
                        ChangeTracker.RecordAdditionToCollectionProperties("Enrollments", item);
                    }
                    // This is the principal end in an association that performs cascade deletes.
                    // Update the event listener to refer to the new dependent.
                    ChangeTracker.ObjectStateChanging += item.HandleCascadeDelete;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Enrollment item in e.OldItems)
                {
                    if (ReferenceEquals(item.Course, this))
                    {
                        item.Course = null;
                    }
                    if (ChangeTracker.ChangeTrackingEnabled)
                    {
                        ChangeTracker.RecordRemovalFromCollectionProperties("Enrollments", item);
                    }
                    // This is the principal end in an association that performs cascade deletes.
                    // Remove the previous dependent from the event listener.
                    ChangeTracker.ObjectStateChanging -= item.HandleCascadeDelete;
                }
            }
        }

        #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)

Share

About the Author

Weidong Shen
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 20 Feb 2012
Article Copyright 2011 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid