Beispiel #1
0
        /// <summary>
        /// The full <see cref="ClasslessDTO"/> from thing.
        /// </summary>
        /// <param name="metaDataProvider">
        /// An instance of <see cref="IMetaDataProvider"/> used to provide metadata (reflection) for a <see cref="CDP4Common.DTO.Thing"/>
        /// </param>
        /// <param name="thing">
        /// The thing.
        /// </param>
        /// <typeparam name="T">
        /// Of type <see cref="Thing"/>
        /// </typeparam>
        /// <returns>
        /// The <see cref="ClasslessDTO"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If the provided <see cref="Thing"/> is null this exception is thrown.
        /// </exception>
        public static ClasslessDTO FullFromThing <T>(IMetaDataProvider metaDataProvider, T thing) where T : Thing
        {
            if (thing == null)
            {
                throw new ArgumentNullException(nameof(thing));
            }

            var classlesdto = new ClasslessDTO();

            // the Iid and ClassKind properties are transferred automatically
            classlesdto.Add("ClassKind", thing.ClassKind);
            classlesdto.Add("Iid", thing.Iid);
            var metainfo = metaDataProvider.GetMetaInfo(thing.ClassKind.ToString());

            foreach (var property in metainfo.GetPropertyNameCollection())
            {
                var propertyMetadata = metainfo.GetPropertyMetaInfo(property);

                if (property.Equals("Iid") || property.Equals("ClassKind") || propertyMetadata.IsDerived || !propertyMetadata.IsDataMember)
                {
                    continue;
                }

                var propertyValue = metainfo.GetValue(property, thing);

                if (propertyValue == null && !propertyMetadata.IsNullable)
                {
                    propertyValue = string.Empty;
                }

                classlesdto.Add(property, propertyValue);
            }

            return(classlesdto);
        }
Beispiel #2
0
        /// <summary>
        /// Given a <see cref="List{T}"/> of properties to add, populate this plain DTO from a <see cref="CDP4Common.DTO.Thing"/>. The Iid and Classkind are populated automatically and thus
        /// do not require to be entered into the <paramref name="propertyList"/> variable. If <paramref name="propertyList"/> is null then only the Iid and ClassKind are passed.
        /// </summary>
        /// <typeparam name="T">
        /// Of type <see cref="Thing"/>
        /// </typeparam>
        /// <param name="metaDataProvider">
        /// An instance of <see cref="IMetaDataProvider"/> used to provide metadata (reflection) for a <see cref="CDP4Common.DTO.Thing"/>
        /// </param>
        /// <param name="thing">
        /// The <see cref="Thing"/>, properties of which to use to populate this DTO with.
        /// </param>
        /// <param name="propertyList">
        /// The list of names of property names to use to filter. Can be null to produce the simplest form with just Id and ClassKind
        /// </param>
        /// <returns>
        /// The <see cref="ClasslessDTO"/>.
        /// </returns>
        public static ClasslessDTO FromThing <T>(IMetaDataProvider metaDataProvider, T thing, IEnumerable <string> propertyList = null) where T : Thing
        {
            if (thing == null)
            {
                throw new ArgumentNullException(nameof(thing));
            }

            var classlesdto = new ClasslessDTO();

            // the Iid and ClassKind properties are transferred automatically
            classlesdto.Add("ClassKind", thing.ClassKind);
            classlesdto.Add("Iid", thing.Iid);
            var metainfo = metaDataProvider.GetMetaInfo(thing.ClassKind.ToString());

            // all other named properties
            if (propertyList != null)
            {
                foreach (var property in propertyList)
                {
                    // If Iid somehow gets included into the list just skip it
                    if (property.Equals("Iid") || property.Equals("ClassKind"))
                    {
                        continue;
                    }

                    var propertyMetadata = metainfo.GetPropertyMetaInfo(property);

                    if (!propertyMetadata.IsDerived && propertyMetadata.IsDataMember)
                    {
                        var propertyValue = metainfo.GetValue(property, thing);

                        if (propertyValue == null && !propertyMetadata.IsNullable)
                        {
                            propertyValue = string.Empty;
                        }

                        classlesdto.Add(property, propertyValue);
                    }
                }
            }

            return(classlesdto);
        }