Example #1
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;
            }
        }
        /// <summary>
        /// Создаёт динамическое представление.
        /// </summary>
        /// <param name="dataObjectType">Тип</param>
        /// <param name="properties">Список полных имён свойств.</param>
        /// <param name="cache">Словарь с созданными динамическими представлениями, заполняется внутри Create.</param>
        /// <returns>Динамическое представление.</returns>
        public static DynamicView Create(Type dataObjectType, List <string> properties, Dictionary <string, DynamicView> cache = null)
        {
            if (properties == null)
            {
                properties = GetProperties(dataObjectType);
            }
            properties.Sort();
            var prevProp = string.Empty;

            for (int i = 0; i < properties.Count; i++)
            {
                if (properties[i] == prevProp)
                {
                    properties.RemoveAt(i);
                    i--;
                    continue;
                }

                prevProp = properties[i];
            }

            var key = CreateKey(dataObjectType, properties);

            if (cache != null && cache.ContainsKey(key))
            {
                return(cache[key]);
            }

            var view = new DynamicView(dataObjectType, properties, cache);

            if (cache != null)
            {
                cache.Add(key, view);
            }
            return(view);
        }