Parameter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / EntityModel / SchemaObjectModel / Parameter.cs / 1305376 / Parameter.cs

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

using Som = System.Data.EntityModel.SchemaObjectModel; 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized; 
using System.Diagnostics;
using System.Xml; 
using System.Data; 
using System.Data.Metadata.Edm;
using System.Reflection; 
using System.IO;
using System.Globalization;
using System.Text;
using System.Data.Entity; 

namespace System.Data.EntityModel.SchemaObjectModel 
{ 
    /// 
    /// Summary description for StructuredProperty. 
    /// 
    internal class Parameter : FacetEnabledSchemaElement
    {
        #region Instance Fields 
        private ParameterDirection _parameterDirection = ParameterDirection.Input;
        private CollectionKind _collectionKind = CollectionKind.None; 
        private ModelFunctionTypeElement _typeSubElement = null; 
        private bool _isRefType = false;
        #endregion 

        #region constructor
        /// 
        /// 
        /// 
        ///  
        internal Parameter(Function parentElement) 
            : base(parentElement)
        { 
            _typeUsageBuilder = new TypeUsageBuilder(this);
        }

        #endregion 

        #region Public Properties 
 
        internal ParameterDirection ParameterDirection
        { 
            get
            {
                return _parameterDirection;
            } 
        }
 
        internal CollectionKind CollectionKind 
        {
            get 
            {
                return _collectionKind;
            }
            set 
            {
                _collectionKind = value; 
            } 
        }
 
        internal bool IsRefType
        {
            get { return _isRefType; }
        } 

        internal override TypeUsage TypeUsage 
        { 
            get
            { 
                if (_typeSubElement != null)
                {
                    return _typeSubElement.GetTypeUsage();
                } 
                else if (base.TypeUsage == null)
                { 
                    return null; 
                }
                else if (CollectionKind != CollectionKind.None) 
                {
                    return TypeUsage.Create(new CollectionType(base.TypeUsage));
                }
                else 
                {
                    return base.TypeUsage; 
                } 
            }
        } 

        #endregion

        new internal SchemaType Type 
        {
            get 
            { 
                return _type;
            } 
        }


        internal void WriteIdentity(StringBuilder builder) 
        {
            builder.Append("Parameter("); 
            if (UnresolvedType != null && !UnresolvedType.Trim().Equals(String.Empty)) 
            {
                if (_collectionKind != CollectionKind.None) 
                {
                    builder.Append("Collection(" + UnresolvedType + ")");
                }
                else if (_isRefType) 
                {
                    builder.Append("Ref(" + UnresolvedType + ")"); 
                } 
                else
                { 
                    builder.Append(UnresolvedType);
                }
            }
            else if (_typeSubElement!=null) 
            {
                _typeSubElement.WriteIdentity(builder); 
            } 
            builder.Append(")");
        } 

        internal override SchemaElement Clone(SchemaElement parentElement)
        {
            Parameter parameter = new Parameter((Function)parentElement); 
            parameter._collectionKind = _collectionKind;
            parameter._parameterDirection = _parameterDirection; 
            parameter._type = _type; 
            parameter.Name = this.Name;
            parameter._typeUsageBuilder = this._typeUsageBuilder; 
            return parameter;
        }

        internal bool ResolveNestedTypeNames(Converter.ConversionCache convertedItemCache, Dictionary newGlobalItems) 
        {
            if (_typeSubElement == null) 
            { 
                return false;
            } 
            return _typeSubElement.ResolveNameAndSetTypeUsage(convertedItemCache, newGlobalItems);
        }

 
        protected override bool HandleAttribute(XmlReader reader)
        { 
            if (base.HandleAttribute(reader)) 
            {
                return true; 
            }
            else if (CanHandleAttribute(reader, XmlConstants.TypeElement))
            {
                HandleTypeAttribute(reader); 
                return true;
            } 
            else if (CanHandleAttribute(reader, XmlConstants.Mode)) 
            {
                HandleModeAttribute(reader); 
                return true;
            }
            else if (_typeUsageBuilder.HandleAttribute(reader))
            { 
                return true;
            } 
 
            return false;
        } 

        #region Private Methods

        private void HandleTypeAttribute(XmlReader reader) 
        {
            Debug.Assert(reader != null); 
            Debug.Assert(UnresolvedType == null); 

            string type; 
            if (!Utils.GetString(Schema, reader, out type))
                return;

            TypeModifier typeModifier; 

            Function.RemoveTypeModifier(ref type, out typeModifier, out _isRefType); 
 
            switch (typeModifier)
            { 
                case TypeModifier.Array:
                    CollectionKind = CollectionKind.Bag;
                    break;
                default: 
                    Debug.Assert(typeModifier == TypeModifier.None, string.Format(CultureInfo.CurrentCulture, "Type is not valid for property {0}: {1}. The modifier for the type cannot be used in this context.", FQName, reader.Value));
                    break; 
            } 

            if (!Utils.ValidateDottedName(Schema, reader, type)) 
                return;

            UnresolvedType = type;
        } 

        private void HandleModeAttribute(XmlReader reader) 
        { 
            Debug.Assert(reader != null);
 
            string value = reader.Value;

            if (String.IsNullOrEmpty(value))
            { 
                return;
            } 
 
            value = value.Trim();
 
            if (!String.IsNullOrEmpty(value))
            {
                switch (value)
                { 
                    case XmlConstants.In:
                        _parameterDirection = ParameterDirection.Input; 
                        break; 
                    case XmlConstants.Out:
                        _parameterDirection = ParameterDirection.Output; 
                        break;
                    case XmlConstants.InOut:
                        _parameterDirection = ParameterDirection.InputOutput;
                        break; 
                    default:
                        { 
                            // don't try to identify the parameter by any of the attributes 
                            // because we are still parsing attributes, and we don't know which ones
                            // have been parsed yet. 
                            int index = ParentElement.Parameters.Count;
                            AddError(ErrorCode.BadParameterDirection, EdmSchemaErrorSeverity.Error, reader,
                                System.Data.Entity.Strings.BadParameterDirection(
                                              value, 
                                              index,
                                              this.ParentElement.Name, 
                                              this.ParentElement.ParentElement.FQName)); 
                        }
                        break; 
                }
            }
        }
 
        protected override bool HandleElement(XmlReader reader)
        { 
            if (base.HandleElement(reader)) 
            {
                return true; 
            }
            else if (CanHandleElement(reader, XmlConstants.CollectionType))
            {
                HandleCollectionTypeElement(reader); 
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.ReferenceType)) 
            {
                HandleReferenceTypeElement(reader); 
                return true;
            }
            else if (CanHandleElement(reader, XmlConstants.TypeRef))
            { 
                HandleTypeRefElement(reader);
                return true; 
            } 
            else if (CanHandleElement(reader, XmlConstants.RowType))
            { 
                HandleRowTypeElement(reader);
                return true;
            }
 
            return false;
        } 
 

        protected void HandleCollectionTypeElement(XmlReader reader) 
        {
            Debug.Assert(reader != null);

            var subElement = new CollectionTypeElement(this); 
            subElement.Parse(reader);
            _typeSubElement = subElement; 
        } 

        protected void HandleReferenceTypeElement(XmlReader reader) 
        {
            Debug.Assert(reader != null);

            var subElement = new ReferenceTypeElement(this); 
            subElement.Parse(reader);
            _typeSubElement = subElement; 
        } 

        protected void HandleTypeRefElement(XmlReader reader) 
        {
            Debug.Assert(reader != null);

            var subElement = new TypeRefElement(this); 
            subElement.Parse(reader);
            _typeSubElement = subElement; 
        } 

        protected void HandleRowTypeElement(XmlReader reader) 
        {
            Debug.Assert(reader != null);

            var subElement = new RowTypeElement(this); 
            subElement.Parse(reader);
            _typeSubElement = subElement; 
        } 

        #endregion 


        internal override void ResolveTopLevelNames()
        { 
            if(_typeSubElement!=null)
            { 
                _typeSubElement.ResolveTopLevelNames(); 
            }
 
            // Can't be "else if" because element could have attribute AND sub-element,
            // in which case semantic validation won't work unless it has resolved both (so _type is not null)
            if (_unresolvedType != null)
            { 
                base.ResolveTopLevelNames();
            } 
        } 

 
        internal void ValidateForModelFunction()
        {

            if (_type != null && _type is ScalarType == false && _typeUsageBuilder.HasUserDefinedFacets) 
            {
                //Non-scalar parameter type should not have Facets 
                AddError(ErrorCode.ModelFuncionFacetOnNonScalarType, EdmSchemaErrorSeverity.Error, Strings.FacetsOnNonScalarType(_type.FQName)); 
            }
 
            if (_type == null && _typeUsageBuilder.HasUserDefinedFacets)
            {
                //Type attribute not specified but facets exist
                AddError(ErrorCode.ModelFunctionIncorrectlyPlacedFacet, EdmSchemaErrorSeverity.Error, Strings.FacetDeclarationRequiresTypeAttribute); 
            }
 
            if (_type == null && _typeSubElement == null) 
            {
                //Type not declared as either attribute or subelement 
                AddError(ErrorCode.ModelFunctionTypeNotDeclared, EdmSchemaErrorSeverity.Error, Strings.TypeMustBeDeclared);
            }

            if (_type != null && _typeSubElement != null) 
            {
                //Both attribute and sub-element declarations exist 
                AddError(ErrorCode.TypeDeclaredAsAttributeAndElement, EdmSchemaErrorSeverity.Error, Strings.TypeDeclaredAsAttributeAndElement); 
            }
 
            if (_type != null && _isRefType && !(_type is SchemaEntityType))
            {
                //Ref type refers to non entity type
                AddError(ErrorCode.ReferenceToNonEntityType, EdmSchemaErrorSeverity.Error, Strings.ReferenceToNonEntityType(_type.FQName)); 
            }
 
            if (_typeSubElement != null) 
            {
                _typeSubElement.Validate(); 
            }
        }
    }
} 

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