Propagator.ExtentPlaceholderCreator.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataEntity / System / Data / Map / Update / Internal / Propagator.ExtentPlaceholderCreator.cs / 3 / Propagator.ExtentPlaceholderCreator.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

using System.Collections.Generic; 
using System.Diagnostics;
using System.Data.Common;
using System.Data.Common.CommandTrees;
using System.Data.Common.Utils; 
using System.Data.Metadata.Edm;
namespace System.Data.Mapping.Update.Internal 
{ 
    internal partial class Propagator
    { 
        /// 
        /// Class generating default records for extents. Has a single external entry point, the
        ///  static method.
        ///  
        private class ExtentPlaceholderCreator
        { 
            #region Constructors 
            /// 
            /// Constructs a new placeholder creator. 
            /// 
            /// Context used to generate all elements of the placeholder.
            private ExtentPlaceholderCreator(UpdateTranslator parent)
            { 
                EntityUtil.CheckArgumentNull(parent, "parent");
                m_parent = parent; 
            } 
            #endregion
 
            #region Fields
            static private Dictionary s_typeDefaultMap = InitializeTypeDefaultMap();
            private UpdateTranslator m_parent;
            #endregion 

            #region Methods 
            ///  
            /// Initializes a map from primitive scalar types in the C-Space to default values
            /// used within the placeholder. 
            /// 
            private static Dictionary InitializeTypeDefaultMap()
            {
                Dictionary typeDefaultMap = new Dictionary( 
                    EqualityComparer.Default);
 
                // Use CLR defaults for value types, arbitrary constants for reference types 
                // (since these default to null)
                typeDefaultMap[PrimitiveTypeKind.Binary] = new Byte[0]; 
                typeDefaultMap[PrimitiveTypeKind.Boolean] = default(Boolean);
                typeDefaultMap[PrimitiveTypeKind.Byte] = default(Byte);
                typeDefaultMap[PrimitiveTypeKind.DateTime] = default(DateTime);
                typeDefaultMap[PrimitiveTypeKind.Time] = default(TimeSpan); 
                typeDefaultMap[PrimitiveTypeKind.DateTimeOffset] = default(DateTimeOffset);
                typeDefaultMap[PrimitiveTypeKind.Decimal] = default(Decimal); 
                typeDefaultMap[PrimitiveTypeKind.Double] = default(Double); 
                typeDefaultMap[PrimitiveTypeKind.Guid] = default(Guid);
                typeDefaultMap[PrimitiveTypeKind.Int16] = default(Int16); 
                typeDefaultMap[PrimitiveTypeKind.Int32] = default(Int32);
                typeDefaultMap[PrimitiveTypeKind.Int64] = default(Int64);
                typeDefaultMap[PrimitiveTypeKind.Single] = default(Single);
                typeDefaultMap[PrimitiveTypeKind.SByte] = default(SByte); 
                typeDefaultMap[PrimitiveTypeKind.String] = String.Empty;
 
#if DEBUG 
                foreach (object o in typeDefaultMap.Values)
                { 
                    Debug.Assert(null != o, "DbConstantExpression instances do not support null values");
                }
#endif
 
                return typeDefaultMap;
            } 
 
            /// 
            /// Creates a record for an extent containing default values. Assumes the extent is either 
            /// a relationship set or an entity set.
            /// 
            /// 
            /// Each scalar value appearing in the record is a . A placeholder is created by recursively 
            /// building a record, so an entity record type will return a new record ()
            /// consisting of some recursively built record for each column in the type. 
            ///  
            /// Extent
            /// Command tree used to generate portions of the record 
            /// A default record for the 
            internal static PropagatorResult CreatePlaceholder(EntitySetBase extent, UpdateTranslator parent)
            {
                EntityUtil.CheckArgumentNull(extent, "extent"); 

                ExtentPlaceholderCreator creator = new ExtentPlaceholderCreator(parent); 
 
                AssociationSet associationSet = extent as AssociationSet;
                if (null != associationSet) 
                {
                    return creator.CreateAssociationSetPlaceholder(associationSet);
                }
 
                EntitySet entitySet = extent as EntitySet;
                if (null != entitySet) 
                { 
                    return creator.CreateEntitySetPlaceholder(entitySet);
                } 

                throw EntityUtil.NotSupported(System.Data.Entity.Strings.Update_UnsupportedExtentType(
                    extent.Name, extent.GetType().Name));
            } 

            ///  
            /// Specialization of  for an entity set extent. 
            /// 
            ///  
            /// 
            private PropagatorResult CreateEntitySetPlaceholder(EntitySet entitySet)
            {
                EntityUtil.CheckArgumentNull(entitySet, "entitySet"); 
                ReadOnlyMetadataCollection members = entitySet.ElementType.Properties;
                PropagatorResult[] memberValues = new PropagatorResult[members.Count]; 
 
                for (int ordinal = 0; ordinal < members.Count; ordinal++)
                { 
                    PropagatorResult memberValue = CreateMemberPlaceholder(members[ordinal]);
                    memberValues[ordinal] = memberValue;
                }
 
                PropagatorResult result = PropagatorResult.CreateStructuralValue(memberValues, entitySet.ElementType, false);
 
                return result; 
            }
 
            /// 
            /// Specialization of  for a relationship set extent.
            /// 
            ///  
            /// 
            private PropagatorResult CreateAssociationSetPlaceholder(AssociationSet associationSet) 
            { 
                Debug.Assert(null != associationSet, "Caller must verify parameters are not null");
 
                var endMetadata = associationSet.ElementType.AssociationEndMembers;
                PropagatorResult[] endReferenceValues = new PropagatorResult[endMetadata.Count];

                // Create a reference expression for each end in the relationship 
                for (int endOrdinal = 0; endOrdinal < endMetadata.Count; endOrdinal++)
                { 
                    var end = endMetadata[endOrdinal]; 
                    EntityType entityType = (EntityType)((RefType)end.TypeUsage.EdmType).ElementType;
 
                    // Retrieve key values for this end
                    PropagatorResult[] keyValues = new PropagatorResult[entityType.KeyMembers.Count];
                    for (int memberOrdinal = 0; memberOrdinal < entityType.KeyMembers.Count; memberOrdinal++)
                    { 
                        EdmMember keyMember = entityType.KeyMembers[memberOrdinal];
                        PropagatorResult keyValue = CreateMemberPlaceholder(keyMember); 
                        keyValues[memberOrdinal] = keyValue; 
                    }
 
                    RowType endType = entityType.GetKeyRowType(m_parent.MetadataWorkspace);
                    PropagatorResult refKeys = PropagatorResult.CreateStructuralValue(keyValues, endType, false);

                    endReferenceValues[endOrdinal] = refKeys; 
                }
 
                PropagatorResult result = PropagatorResult.CreateStructuralValue(endReferenceValues, associationSet.ElementType, false); 
                return result;
            } 

            /// 
            /// Returns a placeholder for a specific metadata member.
            ///  
            /// EdmMember for which to produce a placeholder.
            /// Placeholder element for the given member. 
            private PropagatorResult CreateMemberPlaceholder(EdmMember member) 
            {
                EntityUtil.CheckArgumentNull(member, "member"); 

                return Visit(member);
            }
 
            #region Visitor implementation
            ///  
            /// Given default values for children members, produces a new default expression for the requested (parent) member. 
            /// 
            /// Parent member 
            /// Default value for parent member
            internal PropagatorResult Visit(EdmMember node)
            {
                PropagatorResult result; 
                TypeUsage nodeType = Helper.GetModelTypeUsage(node);
 
                if (BuiltInTypeKind.PrimitiveType == nodeType.EdmType.BuiltInTypeKind) 
                {
                    object value; 

                    // Find "sanctioned" default value
                    PrimitiveTypeKind primitiveTypeKind = MetadataHelper.GetPrimitiveTypeKind(nodeType);
                    if (!s_typeDefaultMap.TryGetValue(primitiveTypeKind, out value)) 
                    {
                        // If none exists, default to lowest common denominator for constants 
                        value = default(byte); 
                    }
 
                    // Return a new constant expression flagged as unknown since the value is only there for
                    // show. (Not entirely for show, because null constraints may require a value for a record,
                    // whether that record is a placeholder or not).
                    result = PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value); 
                }
                else 
                { 
                    // Construct a new 'complex type' (really any structural type) member.
                    StructuralType structuralType = (StructuralType)nodeType.EdmType; 
                    IBaseList members = TypeHelpers.GetAllStructuralMembers(structuralType);

                    PropagatorResult[] args = new PropagatorResult[members.Count];
                    for (int ordinal = 0; ordinal < members.Count; ordinal++) 
//                    foreach (EdmMember member in members)
                    { 
                        args[ordinal] = Visit(members[ordinal]); 
                    }
 
                    result = PropagatorResult.CreateStructuralValue(args, structuralType, false);
                }

                return result; 
            }
 
            #endregion 
            #endregion
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

using System.Collections.Generic; 
using System.Diagnostics;
using System.Data.Common;
using System.Data.Common.CommandTrees;
using System.Data.Common.Utils; 
using System.Data.Metadata.Edm;
namespace System.Data.Mapping.Update.Internal 
{ 
    internal partial class Propagator
    { 
        /// 
        /// Class generating default records for extents. Has a single external entry point, the
        ///  static method.
        ///  
        private class ExtentPlaceholderCreator
        { 
            #region Constructors 
            /// 
            /// Constructs a new placeholder creator. 
            /// 
            /// Context used to generate all elements of the placeholder.
            private ExtentPlaceholderCreator(UpdateTranslator parent)
            { 
                EntityUtil.CheckArgumentNull(parent, "parent");
                m_parent = parent; 
            } 
            #endregion
 
            #region Fields
            static private Dictionary s_typeDefaultMap = InitializeTypeDefaultMap();
            private UpdateTranslator m_parent;
            #endregion 

            #region Methods 
            ///  
            /// Initializes a map from primitive scalar types in the C-Space to default values
            /// used within the placeholder. 
            /// 
            private static Dictionary InitializeTypeDefaultMap()
            {
                Dictionary typeDefaultMap = new Dictionary( 
                    EqualityComparer.Default);
 
                // Use CLR defaults for value types, arbitrary constants for reference types 
                // (since these default to null)
                typeDefaultMap[PrimitiveTypeKind.Binary] = new Byte[0]; 
                typeDefaultMap[PrimitiveTypeKind.Boolean] = default(Boolean);
                typeDefaultMap[PrimitiveTypeKind.Byte] = default(Byte);
                typeDefaultMap[PrimitiveTypeKind.DateTime] = default(DateTime);
                typeDefaultMap[PrimitiveTypeKind.Time] = default(TimeSpan); 
                typeDefaultMap[PrimitiveTypeKind.DateTimeOffset] = default(DateTimeOffset);
                typeDefaultMap[PrimitiveTypeKind.Decimal] = default(Decimal); 
                typeDefaultMap[PrimitiveTypeKind.Double] = default(Double); 
                typeDefaultMap[PrimitiveTypeKind.Guid] = default(Guid);
                typeDefaultMap[PrimitiveTypeKind.Int16] = default(Int16); 
                typeDefaultMap[PrimitiveTypeKind.Int32] = default(Int32);
                typeDefaultMap[PrimitiveTypeKind.Int64] = default(Int64);
                typeDefaultMap[PrimitiveTypeKind.Single] = default(Single);
                typeDefaultMap[PrimitiveTypeKind.SByte] = default(SByte); 
                typeDefaultMap[PrimitiveTypeKind.String] = String.Empty;
 
#if DEBUG 
                foreach (object o in typeDefaultMap.Values)
                { 
                    Debug.Assert(null != o, "DbConstantExpression instances do not support null values");
                }
#endif
 
                return typeDefaultMap;
            } 
 
            /// 
            /// Creates a record for an extent containing default values. Assumes the extent is either 
            /// a relationship set or an entity set.
            /// 
            /// 
            /// Each scalar value appearing in the record is a . A placeholder is created by recursively 
            /// building a record, so an entity record type will return a new record ()
            /// consisting of some recursively built record for each column in the type. 
            ///  
            /// Extent
            /// Command tree used to generate portions of the record 
            /// A default record for the 
            internal static PropagatorResult CreatePlaceholder(EntitySetBase extent, UpdateTranslator parent)
            {
                EntityUtil.CheckArgumentNull(extent, "extent"); 

                ExtentPlaceholderCreator creator = new ExtentPlaceholderCreator(parent); 
 
                AssociationSet associationSet = extent as AssociationSet;
                if (null != associationSet) 
                {
                    return creator.CreateAssociationSetPlaceholder(associationSet);
                }
 
                EntitySet entitySet = extent as EntitySet;
                if (null != entitySet) 
                { 
                    return creator.CreateEntitySetPlaceholder(entitySet);
                } 

                throw EntityUtil.NotSupported(System.Data.Entity.Strings.Update_UnsupportedExtentType(
                    extent.Name, extent.GetType().Name));
            } 

            ///  
            /// Specialization of  for an entity set extent. 
            /// 
            ///  
            /// 
            private PropagatorResult CreateEntitySetPlaceholder(EntitySet entitySet)
            {
                EntityUtil.CheckArgumentNull(entitySet, "entitySet"); 
                ReadOnlyMetadataCollection members = entitySet.ElementType.Properties;
                PropagatorResult[] memberValues = new PropagatorResult[members.Count]; 
 
                for (int ordinal = 0; ordinal < members.Count; ordinal++)
                { 
                    PropagatorResult memberValue = CreateMemberPlaceholder(members[ordinal]);
                    memberValues[ordinal] = memberValue;
                }
 
                PropagatorResult result = PropagatorResult.CreateStructuralValue(memberValues, entitySet.ElementType, false);
 
                return result; 
            }
 
            /// 
            /// Specialization of  for a relationship set extent.
            /// 
            ///  
            /// 
            private PropagatorResult CreateAssociationSetPlaceholder(AssociationSet associationSet) 
            { 
                Debug.Assert(null != associationSet, "Caller must verify parameters are not null");
 
                var endMetadata = associationSet.ElementType.AssociationEndMembers;
                PropagatorResult[] endReferenceValues = new PropagatorResult[endMetadata.Count];

                // Create a reference expression for each end in the relationship 
                for (int endOrdinal = 0; endOrdinal < endMetadata.Count; endOrdinal++)
                { 
                    var end = endMetadata[endOrdinal]; 
                    EntityType entityType = (EntityType)((RefType)end.TypeUsage.EdmType).ElementType;
 
                    // Retrieve key values for this end
                    PropagatorResult[] keyValues = new PropagatorResult[entityType.KeyMembers.Count];
                    for (int memberOrdinal = 0; memberOrdinal < entityType.KeyMembers.Count; memberOrdinal++)
                    { 
                        EdmMember keyMember = entityType.KeyMembers[memberOrdinal];
                        PropagatorResult keyValue = CreateMemberPlaceholder(keyMember); 
                        keyValues[memberOrdinal] = keyValue; 
                    }
 
                    RowType endType = entityType.GetKeyRowType(m_parent.MetadataWorkspace);
                    PropagatorResult refKeys = PropagatorResult.CreateStructuralValue(keyValues, endType, false);

                    endReferenceValues[endOrdinal] = refKeys; 
                }
 
                PropagatorResult result = PropagatorResult.CreateStructuralValue(endReferenceValues, associationSet.ElementType, false); 
                return result;
            } 

            /// 
            /// Returns a placeholder for a specific metadata member.
            ///  
            /// EdmMember for which to produce a placeholder.
            /// Placeholder element for the given member. 
            private PropagatorResult CreateMemberPlaceholder(EdmMember member) 
            {
                EntityUtil.CheckArgumentNull(member, "member"); 

                return Visit(member);
            }
 
            #region Visitor implementation
            ///  
            /// Given default values for children members, produces a new default expression for the requested (parent) member. 
            /// 
            /// Parent member 
            /// Default value for parent member
            internal PropagatorResult Visit(EdmMember node)
            {
                PropagatorResult result; 
                TypeUsage nodeType = Helper.GetModelTypeUsage(node);
 
                if (BuiltInTypeKind.PrimitiveType == nodeType.EdmType.BuiltInTypeKind) 
                {
                    object value; 

                    // Find "sanctioned" default value
                    PrimitiveTypeKind primitiveTypeKind = MetadataHelper.GetPrimitiveTypeKind(nodeType);
                    if (!s_typeDefaultMap.TryGetValue(primitiveTypeKind, out value)) 
                    {
                        // If none exists, default to lowest common denominator for constants 
                        value = default(byte); 
                    }
 
                    // Return a new constant expression flagged as unknown since the value is only there for
                    // show. (Not entirely for show, because null constraints may require a value for a record,
                    // whether that record is a placeholder or not).
                    result = PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value); 
                }
                else 
                { 
                    // Construct a new 'complex type' (really any structural type) member.
                    StructuralType structuralType = (StructuralType)nodeType.EdmType; 
                    IBaseList members = TypeHelpers.GetAllStructuralMembers(structuralType);

                    PropagatorResult[] args = new PropagatorResult[members.Count];
                    for (int ordinal = 0; ordinal < members.Count; ordinal++) 
//                    foreach (EdmMember member in members)
                    { 
                        args[ordinal] = Visit(members[ordinal]); 
                    }
 
                    result = PropagatorResult.CreateStructuralValue(args, structuralType, false);
                }

                return result; 
            }
 
            #endregion 
            #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