示例#1
0
        private static void CustomWritePropertiesAsElements(SerializationContext context, EntityIndex element, XmlWriter writer)
        {
            // Unique
            if (!context.Result.Failed)
            {
                Defaultable <bool> propValue = element.Unique;
                propValue.SerializeToXml(writer, "unique");
            }

            // FillFactor
            if (!context.Result.Failed)
            {
                Defaultable <double> propValue = element.FillFactor;
                propValue.SerializeToXml(writer, "fillFactor");
            }

            // IndexName
            if (!context.Result.Failed)
            {
                Defaultable <string> indexName = element.IndexName;
                indexName.SerializeToXml(writer, "indexName");
            }

            // Fields
            if (!context.Result.Failed)
            {
                element.Fields.SerializeToXml(writer);
            }
        }
示例#2
0
        private void TestSerializeDefaultableGeneric <T>(T testValue)
        {
            Defaultable <T> value = new Defaultable <T>();

            TestDefaultableAsDefault(value);

            value.SetAsCustom(testValue);
            Defaultable <T> cloned = value.CloneBySerialization();

            Assert.IsTrue(value.EqualsTo(cloned), string.Format("Serializing '{0}' value", testValue.GetType().Name));
        }
示例#3
0
        private string GetPairFromToValue(bool from)
        {
            bool isSource     = IsSourceWithinAssociation();
            var  associations = this.PersistentTypeHasAssociations;

            if (associations == null)
            {
                return(string.Empty);
            }

            var endA = isSource ? associations.End2 : associations.End1;
            var endB = isSource ? associations.End1 : associations.End2;
            Defaultable <string> defPairTo = from ? endA.PairTo : endB.PairTo;

            return(defPairTo.IsDefault() ? string.Empty : defPairTo.Value);
        }
示例#4
0
        /// <summary>
        /// Converts values to a TItemType.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (context != null)
            {
                object      propertyValue = context.PropertyDescriptor.GetValue(context.Instance);
                Defaultable defaultable   = propertyValue as Defaultable;

                if (value is string && defaultable != null)
                {
                    defaultable.SetValue(value);
                    return(defaultable.Clone());
                }
            }

            return(base.ConvertFrom(context, culture, value));
        }
示例#5
0
        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            object      newInstance   = null;
            object      propertyValue = context.PropertyDescriptor.GetValue(context.Instance);
            Defaultable defaultable   = propertyValue as Defaultable;

            if (defaultable != null)
            {
                newInstance = defaultable.CloneFromPropertyValues(propertyValues);
            }

            if (newInstance == null)
            {
                newInstance = base.CreateInstance(context, propertyValues);
            }

            return(newInstance);
        }
示例#6
0
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            Defaultable defaultable = (Defaultable)value;

            var descriptor = TypeDescriptor.GetProperties(defaultable.GetType(), attributes);

            List <PropertyDescriptor> propertiesToRemove = new List <PropertyDescriptor>();

            foreach (PropertyDescriptor prop in descriptor)
            {
                if (prop.Name == "Value" && defaultable.IsDefault())
                {
                    propertiesToRemove.Add(prop);
                }
            }


            PropertyDescriptorCollection result = descriptor;

            if (propertiesToRemove.Count > 0)
            {
                PropertyDescriptor[] propertyDescriptors = descriptor.OfType <PropertyDescriptor>().ToArray();
                result = new PropertyDescriptorCollection(propertyDescriptors, false);

                foreach (PropertyDescriptor prop in descriptor)
                {
                    if (prop.Name == "Value" && defaultable.IsDefault())
                    {
                        try
                        {
                            result.Remove(prop);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }
            }

            result.Sort(new[] { "ValueType", "Value" });

            return(result);
        }
示例#7
0
    public string EscapeType(IPropertyBase property)
    {
        string result = null;

        if (property is IScalarProperty)
        {
            IScalarProperty scalarProperty = (IScalarProperty)property;
            Type            clrType        = scalarProperty.Type.TryGetClrType(null);
            string          typeName;

            if (clrType != null)
            {
                typeName = Escape(clrType);
                Defaultable <bool> nullable = scalarProperty.FieldAttribute.Nullable;
                bool isNullable             = !nullable.IsDefault() && nullable.Value;
                if (clrType.IsValueType && isNullable)
                {
                    typeName = String.Format(CultureInfo.InvariantCulture, SYSTEM_NULLABLE_FORMAT, typeName);
                }
            }
            else
            {
                typeName = scalarProperty.Type.FullName;
            }

            result = typeName;
        }
        else if (property is INavigationProperty)
        {
            INavigationProperty navigationProperty = (INavigationProperty)property;
            result = navigationProperty.GetPropertyType(_code, EscapeNameWithNamespace,
                                                        delegate(OrmType type, string s)
            {
                return(BuildXtensiveType(type, s));
            });
        }
        else if (property is IStructureProperty)
        {
            IStructureProperty structureProperty = (IStructureProperty)property;
            result = EscapeNameWithNamespace(structureProperty.TypeOf, property.Owner);
        }

        return(result);
    }
示例#8
0
        private void TestDefaultableMerge <T>(T currentValue, T otherValue, MergeConflictAction mergeConflictAction)
        {
            Defaultable <T> current = new Defaultable <T>();

            current.SetAsCustom(currentValue);

            Defaultable <T> other = new Defaultable <T>();

            other.SetAsCustom(otherValue);

            Defaultable <T> merged       = current.Merge(other, mergeConflictAction);
            string          errorMessage = string.Format("Merging '{0}' values", currentValue.GetType().Name);

            if (mergeConflictAction == MergeConflictAction.TakeCurrent)
            {
                Assert.IsTrue(current.EqualsTo(merged), errorMessage);
            }
            else if (mergeConflictAction == MergeConflictAction.TakeOther)
            {
                Assert.IsTrue(other.EqualsTo(merged), errorMessage);
            }
        }
示例#9
0
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            object propertyValue = context.PropertyDescriptor.GetValue(context.Instance);
            bool   isExclusive   = propertyValue == null;

            if (!isExclusive)
            {
                Defaultable defaultable = propertyValue as Defaultable;
                if (defaultable != null)
                {
                    object exValue = defaultable.InternalGetValue();
                    isExclusive = defaultable.IsDefault() || exValue == null;

                    Type typeOfValue = defaultable.GetTypeOfValue();
                    if (!isExclusive)
                    {
                        isExclusive = (typeOfValue == typeof(bool) || typeOfValue.IsEnum);
                    }
                }
            }

            return(isExclusive);
        }
示例#10
0
        private void TestDefaultableAsDefault(Defaultable source)
        {
            Defaultable cloned = source.CloneBySerialization();

            Assert.IsTrue(source.EqualsTo(cloned));
        }
示例#11
0
        private void TestSerializationInterface(EntityModel entityModel)
        {
            const string         INTERFACE1_NAME            = "IEntity";
            const AccessModifier INTERFACE1_ACCESS_MODIFIER = AccessModifier.Internal;
            const string         INTERFACE1_DOCUMENTATION   = "Documentation 4E6C6256-A5FE-442E-AA37-2B58897D76CB";
            const string         INTERFACE1_NAMESPACE       = "Namespace 49341C9C-DAC5-4450-BD6F-569FE6FC85A6";

            StringTreeNode index1_keyField_Id = new StringTreeNode {
                DisplayValue = "Id", IconIndex = 2
            };
            StringTreeNode index1_keyField_OId = new StringTreeNode {
                DisplayValue = "OId", IconIndex = 1
            };
            StringTreeNode index1_IncludedField_Age = new StringTreeNode {
                DisplayValue = "Age", IconIndex = 0
            };
            StringTreeNode index1_IncludedField_Weight = new StringTreeNode {
                DisplayValue = "Weight", IconIndex = 0
            };
            Defaultable <double> index1_FillFactor = null;
            Defaultable <string> index1_IndexName  = null;

            StringTreeNode index2_keyField_Id2 = new StringTreeNode {
                DisplayValue = "Id2", IconIndex = 1
            };
            StringTreeNode index2_keyField_OId2 = new StringTreeNode {
                DisplayValue = "OId2", IconIndex = 2
            };
            StringTreeNode index2_IncludedField_Age2 = new StringTreeNode {
                DisplayValue = "Age2", IconIndex = 0
            };
            StringTreeNode index2_IncludedField_Weight2 = new StringTreeNode {
                DisplayValue = "Weight2", IconIndex = 0
            };
            Defaultable <double> index2_FillFactor = null;
            Defaultable <string> index2_IndexName  = null;

            Interface interface1 = CreateElement(entityModel,
                                                 delegate(Store store)
            {
                Interface result     = new Interface(store);
                result.Name          = INTERFACE1_NAME;
                result.Access        = INTERFACE1_ACCESS_MODIFIER;
                result.Documentation = INTERFACE1_DOCUMENTATION;
                result.Namespace     = INTERFACE1_NAMESPACE;

                EntityIndex index1 = new EntityIndex(store);
                result.Indexes.Add(index1);

                index1.Fields.KeyFields.Add(index1_keyField_Id);
                index1.Fields.KeyFields.Add(index1_keyField_OId);
                index1.Fields.IncludedFields.Add(index1_IncludedField_Age);
                index1.Fields.IncludedFields.Add(index1_IncludedField_Weight);

                index1.FillFactor.SetAsCustom(123.45);
                index1_FillFactor = (Defaultable <double>)index1.FillFactor.Clone();

                index1.IndexName.SetAsCustom("idxPrimary");
                index1_IndexName = (Defaultable <string>)index1.IndexName.Clone();

                EntityIndex index2 = new EntityIndex(store);
                result.Indexes.Add(index2);

                index2.Fields.KeyFields.Add(index2_keyField_Id2);
                index2.Fields.KeyFields.Add(index2_keyField_OId2);
                index2.Fields.IncludedFields.Add(index2_IncludedField_Age2);
                index2.Fields.IncludedFields.Add(index2_IncludedField_Weight2);

                index2.FillFactor.SetAsCustom(45.123);
                index2_FillFactor = (Defaultable <double>)index2.FillFactor.Clone();

                index2.IndexName.SetAsCustom("idxSecondary");
                index2_IndexName = (Defaultable <string>)index2.IndexName.Clone();

                result.AddScalarProperty("Id", typeof(Int32));

                return(result);
            });

            Assert.AreEqual(entityModel.PersistentTypes.Count, 1);

            Interface clonedInterface1 = TestSerializationByClone(interface1);

            Assert.AreEqual(clonedInterface1.Name, INTERFACE1_NAME);
            Assert.AreEqual(clonedInterface1.Access, INTERFACE1_ACCESS_MODIFIER);
            Assert.AreEqual(clonedInterface1.Documentation, INTERFACE1_DOCUMENTATION);
            Assert.AreEqual(clonedInterface1.Namespace, INTERFACE1_NAMESPACE);

            Assert.AreEqual(clonedInterface1.Indexes.Count, 2);

            EntityIndex clonedIndex1 = clonedInterface1.Indexes[0];

            Assert.AreEqual(clonedIndex1.Fields.KeyFields.Count, 2);
            Assert.AreEqual(clonedIndex1.Fields.IncludedFields.Count, 2);
            Assert.IsTrue(clonedIndex1.FillFactor.EqualsTo(index1_FillFactor));
            Assert.IsTrue(clonedIndex1.IndexName.EqualsTo(index1_IndexName));

            StringTreeNode clonedIndex1_keyField_Id = clonedIndex1.Fields.KeyFields[0];

            Assert.AreEqual(clonedIndex1_keyField_Id.DisplayValue, index1_keyField_Id.DisplayValue);
            Assert.AreEqual(clonedIndex1_keyField_Id.IconIndex, index1_keyField_Id.IconIndex);

            StringTreeNode clonedIndex1_keyField_OId = clonedIndex1.Fields.KeyFields[1];

            Assert.AreEqual(clonedIndex1_keyField_OId.DisplayValue, index1_keyField_OId.DisplayValue);
            Assert.AreEqual(clonedIndex1_keyField_OId.IconIndex, index1_keyField_OId.IconIndex);

            StringTreeNode clonedIndex1_IncludedField_Age = clonedIndex1.Fields.IncludedFields[0];

            Assert.AreEqual(clonedIndex1_IncludedField_Age.DisplayValue, index1_IncludedField_Age.DisplayValue);
            Assert.AreEqual(clonedIndex1_IncludedField_Age.IconIndex, index1_IncludedField_Age.IconIndex);

            StringTreeNode clonedIndex1_IncludedField_Weight = clonedIndex1.Fields.IncludedFields[1];

            Assert.AreEqual(clonedIndex1_IncludedField_Weight.DisplayValue, index1_IncludedField_Weight.DisplayValue);
            Assert.AreEqual(clonedIndex1_IncludedField_Weight.IconIndex, index1_IncludedField_Weight.IconIndex);

            EntityIndex clonedIndex2 = clonedInterface1.Indexes[1];

            Assert.AreEqual(clonedIndex2.Fields.KeyFields.Count, 2);
            Assert.AreEqual(clonedIndex2.Fields.IncludedFields.Count, 2);
            Assert.IsTrue(clonedIndex2.FillFactor.EqualsTo(index2_FillFactor));
            Assert.IsTrue(clonedIndex2.IndexName.EqualsTo(index2_IndexName));

            StringTreeNode clonedIndex2_keyField_Id2 = clonedIndex2.Fields.KeyFields[0];

            Assert.AreEqual(clonedIndex2_keyField_Id2.DisplayValue, index2_keyField_Id2.DisplayValue);
            Assert.AreEqual(clonedIndex2_keyField_Id2.IconIndex, index2_keyField_Id2.IconIndex);

            StringTreeNode clonedIndex2_keyField_OId2 = clonedIndex2.Fields.KeyFields[1];

            Assert.AreEqual(clonedIndex2_keyField_OId2.DisplayValue, index2_keyField_OId2.DisplayValue);
            Assert.AreEqual(clonedIndex2_keyField_OId2.IconIndex, index2_keyField_OId2.IconIndex);

            StringTreeNode clonedIndex2_IncludedField_Age2 = clonedIndex2.Fields.IncludedFields[0];

            Assert.AreEqual(clonedIndex2_IncludedField_Age2.DisplayValue, index2_IncludedField_Age2.DisplayValue);
            Assert.AreEqual(clonedIndex2_IncludedField_Age2.IconIndex, index2_IncludedField_Age2.IconIndex);

            StringTreeNode clonedIndex2_IncludedField_Weight2 = clonedIndex2.Fields.IncludedFields[1];

            Assert.AreEqual(clonedIndex2_IncludedField_Weight2.DisplayValue, index2_IncludedField_Weight2.DisplayValue);
            Assert.AreEqual(clonedIndex2_IncludedField_Weight2.IconIndex, index2_IncludedField_Weight2.IconIndex);
        }
示例#12
0
        private static void CustomReadPropertiesFromElements(SerializationContext serializationContext, EntityIndex element, XmlReader reader)
        {
            while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == System.Xml.XmlNodeType.Element)
            {
                switch (reader.LocalName)
                {
                case "unique":
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        Defaultable <bool> unique = new Defaultable <bool>();
                        unique.DeserializeFromXml(reader);
                        element.Unique = unique;

                        SerializationUtilities.SkipToNextElement(reader);
                        reader.SkipToNextElementFix();
                    }
                    break;

                case "fillFactor":
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        Defaultable <double> fillfactor = new Defaultable <double>();
                        fillfactor.DeserializeFromXml(reader);
                        element.FillFactor = fillfactor;

                        SerializationUtilities.SkipToNextElement(reader);
                        reader.SkipToNextElementFix();
                    }
                    break;

                case "indexName":
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        Defaultable <string> indexName = new Defaultable <string>();
                        indexName.DeserializeFromXml(reader);
                        element.IndexName = indexName;

                        SerializationUtilities.SkipToNextElement(reader);
                        reader.SkipToNextElementFix();
                    }
                    break;

                case "fields":
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        OrmIndexFields fields = new OrmIndexFields();
                        fields.DeserializeFromXml(reader);
                        element.Fields = fields;

                        SerializationUtilities.SkipToNextElement(reader);
                        reader.SkipToNextElementFix();
                    }
                    break;

                default:
                    return;      // Don't know this element.
                }
            }
        }
示例#13
0
    private string[] CreateTypeAttributes(ITypeAttributesBuilder attributesBuilder, IEnumerable <IOrmAttribute> attributes,
                                          bool allowInPoco = false)
    {
        if (this.ActiveDTOStage && !allowInPoco)
        {
            return(new string[0]);
        }

        List <string> result = new List <string>();

        if (attributes != null)
        {
            foreach (var attribute in attributes)
            {
                foreach (var attributeGroup in attributesBuilder.GetAttributeGroups(attribute))
                {
                    var           attributeGroupItems = attributesBuilder.GetAttributeGroupItems(attribute, attributeGroup);
                    StringBuilder attr = new StringBuilder(attributeGroup.FormatFullName());
                    if (attributeGroupItems.Count > 0)
                    {
                        int beforeItemsLen = attr.Length;

                        bool handledByCustom = attributesBuilder.BuildCustomFormatComplete(attributeGroup,
                                                                                           attributeGroupItems, ref attr);

                        if (!handledByCustom)
                        {
                            foreach (KeyValuePair <string, Defaultable> attributeGroupItem in attributesBuilder.SortAttributeGroupItems(
                                         attributeGroup, attributeGroupItems))
                            {
                                string      attrItemName  = attributeGroupItem.Key;
                                Defaultable attrItemValue = attributeGroupItem.Value;
                                if (!attrItemValue.IsDefault())
                                {
                                    object value        = attrItemValue.GetValue();
                                    string valueLiteral = CreateLiteral(value);

                                    bool buildedCustomFormat = attributesBuilder.BuildCustomFormat(attributeGroup,
                                                                                                   attrItemName, valueLiteral, value, ref attr);

                                    if (!buildedCustomFormat)
                                    {
                                        attr.AppendFormat("{0}={1},", attrItemName, valueLiteral);
                                    }
                                }
                            }
                        }

                        if (attr[attr.Length - 1] == ',')
                        {
                            attr = attr.Remove(attr.Length - 1, 1);
                        }

                        if (attr.Length > beforeItemsLen)
                        {
                            attr.Insert(beforeItemsLen, "(");
                            attr.Append(")");
                        }
                    }

                    result.Add(attr.ToString());
                }
            }
        }

        return(result.ToArray());
    }