private IEnumerable <DataEntity> ToDataEntity <T>(IEnumerable <T> entities)
        {
            var type   = typeof(T);
            var fields = type.GetProperties(BindingFlags.Instance |
                                            BindingFlags.FlattenHierarchy |
                                            BindingFlags.Public |
                                            BindingFlags.GetProperty);

            foreach (var entity in entities)
            {
                var dataEntity = new QueryDataEntity
                {
                    ObjectDefinitionFullName = type.Name,
                    Name = type.Name
                };


                foreach (var field in fields)
                {
                    dataEntity.Properties.Add(
                        field.Name,
                        field.GetValue(entity, null));
                }

                yield return(dataEntity.ToDataEntity());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Transforms the specified entity collection into a Scribe Online Data Entity collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        private IEnumerable <DataEntity> ToDataEntity <T>(IEnumerable <T> entities)
        {
            //get the type and its properties. We'll use this to build the fields with Reflection
            var type   = typeof(T);
            var fields = type.GetProperties(BindingFlags.Instance |
                                            BindingFlags.FlattenHierarchy |
                                            BindingFlags.Public |
                                            BindingFlags.GetProperty);

            //Loop through the retrieved entities and create a DataEntity from it:
            foreach (var entity in entities)
            {
                var dataEntity = new QueryDataEntity
                {
                    ObjectDefinitionFullName = type.Name,
                    Name = type.Name
                };

                //Add the fields to the entity:

                /* Each KeyValuePair in the fields must be complete.
                 * If the field's value is NULL here, Scribe OnLine will throw
                 * an exception.
                 * To send a NULL field to Scribe Online, just don't add it to this dictionary.
                 */

                foreach (var field in fields)
                {
                    dataEntity.Properties.Add(
                        field.Name,
                        field.GetValue(entity, null));
                }

                //Hand back the completed object:
                yield return(dataEntity.ToDataEntity());
            }
        }