Exemplo n.º 1
0
        public void SetValues(object value)
        {
            IDictionary <string, Func <object, object> > propertyGetters = DbHelpers.GetPropertyGetters(value.GetType());

            foreach (string propertyName in (IEnumerable <string>) this.PropertyNames)
            {
                Func <object, object> func;
                if (propertyGetters.TryGetValue(propertyName, out func))
                {
                    object newValue = func(value);
                    IPropertyValuesItem propertyValuesItem = this.GetItem(propertyName);
                    if (newValue == null && propertyValuesItem.IsComplex)
                    {
                        throw Error.DbPropertyValues_ComplexObjectCannotBeNull((object)propertyName, (object)this._type.Name);
                    }
                    InternalPropertyValues internalPropertyValues = propertyValuesItem.Value as InternalPropertyValues;
                    if (internalPropertyValues == null)
                    {
                        this.SetValue(propertyValuesItem, newValue);
                    }
                    else
                    {
                        internalPropertyValues.SetValues(newValue);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public object this[string propertyName]
 {
     get
     {
         return(this.GetItem(propertyName).Value);
     }
     set
     {
         DbPropertyValues dbPropertyValues = value as DbPropertyValues;
         if (dbPropertyValues != null)
         {
             value = (object)dbPropertyValues.InternalPropertyValues;
         }
         IPropertyValuesItem    propertyValuesItem     = this.GetItem(propertyName);
         InternalPropertyValues internalPropertyValues = propertyValuesItem.Value as InternalPropertyValues;
         if (internalPropertyValues == null)
         {
             this.SetValue(propertyValuesItem, value);
         }
         else
         {
             InternalPropertyValues values = value as InternalPropertyValues;
             if (values == null)
             {
                 throw Error.DbPropertyValues_AttemptToSetNonValuesOnComplexProperty();
             }
             internalPropertyValues.SetValues(values);
         }
     }
 }
        /// <summary>
        ///     Initializes a new instance of the <see cref = "ClonedPropertyValues" /> class by copying
        ///     values from the given dictionary.
        /// </summary>
        /// <param name = "original">The dictionary to clone.</param>
        /// <param name = "valuesRecord">If non-null, then the values for the new dictionary are taken from this record rather than from the original dictionary.</param>
        internal ClonedPropertyValues(InternalPropertyValues original, DbDataRecord valuesRecord = null)
            : base(original.InternalContext, original.ObjectType, original.IsEntityValues)
        {
            _propertyNames = original.PropertyNames;
            _propertyValues = new Dictionary<string, ClonedPropertyValuesItem>(_propertyNames.Count);

            foreach (var propertyName in _propertyNames)
            {
                var item = original.GetItem(propertyName);

                var value = item.Value;
                var asValues = value as InternalPropertyValues;
                if (asValues != null)
                {
                    var nestedValuesRecord = valuesRecord == null ? null : (DbDataRecord)valuesRecord[propertyName];
                    value = new ClonedPropertyValues(asValues, nestedValuesRecord);
                }
                else if (valuesRecord != null)
                {
                    value = valuesRecord[propertyName];
                    if (value == DBNull.Value)
                    {
                        value = null;
                    }
                }

                _propertyValues[propertyName] = new ClonedPropertyValuesItem(
                    propertyName, value, item.Type, item.IsComplex);
            }
        }
        // <summary>
        // Initializes a new instance of the <see cref="ClonedPropertyValues" /> class by copying
        // values from the given dictionary.
        // </summary>
        // <param name="original"> The dictionary to clone. </param>
        // <param name="valuesRecord"> If non-null, then the values for the new dictionary are taken from this record rather than from the original dictionary. </param>
        internal ClonedPropertyValues(InternalPropertyValues original, DbDataRecord valuesRecord = null)
            : base(original.InternalContext, original.ObjectType, original.IsEntityValues)
        {
            _propertyNames  = original.PropertyNames;
            _propertyValues = new Dictionary <string, ClonedPropertyValuesItem>(_propertyNames.Count);

            foreach (var propertyName in _propertyNames)
            {
                var item = original.GetItem(propertyName);

                var value    = item.Value;
                var asValues = value as InternalPropertyValues;
                if (asValues != null)
                {
                    var nestedValuesRecord = valuesRecord == null ? null : (DbDataRecord)valuesRecord[propertyName];
                    value = new ClonedPropertyValues(asValues, nestedValuesRecord);
                }
                else if (valuesRecord != null)
                {
                    value = valuesRecord[propertyName];
                    if (value == DBNull.Value)
                    {
                        value = null;
                    }
                }

                _propertyValues[propertyName] = new ClonedPropertyValuesItem(
                    propertyName, value, item.Type, item.IsComplex);
            }
        }
Exemplo n.º 5
0
 internal ClonedPropertyValues(InternalPropertyValues original, DbDataRecord valuesRecord = null)
     : base(original.InternalContext, original.ObjectType, original.IsEntityValues)
 {
     this._propertyNames  = original.PropertyNames;
     this._propertyValues = (IDictionary <string, ClonedPropertyValuesItem>) new Dictionary <string, ClonedPropertyValuesItem>(this._propertyNames.Count);
     foreach (string propertyName in (IEnumerable <string>) this._propertyNames)
     {
         IPropertyValuesItem propertyValuesItem = original.GetItem(propertyName);
         object obj = propertyValuesItem.Value;
         InternalPropertyValues original1 = obj as InternalPropertyValues;
         if (original1 != null)
         {
             DbDataRecord valuesRecord1 = valuesRecord == null ? (DbDataRecord)null : (DbDataRecord)valuesRecord[propertyName];
             obj = (object)new ClonedPropertyValues(original1, valuesRecord1);
         }
         else if (valuesRecord != null)
         {
             obj = valuesRecord[propertyName];
             if (obj == DBNull.Value)
             {
                 obj = (object)null;
             }
         }
         this._propertyValues[propertyName] = new ClonedPropertyValuesItem(propertyName, obj, propertyValuesItem.Type, propertyValuesItem.IsComplex);
     }
 }
Exemplo n.º 6
0
 internal static Mock<InternalEntityEntryForMock<FakeWithProps>> CreateMockInternalEntityEntry(
     InternalPropertyValues currentValues = null,
     InternalPropertyValues originalValues = null)
 {
     currentValues = currentValues ?? CreateSimpleValues(10);
     var entity = (FakeWithProps)currentValues.ToObject();
     var mockInternalEntry = MockHelper.CreateMockInternalEntityEntry(
         entity, new EntityReference<FakeEntity>(), new EntityCollection<FakeEntity>(), isDetached: false);
     mockInternalEntry.Setup(e => e.ValidateAndGetPropertyMetadata("ValueTypeProp", It.IsAny<Type>(), It.IsAny<Type>())).Returns(
         ValueTypePropertyMetadata);
     mockInternalEntry.Setup(e => e.ValidateAndGetPropertyMetadata("RefTypeProp", It.IsAny<Type>(), It.IsAny<Type>())).Returns(
         RefTypePropertyMetadata);
     mockInternalEntry.Setup(e => e.ValidateAndGetPropertyMetadata("ComplexProp", It.IsAny<Type>(), It.IsAny<Type>())).Returns(
         ComplexPropertyMetadata);
     mockInternalEntry.Setup(e => e.ValidateAndGetPropertyMetadata("Reference", It.IsAny<Type>(), It.IsAny<Type>()));
     mockInternalEntry.Setup(e => e.ValidateAndGetPropertyMetadata("Collection", It.IsAny<Type>(), It.IsAny<Type>()));
     mockInternalEntry.Setup(e => e.ValidateAndGetPropertyMetadata("Missing", It.IsAny<Type>(), It.IsAny<Type>()));
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("ValueTypeProp"));
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("RefTypeProp"));
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("ComplexProp"));
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("Reference")).Returns(ReferenceMetadata);
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("Collection")).Returns(CollectionMetadata);
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("ValueTypeProp"));
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("RefTypeProp"));
     mockInternalEntry.Setup(e => e.GetNavigationMetadata("ComplexProp"));
     mockInternalEntry.SetupGet(e => e.CurrentValues).Returns(currentValues);
     mockInternalEntry.SetupGet(e => e.OriginalValues).Returns(originalValues ?? CreateSimpleValues(20));
     mockInternalEntry.CallBase = true;
     return mockInternalEntry;
 }
Exemplo n.º 7
0
 public void SetValues(InternalPropertyValues values)
 {
     if (!this._type.IsAssignableFrom(values.ObjectType))
     {
         throw Error.DbPropertyValues_AttemptToSetValuesFromWrongType((object)values.ObjectType.Name, (object)this._type.Name);
     }
     foreach (string propertyName in (IEnumerable <string>) this.PropertyNames)
     {
         IPropertyValuesItem propertyValuesItem = values.GetItem(propertyName);
         if (propertyValuesItem.Value == null && propertyValuesItem.IsComplex)
         {
             throw Error.DbPropertyValues_NestedPropertyValuesNull((object)propertyName, (object)this._type.Name);
         }
         this[propertyName] = propertyValuesItem.Value;
     }
 }
Exemplo n.º 8
0
        private void SetPropertyValueUsingValues(InternalPropertyValues internalValues, object value)
        {
            InternalPropertyValues internalValue = internalValues[this.Name] as InternalPropertyValues;

            if (internalValue != null)
            {
                if (!internalValue.ObjectType.IsAssignableFrom(value.GetType()))
                {
                    throw Error.DbPropertyValues_AttemptToSetValuesFromWrongObject((object)value.GetType().Name, (object)internalValue.ObjectType.Name);
                }
                internalValue.SetValues(value);
            }
            else
            {
                internalValues[this.Name] = value;
            }
        }
        // <summary>
        // Appends a query for the properties in the entity to the given string builder that is being used to
        // build the eSQL query.  This method may be called recursively to query for all the sub-properties of
        // a complex property.
        // </summary>
        // <param name="queryBuilder"> The query builder. </param>
        // <param name="prefix"> The qualifier with which to prefix each property name. </param>
        // <param name="templateValues"> The dictionary that acts as a template for the properties to query. </param>
        private void AppendEntitySqlRow(
            StringBuilder queryBuilder, string prefix, InternalPropertyValues templateValues)
        {
            var commaRequired = false;

            foreach (var propertyName in templateValues.PropertyNames)
            {
                if (commaRequired)
                {
                    queryBuilder.Append(", ");
                }
                else
                {
                    commaRequired = true;
                }

                var quotedName = DbHelpers.QuoteIdentifier(propertyName);

                var templateItem = templateValues.GetItem(propertyName);

                if (templateItem.IsComplex)
                {
                    var nestedValues = templateItem.Value as InternalPropertyValues;
                    if (nestedValues == null)
                    {
                        throw Error.DbPropertyValues_CannotGetStoreValuesWhenComplexPropertyIsNull(
                                  propertyName, EntityType.Name);
                    }

                    // Call the same method recursively to get all the values of the complex property
                    queryBuilder.Append("ROW(");
                    AppendEntitySqlRow(
                        queryBuilder, String.Format(CultureInfo.InvariantCulture, "{0}.{1}", prefix, quotedName),
                        nestedValues);
                    queryBuilder.AppendFormat(CultureInfo.InvariantCulture, ") AS {0}", quotedName);
                }
                else
                {
                    queryBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}.{1} ", prefix, quotedName);
                }
            }
        }
Exemplo n.º 10
0
        public object ToObject()
        {
            object obj1 = this.CreateObject();
            IDictionary <string, Action <object, object> > propertySetters = DbHelpers.GetPropertySetters(this._type);

            foreach (string propertyName in (IEnumerable <string>) this.PropertyNames)
            {
                object obj2 = this.GetItem(propertyName).Value;
                InternalPropertyValues internalPropertyValues = obj2 as InternalPropertyValues;
                if (internalPropertyValues != null)
                {
                    obj2 = internalPropertyValues.ToObject();
                }
                Action <object, object> action;
                if (propertySetters.TryGetValue(propertyName, out action))
                {
                    action(obj1, obj2);
                }
            }
            return(obj1);
        }
        /// <summary>
        ///     Sets the values of this dictionary by reading values from another dictionary.
        ///     The other dictionary must be based on the same type as this dictionary, or a type derived
        ///     from the type for this dictionary.
        /// </summary>
        /// <param name="values"> The dictionary to read values from. </param>
        public void SetValues(InternalPropertyValues values)
        {
            DebugCheck.NotNull(values);

            // Setting values from a derived type is allowed, but setting values from a base type is not.
            if (!_type.IsAssignableFrom(values.ObjectType))
            {
                throw Error.DbPropertyValues_AttemptToSetValuesFromWrongType(values.ObjectType.Name, _type.Name);
            }

            foreach (var propertyName in PropertyNames)
            {
                var item = values.GetItem(propertyName);

                if (item.Value == null &&
                    item.IsComplex)
                {
                    throw Error.DbPropertyValues_NestedPropertyValuesNull(propertyName, _type.Name);
                }

                this[propertyName] = item.Value;
            }
        }
        // <summary>
        // Sets the property value, potentially by setting individual nested values for a complex
        // property.
        // </summary>
        // <param name="value"> The value. </param>
        private void SetPropertyValueUsingValues(InternalPropertyValues internalValues, object value)
        {
            DebugCheck.NotNull(internalValues);

            var nestedValues = internalValues[Name] as InternalPropertyValues;

            if (nestedValues != null)
            {
                Debug.Assert(value != null, "Should already have thrown if complex object is null.");

                // Setting values from a derived type is allowed, but setting values from a base type is not.
                if (!nestedValues.ObjectType.IsAssignableFrom(value.GetType()))
                {
                    throw Error.DbPropertyValues_AttemptToSetValuesFromWrongObject(
                              value.GetType().Name, nestedValues.ObjectType.Name);
                }

                nestedValues.SetValues(value);
            }
            else
            {
                internalValues[Name] = value;
            }
        }
Exemplo n.º 13
0
        private void AppendEntitySqlRow(
            StringBuilder queryBuilder,
            string prefix,
            InternalPropertyValues templateValues)
        {
            bool flag = false;

            foreach (string propertyName in (IEnumerable <string>)templateValues.PropertyNames)
            {
                if (flag)
                {
                    queryBuilder.Append(", ");
                }
                else
                {
                    flag = true;
                }
                string str = DbHelpers.QuoteIdentifier(propertyName);
                IPropertyValuesItem propertyValuesItem = templateValues.GetItem(propertyName);
                if (propertyValuesItem.IsComplex)
                {
                    InternalPropertyValues templateValues1 = propertyValuesItem.Value as InternalPropertyValues;
                    if (templateValues1 == null)
                    {
                        throw Error.DbPropertyValues_CannotGetStoreValuesWhenComplexPropertyIsNull((object)propertyName, (object)this.EntityType.Name);
                    }
                    queryBuilder.Append("ROW(");
                    this.AppendEntitySqlRow(queryBuilder, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1}", (object)prefix, (object)str), templateValues1);
                    queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, ") AS {0}", (object)str);
                }
                else
                {
                    queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1} ", (object)prefix, (object)str);
                }
            }
        }
        /// <summary>
        ///     Appends a query for the properties in the entity to the given string builder that is being used to
        ///     build the eSQL query.  This method may be called recursively to query for all the sub-properties of
        ///     a complex property.
        /// </summary>
        /// <param name = "queryBuilder">The query builder.</param>
        /// <param name = "prefix">The qualifier with which to prefix each property name.</param>
        /// <param name = "templateValues">The dictionary that acts as a template for the properties to query.</param>
        private void AppendEntitySqlRow(
            StringBuilder queryBuilder, string prefix, InternalPropertyValues templateValues)
        {
            var commaRequired = false;
            foreach (var propertyName in templateValues.PropertyNames)
            {
                if (commaRequired)
                {
                    queryBuilder.Append(", ");
                }
                else
                {
                    commaRequired = true;
                }

                var quotedName = DbHelpers.QuoteIdentifier(propertyName);

                var templateItem = templateValues.GetItem(propertyName);

                if (templateItem.IsComplex)
                {
                    var nestedValues = templateItem.Value as InternalPropertyValues;
                    if (nestedValues == null)
                    {
                        throw Error.DbPropertyValues_CannotGetStoreValuesWhenComplexPropertyIsNull(
                            propertyName, EntityType.Name);
                    }

                    // Call the same method recursively to get all the values of the complex property
                    queryBuilder.Append("ROW(");
                    AppendEntitySqlRow(
                        queryBuilder, String.Format(CultureInfo.InvariantCulture, "{0}.{1}", prefix, quotedName),
                        nestedValues);
                    queryBuilder.AppendFormat(CultureInfo.InvariantCulture, ") AS {0}", quotedName);
                }
                else
                {
                    queryBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}.{1} ", prefix, quotedName);
                }
            }
        }
        // <summary>
        // Sets the values of this dictionary by reading values from another dictionary.
        // The other dictionary must be based on the same type as this dictionary, or a type derived
        // from the type for this dictionary.
        // </summary>
        // <param name="values"> The dictionary to read values from. </param>
        public void SetValues(InternalPropertyValues values)
        {
            DebugCheck.NotNull(values);

            // Setting values from a derived type is allowed, but setting values from a base type is not.
            if (!_type.IsAssignableFrom(values.ObjectType))
            {
                throw Error.DbPropertyValues_AttemptToSetValuesFromWrongType(values.ObjectType.Name, _type.Name);
            }

            foreach (var propertyName in PropertyNames)
            {
                var item = values.GetItem(propertyName);

                if (item.Value == null
                    && item.IsComplex)
                {
                    throw Error.DbPropertyValues_NestedPropertyValuesNull(propertyName, _type.Name);
                }

                this[propertyName] = item.Value;
            }
        }