Example #1
0
        /// <summary>
        /// Adds the property for exposing.
        /// </summary>
        /// <param name="typeSettings">The type settings.</param>
        /// <param name="dataObjectProperty">The data object property.</param>
        private static void AddProperty(DataObjectEdmTypeSettings typeSettings, PropertyInfo dataObjectProperty)
        {
            // Master property.
            if (dataObjectProperty.PropertyType.IsSubclassOf(typeof(DataObject)))
            {
                var masterMetadata = new DataObjectEdmMasterSettings(dataObjectProperty.PropertyType)
                {
                    AllowNull = dataObjectProperty.GetCustomAttribute(typeof(NotNullAttribute)) == null
                };

                typeSettings.MasterProperties.Add(dataObjectProperty, masterMetadata);
                return;
            }

            // Detail property.
            if (dataObjectProperty.PropertyType.IsSubclassOf(typeof(DetailArray)))
            {
                var detailType = dataObjectProperty.PropertyType.GetProperty("Item", new[] { typeof(int) }).PropertyType;
                typeSettings.DetailProperties.Add(dataObjectProperty, new DataObjectEdmDetailSettings(detailType));
                return;
            }

            // Own property.
            typeSettings.OwnProperties.Add(dataObjectProperty);
        }
        private void RegisterMasters()
        {
            foreach (Type dataObjectType in _metadata.Types)
            {
                DataObjectEdmTypeSettings typeSettings  = _metadata[dataObjectType];
                EdmEntityType             edmEntityType = _registeredEdmEntityTypes[dataObjectType];

                foreach (var masterProperty in typeSettings.MasterProperties)
                {
                    if (!_registeredEdmEntityTypes.ContainsKey(masterProperty.Value.MasterType))
                    {
                        throw new Exception($"Тип мастера {masterProperty.Value.MasterType.FullName} не найден для типа {dataObjectType.FullName}.");
                    }

                    EdmEntityType edmTargetEntityType = _registeredEdmEntityTypes[masterProperty.Value.MasterType];
                    bool          allowNull           = masterProperty.Value.AllowNull;

                    var navigationProperty = new EdmNavigationPropertyInfo
                    {
                        Name               = GetEntityPropertName(masterProperty.Key),
                        Target             = edmTargetEntityType,
                        TargetMultiplicity = allowNull
                            ? EdmMultiplicity.ZeroOrOne
                            : EdmMultiplicity.One
                    };

                    EdmNavigationProperty unidirectionalNavigation = edmEntityType.AddUnidirectionalNavigation(navigationProperty);
                    this.SetAnnotationValue(unidirectionalNavigation, new ClrPropertyInfoAnnotation(masterProperty.Key));

                    EdmEntitySet thisEdmEntitySet   = _registeredEntitySets[dataObjectType];
                    EdmEntitySet targetEdmEntitySet = _registeredEntitySets[masterProperty.Value.MasterType];
                    thisEdmEntitySet.AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);

                    // Add relation for all derived types.
                    if (_typeHierarchy.ContainsKey(dataObjectType))
                    {
                        foreach (Type derivedDataObjectType in _typeHierarchy[dataObjectType])
                        {
                            GetEdmEntitySet(derivedDataObjectType).AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);
                        }
                    }
                }
            }
        }
        private void RegisterDetails()
        {
            foreach (Type dataObjectType in _metadata.Types)
            {
                DataObjectEdmTypeSettings typeSettings  = _metadata[dataObjectType];
                EdmEntityType             edmEntityType = _registeredEdmEntityTypes[dataObjectType];

                foreach (var detailProperty in typeSettings.DetailProperties)
                {
                    if (!_registeredEdmEntityTypes.ContainsKey(detailProperty.Value.DetailType))
                    {
                        throw new Exception($"Тип детейла {detailProperty.Value.DetailType.FullName} не найден для типа {dataObjectType.FullName}.");
                    }

                    EdmEntityType edmTargetEntityType = _registeredEdmEntityTypes[detailProperty.Value.DetailType];

                    var navigationProperty = new EdmNavigationPropertyInfo
                    {
                        Name               = GetEntityPropertName(detailProperty.Key),
                        Target             = edmTargetEntityType,
                        TargetMultiplicity = EdmMultiplicity.Many
                    };

                    EdmNavigationProperty unidirectionalNavigation = edmEntityType.AddUnidirectionalNavigation(navigationProperty);
                    this.SetAnnotationValue(unidirectionalNavigation, new ClrPropertyInfoAnnotation(detailProperty.Key));

                    EdmEntitySet thisEdmEntitySet   = _registeredEntitySets[dataObjectType];
                    EdmEntitySet targetEdmEntitySet = _registeredEntitySets[detailProperty.Value.DetailType];
                    thisEdmEntitySet.AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);

                    // Add relation for all derived types.
                    if (_typeHierarchy.ContainsKey(dataObjectType))
                    {
                        foreach (Type derivedDataObjectType in _typeHierarchy[dataObjectType])
                        {
                            GetEdmEntitySet(derivedDataObjectType).AddNavigationTarget(unidirectionalNavigation, targetEdmEntitySet);
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Adds the properties for exposing.
        /// </summary>
        /// <param name="dataObjectType">The type of the data object.</param>
        /// <param name="typeSettings">The type settings.</param>
        private void AddProperties(Type dataObjectType, DataObjectEdmTypeSettings typeSettings)
        {
            Func <PropertyInfo, bool> propertyFilter = PropertyFilter ?? (p => true);

            var keyType = GetKeyType(dataObjectType);

            if (keyType == GetKeyType(dataObjectType.BaseType))
            {
                typeSettings.KeyType = null;
            }
            else
            {
                Contract.Assume(
                    dataObjectType.BaseType == typeof(DataObject),
                    $"Запрещено переопределение ключа в типе {dataObjectType.FullName}, т.к он не наследуется непосредственно от DataObject.");
                typeSettings.KeyType = keyType;
            }

            IEnumerable <PropertyInfo> properties = dataObjectType
                                                    .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
                                                    .Where(propertyFilter);

            foreach (var property in properties)
            {
                bool overridden = false;
                foreach (var method in property.GetAccessors(true))
                {
                    if (!method.Equals(method.GetBaseDefinition()))
                    {
                        overridden = true;
                        break;
                    }
                }

                if (overridden && property.Name != _keyProperty.Name)
                {
                    continue;
                }
                AddProperty(typeSettings, property);
            }
        }
        private void BuildEntitySets()
        {
            var container = new EdmEntityContainer(DefaultNamespace, "DataObjectContainer");

            AddElement(container);

            foreach (Type dataObjectType in _metadata.Types)
            {
                DataObjectEdmTypeSettings typeSettings = _metadata[dataObjectType];

                if (!typeSettings.EnableCollection)
                {
                    continue;
                }

                EdmEntityType edmEntityType = _registeredEdmEntityTypes[dataObjectType];
                EdmEntitySet  edmEntitySet  = container.AddEntitySet(typeSettings.CollectionName, edmEntityType);

                _registeredCollections.Add(typeSettings.CollectionName, dataObjectType);
                _registeredEntitySets.Add(dataObjectType, edmEntitySet);
            }
        }
Example #6
0
        /// <summary>
        /// Adds the data object for exposing with its whole hierarchy.
        /// </summary>
        /// <param name="meta">The metadata object.</param>
        /// <param name="dataObjectType">The type of the data object.</param>
        private void AddDataObjectWithHierarchy(DataObjectEdmMetadata meta, Type dataObjectType)
        {
            // Some base class can be already added.
            if (meta.Contains(dataObjectType))
            {
                return;
            }

            if (dataObjectType == typeof(DataObject))
            {
                var dataObjectTypeSettings = meta[dataObjectType] = new DataObjectEdmTypeSettings()
                {
                    KeyType = typeof(Guid), EnableCollection = true, CollectionName = EntitySetNameBuilder(dataObjectType)
                };                                                                                                                                                                                              // TODO
                AddProperty(dataObjectTypeSettings, typeof(DataObject).GetProperty(_keyProperty.Name));
                return;
            }

            Type baseType = dataObjectType.BaseType;

            Contract.Assume(baseType != null);

            AddDataObjectWithHierarchy(meta, baseType);

            var typeSettings = meta[dataObjectType] = new DataObjectEdmTypeSettings
            {
                EnableCollection = true,
                CollectionName   = EntitySetNameBuilder(dataObjectType),
                DefaultView      = DynamicView.Create(dataObjectType, null).View
            };

            AddProperties(dataObjectType, typeSettings);
            if (typeSettings.KeyType != null)
            {
                meta[baseType].KeyType = null;
            }
        }