EntityObject.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataEntity / System / Data / Objects / DataClasses / EntityObject.cs / 2 / EntityObject.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner       [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 
using System.Data;
using System.Diagnostics; 
using System.Reflection;
using System.ComponentModel;
using System.Runtime.Serialization;
 
namespace System.Data.Objects.DataClasses
{ 
    ///  
    /// This is the class is the basis for all perscribed EntityObject classes.
    ///  
    [DataContract(IsReference=true)]
    [Serializable]
    public abstract class EntityObject : StructuralObject, IEntityWithKey, IEntityWithChangeTracker, IEntityWithRelationships
    { 
        #region Privates
 
        // The following 2 fields are serialized.  Adding or removing a serialized field is considered 
        // a breaking change.  This includes changing the field type or field name of existing
        // serialized fields. If you need to make this kind of change, it may be possible, but it 
        // will require some custom serialization/deserialization code.
        private RelationshipManager _relationships;
        private EntityKey _entityKey;
 
        [NonSerialized]
        private IEntityChangeTracker _entityChangeTracker = s_detachedEntityChangeTracker; 
        [NonSerialized] 
        private static readonly DetachedEntityChangeTracker s_detachedEntityChangeTracker = new DetachedEntityChangeTracker();
 
        /// 
        /// Helper class used when we are not currently attached to a change tracker.
        /// Simplifies the code so we don't always have to check for null before using the change tracker
        ///  
        private class DetachedEntityChangeTracker : IEntityChangeTracker
        { 
            void IEntityChangeTracker.EntityMemberChanging(string entityMemberName) { } 
            void IEntityChangeTracker.EntityMemberChanged(string entityMemberName) { }
            void IEntityChangeTracker.EntityComplexMemberChanging(string entityMemberName, object complexObject, string complexMemberName) { } 
            void IEntityChangeTracker.EntityComplexMemberChanged(string entityMemberName, object complexObject, string complexMemberName) { }
            EntityState IEntityChangeTracker.EntityState
            {
                get 
                {
                    return EntityState.Detached; 
                } 
            }
        } 

        private IEntityChangeTracker EntityChangeTracker
        {
            get 
            {
                if (_entityChangeTracker == null) 
                { 
                    _entityChangeTracker = s_detachedEntityChangeTracker;
                } 
                return _entityChangeTracker;
            }
            set
            { 
                _entityChangeTracker = value;
            } 
        } 

        #endregion 
        #region Publics
        /// 
        /// The storage state of this EntityObject
        ///  
        /// 
        /// This property returns a value from the EntityState enum. 
        ///  
        [System.ComponentModel.Browsable(false)]
        [System.Xml.Serialization.XmlIgnore] 
        public EntityState EntityState
        {
            get
            { 
                Debug.Assert(EntityChangeTracker != null,
                    "EntityChangeTracker should never return null -- if detached should be set to s_detachedEntityChangeTracker"); 
                Debug.Assert(EntityChangeTracker != s_detachedEntityChangeTracker ? EntityChangeTracker.EntityState != EntityState.Detached : true, 
                    "Should never get a detached state from an attached change tracker.");
 
                return EntityChangeTracker.EntityState;
            }
        }
 
        #region IEntityWithKey
 
        ///  
        /// Returns the EntityKey for this EntityObject.
        ///  
        [Browsable(false)]
        [DataMember]
        public EntityKey EntityKey
        { 
            get
            { 
                return _entityKey; 
            }
            set 
            {
                // Report the change to the change tracker
                // If we are not attached to a change tracker, we can do anything we want to the key
                // If we are attached, the change tracker should make sure the new value is valid for the current state 
                Debug.Assert(EntityChangeTracker != null, "_entityChangeTracker should never be null -- if detached it should return s_detachedEntityChangeTracker");
                EntityChangeTracker.EntityMemberChanging(StructuralObject.EntityKeyPropertyName); 
                _entityKey = value; 
                EntityChangeTracker.EntityMemberChanged(StructuralObject.EntityKeyPropertyName);
            } 
        }

        #endregion
        #region IEntityWithChangeTracker 

 
        ///  
        /// Used by the ObjectStateManager to attach or detach this EntityObject to the cache.
        ///  
        /// 
        /// Reference to the ObjectStateEntry that contains this entity
        /// 
        void IEntityWithChangeTracker.SetChangeTracker(IEntityChangeTracker changeTracker) 
        {
            // Fail if the change tracker is already set for this EntityObject and it's being set to something different 
            if (changeTracker != null && EntityChangeTracker != s_detachedEntityChangeTracker && !Object.ReferenceEquals(changeTracker, EntityChangeTracker)) 
            {
                throw EntityUtil.EntityCantHaveMultipleChangeTrackers(); 
            }

            EntityChangeTracker = changeTracker;
        } 

        #endregion IEntityWithChangeTracker 
        #region IEntityWithRelationships 

        ///  
        /// Returns the container for the lazily created relationship
        /// navigation property objects, collections and refs.
        /// 
        RelationshipManager IEntityWithRelationships.RelationshipManager 
        {
            get 
            { 
                if (_relationships == null)
                { 
                    _relationships = RelationshipManager.Create(this);
                }

                return _relationships; 
            }
        } 
 
        #endregion
        #endregion 
        #region Protected Change Tracking Methods

        /// 
        /// This method is called whenever a change is going to be made to an EntityObject 
        /// property.
        ///  
        ///  
        /// The name of the changing property.
        ///  
        /// 
        /// When parameter member is null (Nothing in Visual Basic).
        /// 
        protected sealed override void ReportPropertyChanging( 
            string property)
        { 
            EntityUtil.CheckStringArgument(property, "property"); 

            Debug.Assert(EntityChangeTracker != null, "_entityChangeTracker should never be null -- if detached it should return s_detachedEntityChangeTracker"); 

            base.ReportPropertyChanging(property);

            EntityChangeTracker.EntityMemberChanging(property); 
        }
 
        ///  
        /// This method is called whenever a change is made to an EntityObject
        /// property. 
        /// 
        /// 
        /// The name of the changed property.
        ///  
        /// 
        /// When parameter member is null (Nothing in Visual Basic). 
        ///  
        protected sealed override void ReportPropertyChanged(
            string property) 
        {
            EntityUtil.CheckStringArgument(property, "property");

            Debug.Assert(EntityChangeTracker != null, "EntityChangeTracker should never return null -- if detached it should be return s_detachedEntityChangeTracker"); 
            EntityChangeTracker.EntityMemberChanged(property);
 
            base.ReportPropertyChanged(property); 
        }
 
        #endregion
        #region Internal ComplexObject Change Tracking Methods and Properties

        internal sealed override bool IsChangeTracked 
        {
            get 
            { 
                return EntityState != EntityState.Detached;
            } 
        }

        /// 
        /// This method is called by a ComplexObject contained in this Entity 
        /// whenever a change is about to be made to a property of the
        /// ComplexObject so that the change can be forwarded to the change tracker. 
        ///  
        /// 
        /// The name of the top-level entity property that contains the ComplexObject that is calling this method. 
        /// 
        /// 
        /// The instance of the ComplexObject on which the property is changing.
        ///  
        /// 
        /// The name of the changing property on complexObject. 
        ///  
        internal sealed override void ReportComplexPropertyChanging(
            string entityMemberName, ComplexObject complexObject, string complexMemberName) 
        {
            Debug.Assert(complexObject != null, "invalid complexObject");
            Debug.Assert(!String.IsNullOrEmpty(complexMemberName), "invalid complexMemberName");
 
            EntityChangeTracker.EntityComplexMemberChanging(entityMemberName, complexObject, complexMemberName);
        } 
 
        /// 
        /// This method is called by a ComplexObject contained in this Entity 
        /// whenever a change has been made to a property of the
        /// ComplexObject so that the change can be forwarded to the change tracker.
        /// 
        ///  
        /// The name of the top-level entity property that contains the ComplexObject that is calling this method.
        ///  
        ///  
        /// The instance of the ComplexObject on which the property is changing.
        ///  
        /// 
        /// The name of the changing property on complexObject.
        /// 
        internal sealed override void ReportComplexPropertyChanged( 
            string entityMemberName, ComplexObject complexObject, string complexMemberName)
        { 
            Debug.Assert(complexObject != null, "invalid complexObject"); 
            Debug.Assert(!String.IsNullOrEmpty(complexMemberName), "invalid complexMemberName");
 
            EntityChangeTracker.EntityComplexMemberChanged(entityMemberName, complexObject, complexMemberName);
        }

        #endregion 
    }
} 
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner       [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 
using System.Data;
using System.Diagnostics; 
using System.Reflection;
using System.ComponentModel;
using System.Runtime.Serialization;
 
namespace System.Data.Objects.DataClasses
{ 
    ///  
    /// This is the class is the basis for all perscribed EntityObject classes.
    ///  
    [DataContract(IsReference=true)]
    [Serializable]
    public abstract class EntityObject : StructuralObject, IEntityWithKey, IEntityWithChangeTracker, IEntityWithRelationships
    { 
        #region Privates
 
        // The following 2 fields are serialized.  Adding or removing a serialized field is considered 
        // a breaking change.  This includes changing the field type or field name of existing
        // serialized fields. If you need to make this kind of change, it may be possible, but it 
        // will require some custom serialization/deserialization code.
        private RelationshipManager _relationships;
        private EntityKey _entityKey;
 
        [NonSerialized]
        private IEntityChangeTracker _entityChangeTracker = s_detachedEntityChangeTracker; 
        [NonSerialized] 
        private static readonly DetachedEntityChangeTracker s_detachedEntityChangeTracker = new DetachedEntityChangeTracker();
 
        /// 
        /// Helper class used when we are not currently attached to a change tracker.
        /// Simplifies the code so we don't always have to check for null before using the change tracker
        ///  
        private class DetachedEntityChangeTracker : IEntityChangeTracker
        { 
            void IEntityChangeTracker.EntityMemberChanging(string entityMemberName) { } 
            void IEntityChangeTracker.EntityMemberChanged(string entityMemberName) { }
            void IEntityChangeTracker.EntityComplexMemberChanging(string entityMemberName, object complexObject, string complexMemberName) { } 
            void IEntityChangeTracker.EntityComplexMemberChanged(string entityMemberName, object complexObject, string complexMemberName) { }
            EntityState IEntityChangeTracker.EntityState
            {
                get 
                {
                    return EntityState.Detached; 
                } 
            }
        } 

        private IEntityChangeTracker EntityChangeTracker
        {
            get 
            {
                if (_entityChangeTracker == null) 
                { 
                    _entityChangeTracker = s_detachedEntityChangeTracker;
                } 
                return _entityChangeTracker;
            }
            set
            { 
                _entityChangeTracker = value;
            } 
        } 

        #endregion 
        #region Publics
        /// 
        /// The storage state of this EntityObject
        ///  
        /// 
        /// This property returns a value from the EntityState enum. 
        ///  
        [System.ComponentModel.Browsable(false)]
        [System.Xml.Serialization.XmlIgnore] 
        public EntityState EntityState
        {
            get
            { 
                Debug.Assert(EntityChangeTracker != null,
                    "EntityChangeTracker should never return null -- if detached should be set to s_detachedEntityChangeTracker"); 
                Debug.Assert(EntityChangeTracker != s_detachedEntityChangeTracker ? EntityChangeTracker.EntityState != EntityState.Detached : true, 
                    "Should never get a detached state from an attached change tracker.");
 
                return EntityChangeTracker.EntityState;
            }
        }
 
        #region IEntityWithKey
 
        ///  
        /// Returns the EntityKey for this EntityObject.
        ///  
        [Browsable(false)]
        [DataMember]
        public EntityKey EntityKey
        { 
            get
            { 
                return _entityKey; 
            }
            set 
            {
                // Report the change to the change tracker
                // If we are not attached to a change tracker, we can do anything we want to the key
                // If we are attached, the change tracker should make sure the new value is valid for the current state 
                Debug.Assert(EntityChangeTracker != null, "_entityChangeTracker should never be null -- if detached it should return s_detachedEntityChangeTracker");
                EntityChangeTracker.EntityMemberChanging(StructuralObject.EntityKeyPropertyName); 
                _entityKey = value; 
                EntityChangeTracker.EntityMemberChanged(StructuralObject.EntityKeyPropertyName);
            } 
        }

        #endregion
        #region IEntityWithChangeTracker 

 
        ///  
        /// Used by the ObjectStateManager to attach or detach this EntityObject to the cache.
        ///  
        /// 
        /// Reference to the ObjectStateEntry that contains this entity
        /// 
        void IEntityWithChangeTracker.SetChangeTracker(IEntityChangeTracker changeTracker) 
        {
            // Fail if the change tracker is already set for this EntityObject and it's being set to something different 
            if (changeTracker != null && EntityChangeTracker != s_detachedEntityChangeTracker && !Object.ReferenceEquals(changeTracker, EntityChangeTracker)) 
            {
                throw EntityUtil.EntityCantHaveMultipleChangeTrackers(); 
            }

            EntityChangeTracker = changeTracker;
        } 

        #endregion IEntityWithChangeTracker 
        #region IEntityWithRelationships 

        ///  
        /// Returns the container for the lazily created relationship
        /// navigation property objects, collections and refs.
        /// 
        RelationshipManager IEntityWithRelationships.RelationshipManager 
        {
            get 
            { 
                if (_relationships == null)
                { 
                    _relationships = RelationshipManager.Create(this);
                }

                return _relationships; 
            }
        } 
 
        #endregion
        #endregion 
        #region Protected Change Tracking Methods

        /// 
        /// This method is called whenever a change is going to be made to an EntityObject 
        /// property.
        ///  
        ///  
        /// The name of the changing property.
        ///  
        /// 
        /// When parameter member is null (Nothing in Visual Basic).
        /// 
        protected sealed override void ReportPropertyChanging( 
            string property)
        { 
            EntityUtil.CheckStringArgument(property, "property"); 

            Debug.Assert(EntityChangeTracker != null, "_entityChangeTracker should never be null -- if detached it should return s_detachedEntityChangeTracker"); 

            base.ReportPropertyChanging(property);

            EntityChangeTracker.EntityMemberChanging(property); 
        }
 
        ///  
        /// This method is called whenever a change is made to an EntityObject
        /// property. 
        /// 
        /// 
        /// The name of the changed property.
        ///  
        /// 
        /// When parameter member is null (Nothing in Visual Basic). 
        ///  
        protected sealed override void ReportPropertyChanged(
            string property) 
        {
            EntityUtil.CheckStringArgument(property, "property");

            Debug.Assert(EntityChangeTracker != null, "EntityChangeTracker should never return null -- if detached it should be return s_detachedEntityChangeTracker"); 
            EntityChangeTracker.EntityMemberChanged(property);
 
            base.ReportPropertyChanged(property); 
        }
 
        #endregion
        #region Internal ComplexObject Change Tracking Methods and Properties

        internal sealed override bool IsChangeTracked 
        {
            get 
            { 
                return EntityState != EntityState.Detached;
            } 
        }

        /// 
        /// This method is called by a ComplexObject contained in this Entity 
        /// whenever a change is about to be made to a property of the
        /// ComplexObject so that the change can be forwarded to the change tracker. 
        ///  
        /// 
        /// The name of the top-level entity property that contains the ComplexObject that is calling this method. 
        /// 
        /// 
        /// The instance of the ComplexObject on which the property is changing.
        ///  
        /// 
        /// The name of the changing property on complexObject. 
        ///  
        internal sealed override void ReportComplexPropertyChanging(
            string entityMemberName, ComplexObject complexObject, string complexMemberName) 
        {
            Debug.Assert(complexObject != null, "invalid complexObject");
            Debug.Assert(!String.IsNullOrEmpty(complexMemberName), "invalid complexMemberName");
 
            EntityChangeTracker.EntityComplexMemberChanging(entityMemberName, complexObject, complexMemberName);
        } 
 
        /// 
        /// This method is called by a ComplexObject contained in this Entity 
        /// whenever a change has been made to a property of the
        /// ComplexObject so that the change can be forwarded to the change tracker.
        /// 
        ///  
        /// The name of the top-level entity property that contains the ComplexObject that is calling this method.
        ///  
        ///  
        /// The instance of the ComplexObject on which the property is changing.
        ///  
        /// 
        /// The name of the changing property on complexObject.
        /// 
        internal sealed override void ReportComplexPropertyChanged( 
            string entityMemberName, ComplexObject complexObject, string complexMemberName)
        { 
            Debug.Assert(complexObject != null, "invalid complexObject"); 
            Debug.Assert(!String.IsNullOrEmpty(complexMemberName), "invalid complexMemberName");
 
            EntityChangeTracker.EntityComplexMemberChanged(entityMemberName, complexObject, complexMemberName);
        }

        #endregion 
    }
} 
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK