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.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

#if WPF
namespace SchoolSample.EntityModel
{
    public partial class Instructor : Person, IClientChangeTracking, IChangeTracking, IRevertibleChangeTracking, IDataErrorInfo, IEditableObject
    {
        #region IClientChangeTracking Interface
    
        /// <summary>
        /// Accepts changes made to the entity object
        /// </summary>
        void IClientChangeTracking.AcceptChanges()
        {
            this.AcceptChanges();
        }
    
        /// <summary>
        /// Rejects changes made to the entity object
        /// </summary>
        void IClientChangeTracking.RejectChanges()
        {
            this.RejectChanges();
        }
    
        /// <summary>
        /// Accepts changes made to the entity object and all objects of its object graph
        /// </summary>
        public override void AcceptObjectGraphChanges()
        {
            this.AcceptChanges();
            // call AccetChanges() on all Navigation properties
            foreach (var item in Courses)
                item.AcceptChanges();
            // call base AcceptObjectGraphChanges() 
            base.AcceptObjectGraphChanges();
        }
    
        /// <summary>
        /// Rejects changes made to the entity object and all objects of its object graph
        /// </summary>
        public override void RejectObjectGraphChanges()
        {
            this.RejectChanges();
            // call RejectChanges() on all Navigation properties
            foreach (var item in Courses)
                item.RejectChanges();
            // call base RejectObjectGraphChanges() 
            base.RejectObjectGraphChanges();
        }
    
        internal override bool ObjectGraphHasChanges(List<object> objectList)
        {
            // if already visited this object, just return false
            if (objectList.Any(n => ReferenceEquals(n, this))) return false;
    
            bool hasChanges = base.ObjectGraphHasChanges(objectList);
            if (hasChanges) return true;
    
            // if not, add itself to the visited graph
            var visitedGraph = objectList.ToList();
            visitedGraph.Add(this);
    
            // call ObjectGraphHasChanges() on all Navigation properties
            foreach (var item in Courses)
            {
                hasChanges = item.ObjectGraphHasChanges(visitedGraph);
                if (hasChanges) return true;
            }
            return false;
        }
    
        internal override void EstimateObjectGraphSize(ref long size, ref List<object> visitedGraph)
        {
            // if already visited this object, just return
            if (visitedGraph.Any(n => ReferenceEquals(n, this))) return;
    
            // call base EstimateObjectGraphSize()
            base.EstimateObjectGraphSize(ref size, ref visitedGraph);
    
            size += EstimateSize;
            if (visitedGraph.All(i => !ReferenceEquals(i, this))) visitedGraph.Add(this);
    
            // call EstimateObjectGraphSize() on all Navigation properties
            foreach (var item in Courses)
            {
                item.EstimateObjectGraphSize(ref size, ref visitedGraph);
            }
        }
    
        internal override void EstimateObjectGraphChangeSize(ref long size, List<object> objectList)
        {
    
            // call base EstimateObjectGraphChangeSize()
            base.EstimateObjectGraphChangeSize(ref size, objectList);
    
            size += EstimateSize;
    
            // call EstimateObjectGraphChangeSize() on all Navigation properties that has change
            foreach (var n in Courses)
            {
                // if n has already been visited, we need to skip it
                if (objectList.All(i => !ReferenceEquals(i, n)))
                {
                    if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("Courses")
                        && ChangeTracker.ObjectsAddedToCollectionProperties["Courses"].Contains(n))
                    {
                        // if n exists in ObjectsAddedToCollectionProperties, we need to add its size
                        var visitedGraph = objectList.ToList();
                        visitedGraph.Add(n);
                        n.EstimateObjectGraphChangeSize(ref size, visitedGraph);
                    }
                    else if (n.ObjectGraphHasChanges(objectList))
                    {
                        // if n has changes, we also need to add its size
                        var visitedGraph = objectList.ToList();
                        visitedGraph.Add(n);
                        n.EstimateObjectGraphChangeSize(ref size, visitedGraph);
                    }
                }
            }
        }
    
        internal override void TrimUnchangedEntities(List<object> objectList)
        {
            // call base TrimUnchangedEntities() first
            base.TrimUnchangedEntities(objectList);
    
            bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
            this.StopTracking();
    
            // trim all navigation property items that do not have any change
            foreach (var n in Courses.ToList())
            {
                // if n has already been visited, we need to skip it
                if (objectList.All(i => !ReferenceEquals(i, n)))
                {
                    if (ChangeTracker.ObjectsAddedToCollectionProperties.ContainsKey("Courses")
                        && ChangeTracker.ObjectsAddedToCollectionProperties["Courses"].Contains(n))
                    {
                        // if n exists in ObjectsAddedToCollectionProperties, we need to keep it
                        var visitedGraph = objectList.ToList();
                        visitedGraph.Add(n);
                        n.TrimUnchangedEntities(visitedGraph);
                    }
                    else if (n.ObjectGraphHasChanges(objectList))
                    {
                        // if n has changes, we also need to keep it
                        var visitedGraph = objectList.ToList();
                        visitedGraph.Add(n);
                        n.TrimUnchangedEntities(visitedGraph);
                    }
                    else
                    {
                        Courses.Remove(n);
                    }
                }
            }
    
            ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
        }

        #endregion
        #region IClientChangeTracking Helper Property
    
        internal override long EstimateSize
        {
            get
            {
                long _size = 0;
                // estimate size of all Primitive Properties
                _size += sizeof(ulong);    // HireDate
                _size += sizeof(Decimal);    // Salary
                return _size;
            }
        }

        #endregion
        #region IChangeTracking and IRevertibleChangeTracking interfaces
    
        void IChangeTracking.AcceptChanges()
        {
            this.AcceptChanges();
        }
    
        bool IChangeTracking.IsChanged
        {
            get { return HasChanges; }
        }
    
        void IRevertibleChangeTracking.RejectChanges()
        {
            this.RejectChanges();
        }

        #endregion
        #region IDataErrorInfo interface
    
        string IDataErrorInfo.Error
        {
            get
            {
                if (ValidationErrors.ContainsKey(string.Empty))
                {
                    return ValidationErrors[string.Empty].ErrorMessage;
                }
                return null;
            }
        }
    
        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                if (propertyName == null)
                {
                    propertyName = string.Empty;
                }
    
                if (ValidationErrors.ContainsKey(propertyName))
                {
                    return ValidationErrors[propertyName].ErrorMessage;
                }
                return null;
            }
        }

        #endregion
        #region IDataErrorInfo Protected & Private Helper Methods
    
        /// <summary>
        /// Removes any known errors for the provided property name
        /// by calling ClearErrors()
        /// </summary>
        /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
        partial void PropertySetterEntry(string propertyName)
        {
            if (IsDeserializing || SuspendValidation)
            {
                return;
            }
    
            if (ValidateEntityOnPropertyChanged)
            {
                ClearErrors();
            }
            else
            {
                ClearErrors(propertyName);
            }
        }
    
        /// <summary>
        /// Validates for any known errors for the provided property name
        /// </summary>
        /// <param name="propertyName">Propery name or String.Empty/null for top-level errors</param>
        /// <param name="propertyValue">Property value</param>
        partial void PropertySetterExit(string propertyName, object propertyValue)
        {
            if (IsDeserializing || SuspendValidation)
            {
                return;
            }
    
            if (ValidateEntityOnPropertyChanged)
            {
                Validate(string.Empty, this);
            }
            else
            {
                Validate(propertyName, propertyValue);
            }
        }

        #endregion
        #region IEditableObject interface
    
        private Dictionary<string , object> _cache;
    
        public override void BeginEdit()
        {
            // call base BeginEdit() first
            base.BeginEdit();
            if (_cache == null) _cache = new Dictionary<string, object>();
            // copy all Primitive Properties except the primary key fields
            _cache["HireDate"] = HireDate;
            _cache["Salary"] = Salary;
            // copy all Navigation Properties
            _cache["Courses"] = Courses;
        }
    
        public override void CancelEdit()
        {
            // call base CancelEdit() first
            base.CancelEdit();
            if (_cache == null) _cache = new Dictionary<string, object>();
            if (_cache.Count == 0) return;
            bool changeTrackingEnabled = ChangeTracker.ChangeTrackingEnabled;
            this.StopTracking();
            // copy all Primitive Properties except the primary key fields
            if (HireDate != (Nullable<System.DateTime>)_cache["HireDate"])
                HireDate = (Nullable<System.DateTime>)_cache["HireDate"];
            else
                OnPropertyChanged("HireDate");
            if (Salary != (Nullable<decimal>)_cache["Salary"])
                Salary = (Nullable<decimal>)_cache["Salary"];
            else
                OnPropertyChanged("Salary");
            // copy all Navigation Properties
            Courses = (TrackableCollection<Course>)_cache["Courses"];
            ChangeTracker.ChangeTrackingEnabled = changeTrackingEnabled;
            _cache.Clear();
        }
    
        public override void EndEdit()
        {
            // call base EndEdit() first
            base.EndEdit();
            if (_cache == null) _cache = new Dictionary<string, object>();
            _cache.Clear();
        }

        #endregion
    }
}
#endif

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
Web01 | 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