Exemplo n.º 1
0
        public object Deserialize(object value, IDomainType domainType)
        {
            var complexObject = value as ODataComplexValue;

            if (complexObject == null)
            {
                throw new ArgumentException("value");
            }

            decimal number;

            if (!complexObject.TryGetPropertyValue(NumberPropertyName, out number))
            {
                throw new ArgumentException("value");
            }

            string unitRepresentation;

            if (!complexObject.TryGetPropertyValue(UnitPropertyName, out unitRepresentation))
            {
                throw new ArgumentException("value");
            }

            object unitValue;

            if (!domainType.TryParse(string.Concat(number, " ", unitRepresentation), CultureInfo.InvariantCulture, out unitValue))
            {
                throw new ArgumentException("value");
            }

            return(unitValue);
        }
Exemplo n.º 2
0
        public void MapsEnumerableOfDomainObjectsUsingInterfaces()
        {
            IEnumerable <IDomainType> domainObjects = new IDomainType[]
            {
                new DomainType1
                {
                    Prop0 = 10,
                    Prop1 = 11
                },
                new DomainType2
                {
                    Prop0 = 20,
                    Prop2 = 22
                }
            };

            var dtos = this.mapper.Map <IEnumerable <IDto> >(domainObjects).ToArray();

            dtos.Length.ShouldBe(domainObjects.Count());

            var dto1 = dtos[0].ShouldBeOfType <Dto1>();

            dto1.P0.ShouldBe(10);
            dto1.P1.ShouldBe(11);

            var dto2 = dtos[1].ShouldBeOfType <Dto2>();

            dto2.P0.ShouldBe(20);
            dto2.P2.ShouldBe(22);
        }
Exemplo n.º 3
0
        public object Serialize(object domainValue, IDomainType domainType)
        {
            var unitValue = domainValue as IUnitValue;

            if (unitValue != null)
            {
                var complexObject = this.oDataObjectFactory.CreateODataComplexValue(this.edmComplexType);
                complexObject.Properties = new[] {
                    new ODataProperty
                    {
                        Name  = NumberPropertyName,
                        Value = unitValue.InternalNumber
                    },
                    new ODataProperty
                    {
                        Name  = UnitPropertyName,
                        Value = unitValue.UnitRepresentation
                    }
                };

                return(complexObject);
            }

            if (domainValue != null)
            {
                return(null);                // unknown value, but it might be handled by instance annotations
            }

            throw new ArgumentException("domainValue");
        }
Exemplo n.º 4
0
        private bool ScalarProperty_ReadPropertiesFromAttributes(SerializationContext serializationContext,
                                                                 ScalarProperty scalarProperty, XmlReader reader)
        {
            // Type
            if (!serializationContext.Result.Failed)
            {
                string attribType = DONetEntityModelDesignerSerializationHelper.Instance.ReadAttribute(serializationContext, scalarProperty, reader, "type");
                if (!string.IsNullOrEmpty(attribType))
                {
                    string typeDisplayName = SystemPrimitiveTypesConverter.GetDisplayName(attribType);
                    if (!string.IsNullOrEmpty(typeDisplayName))
                    {
                        Type        clrType     = SystemPrimitiveTypesConverter.GetClrType(typeDisplayName);
                        Guid        typeId      = SystemPrimitiveTypesConverter.GetTypeId(clrType);
                        IModelRoot  entityModel = scalarProperty.Store.GetEntityModel();
                        IDomainType domainType  = entityModel.GetDomainType(typeId);
                        if (domainType == null)
                        {
                            domainType =
                                entityModel.BuildInDomainTypes.SingleOrDefault(type => type.FullName == "System.String");
                        }

                        scalarProperty.Type = (DomainType)domainType;

                        return(true);
                    }
                    else
                    {   // Invalid property value, ignored.
                        EntityModelDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "type", typeof(global::System.String), attribType);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        public object Serialize(object domainValue, IDomainType domainType)
        {
            var localizedValue = domainValue as ILocalizedTextValue;

            if (localizedValue != null)
            {
                // culture specific values
                var cultureValues = new List <ODataComplexValue>();
                foreach (var culture in localizedValue.ProvidedLanguages)
                {
                    var cultureValue = this.oDataObjectFactory.CreateODataComplexValue(this.cultureValueTypeReference.ComplexDefinition());
                    cultureValue.Properties = new[]
                    {
                        new ODataProperty {
                            Name = "Culture", Value = culture.Name
                        },
                        new ODataProperty {
                            Name = "Value", Value = localizedValue.GetValue(culture).Text
                        }
                    };

                    cultureValues.Add(cultureValue);
                }

                var complexValue    = this.oDataObjectFactory.CreateODataComplexValue(this.edmComplexType);
                var collectionValue = this.oDataObjectFactory.CreateODataCollectionValue(this.cultureValuesCollectionTypeReference.CollectionDefinition());
                collectionValue.Items = cultureValues;
                var complexValueProperties = new List <ODataProperty>
                {
                    new ODataProperty
                    {
                        Name  = "CultureValues",
                        Value = collectionValue
                    },
                };

                // invariant value
                ITextValue invariantValue;
                if (localizedValue.TryGetInvariantValue(out invariantValue))
                {
                    complexValueProperties.Add(new ODataProperty {
                        Name = "InvariantValue", Value = invariantValue.Text
                    });
                }

                complexValue.Properties = complexValueProperties;

                return(complexValue);
            }

            if (domainValue != null)
            {
                return(null);                // unknown value, but it might be handled by instance annotations
            }

            throw new ArgumentException("domainValue");
        }
Exemplo n.º 6
0
        public object Deserialize(object value, IDomainType domainType)
        {
            object deserializedValue;

            if (domainType.TryDeserializeValue(Convert.ToString(value), 0, out deserializedValue))
            {
                return(deserializedValue);
            }

            throw new ArgumentException("Cannot deserialize value", nameof(value));
        }
Exemplo n.º 7
0
        public object Serialize(object domainValue, IDomainType domainType)
        {
            var metadataItemValue = domainValue as IMetadataItemValue;

            if (metadataItemValue != null)
            {
                return(metadataItemValue.MetadataItem.Name);
            }

            throw new ArgumentException("domainValue");
        }
Exemplo n.º 8
0
        public object Deserialize(object value, IDomainType domainType)
        {
            // TODO handle cases where this value gets sets to null (insignificant)

            var localizedValue = domainType.EmptyValue as ILocalizedTextValue;

            if (localizedValue == null)
            {
                throw new ArgumentException("defaultValue");
            }

            var complexObject = value as ODataComplexValue;

            if (complexObject == null)
            {
                throw new ArgumentException("value");
            }

            var attributedValue = localizedValue.As <IAttributedValue>();

            localizedValue = attributedValue.RemoveAttributes(attributedValue.Attributes).As <ILocalizedTextValue>();

            // invariant value
            string invariantText;

            if (complexObject.TryGetPropertyValue("InvariantValue", out invariantText))
            {
                localizedValue = localizedValue.SetInvariantValue(invariantText ?? string.Empty);
            }

            // culture dependent values
            ODataCollectionValue cultureValues;

            if (complexObject.TryGetPropertyValue("CultureValues", out cultureValues) && cultureValues.Items.Any())
            {
                foreach (var cultureValue in cultureValues.Items.Cast <ODataComplexValue>())
                {
                    string culture;
                    string text;
                    cultureValue.TryGetPropertyValue("Culture", out culture);
                    cultureValue.TryGetPropertyValue("Value", out text);

                    localizedValue = localizedValue.SetValue(CultureInfo.GetCultureInfo(culture), text);
                }
            }

            return(localizedValue);
        }
Exemplo n.º 9
0
        public static object Deserialize(this IDataType dataType, object value, IDomainType domainType)
        {
            var valueBasedDataType = dataType as IValueBasedDataType;

            if (valueBasedDataType != null)
            {
                return(valueBasedDataType.Deserialize(value));
            }

            var domainTypeBasedDataType = dataType as IDomainTypeDependingDataType;

            if (domainTypeBasedDataType != null)
            {
                return(domainTypeBasedDataType.Deserialize(value, domainType));
            }

            throw new InvalidOperationException("This data type does not provide a know way of deserializing values.");
        }
Exemplo n.º 10
0
        public object Deserialize(object value, IDomainType domainType)
        {
            var edmValue = value as IEdmValue;

            if (edmValue != null)
            {
                string name = this.converter.AsClrValue <string>(edmValue);

                IMetadataItem metadataItem;
                object        metadataItemValue;
                if (this.metadataService.TryGetMetadataItem(name, out metadataItem) &&
                    domainType.TryParse(metadataItem, CultureInfo.InvariantCulture, out metadataItemValue))
                {
                    return(metadataItemValue);
                }
            }

            throw new ArgumentException("value");
        }
Exemplo n.º 11
0
        private void CustomWritePropertiesAsAttributes(SerializationContext serializationContext, ModelElement element, XmlWriter writer)
        {
            // Always call the base class so any extensions are serialized
            base.WritePropertiesAsAttributes(serializationContext, element, writer);

            ScalarProperty instanceOfScalarProperty = element as ScalarProperty;

            global::System.Diagnostics.Debug.Assert(instanceOfScalarProperty != null, "Expecting an instance of ScalarProperty");

            // Type
            if (!serializationContext.Result.Failed)
            {
//                instanceOfScalarProperty.type
//                string clrName = SystemPrimitiveTypesConverter.GetClrName(propValue);

                IDomainType domainType = instanceOfScalarProperty.Type;
                Guid        typeId     = domainType.GetTypeId();

                DONetEntityModelDesignerSerializationHelper.Instance.WriteAttributeString(serializationContext, element,
                                                                                          writer, "typeId", typeId.ToString());
            }
        }
Exemplo n.º 12
0
        public ScalarProperty AddScalarProperty(string name, Type type)
        {
            Guid        typeId      = SystemPrimitiveTypesConverter.GetTypeId(type);
            IDomainType domainType  = null;
            IModelRoot  entityModel = this.Store.GetEntityModel();

            if (typeId != default(Guid))
            {
                domainType = entityModel.GetDomainType(typeId);
            }
            else
            {
                domainType = entityModel.DomainTypes.SingleOrDefault(item => item.FullName == type.FullName);
            }

            if (domainType != null)
            {
                return(AddScalarProperty(name, (DomainType)domainType));
            }

            throw new ArgumentOutOfRangeException("type is not registered as Domain Type!");
        }
Exemplo n.º 13
0
 public bool EqualsTo(IDomainType other)
 {
     return(this.IsBuildIn == other.IsBuildIn &&
            this.BuildInID == other.BuildInID &&
            this.FullName == other.FullName);
 }
Exemplo n.º 14
0
        private void CustomReadPropertiesFromAttributes(SerializationContext serializationContext, ModelElement element, XmlReader reader)
        {
            // Always call the base class so any extensions are deserialized
            base.ReadPropertiesFromAttributes(serializationContext, element, reader);

            ScalarProperty instanceOfScalarProperty = element as ScalarProperty;

            global::System.Diagnostics.Debug.Assert(instanceOfScalarProperty != null, "Expecting an instance of ScalarProperty");

            ModelUpgrader.Instance.RequestSerializationUpgrade(
                upgrader => upgrader.ReadPropertiesFromAttributes(serializationContext, instanceOfScalarProperty, reader));

            // Type

/*
 *          if (!serializationContext.Result.Failed)
 *          {
 *              string attribType = DONetEntityModelDesignerSerializationHelper.Instance.ReadAttribute(serializationContext, element, reader, "type");
 *              if (!string.IsNullOrEmpty(attribType))
 *              {
 *                  string typeDisplayName = SystemPrimitiveTypesConverter.GetDisplayName(attribType);
 *                  if (!string.IsNullOrEmpty(typeDisplayName))
 *                  {
 *                      Type clrType = SystemPrimitiveTypesConverter.GetClrType(typeDisplayName);
 *                      Guid typeId = SystemPrimitiveTypesConverter.GetTypeId(clrType);
 *                      IModelRoot entityModel = element.Store.GetEntityModel();
 *                      IDomainType domainType = entityModel.GetDomainType(typeId);
 *                      if (domainType == null)
 *                      {
 *                          domainType =
 *                              entityModel.BuildInDomainTypes.SingleOrDefault(type => type.FullName == "System.String");
 *                      }
 *
 *                      instanceOfScalarProperty.Type = (DomainType) domainType;
 *
 *                      element.Store.PropertyBag["@@OldModelConversions"] = true;
 *                  }
 *                  else
 *                  {	// Invalid property value, ignored.
 *                      EntityModelDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "type", typeof(global::System.String), attribType);
 *                  }
 *              }
 *          }
 */

            // typeId
            if (!serializationContext.Result.Failed)
            {
                string attribType = DONetEntityModelDesignerSerializationHelper.Instance.ReadAttribute(serializationContext, element, reader, "typeId");
                if (!string.IsNullOrEmpty(attribType))
                {
                    Guid        typeId      = new Guid(attribType);
                    IModelRoot  entityModel = element.Store.GetEntityModel();
                    IDomainType domainType  = entityModel.GetDomainType(typeId);
                    if (domainType == null)
                    {
                        domainType =
                            entityModel.BuildInDomainTypes.SingleOrDefault(type => type.FullName == "System.String");
                    }

                    instanceOfScalarProperty.Type = (DomainType)domainType;
                }
            }
        }
Exemplo n.º 15
0
 public ScalarProperty(string name, IDomainType domainType)
     : base(name)
 {
     this.Type = domainType;
 }
Exemplo n.º 16
0
 public void AddDomainType(IDomainType domainType)
 {
     this.domainTypes.Add(domainType);
 }
Exemplo n.º 17
0
        public object Serialize(object domainValue, IDomainType domainType)
        {
            int attributes;

            return(domainType.SerializeValue(domainValue, out attributes));
        }