コード例 #1
0
        /// <summary>
        /// Maps the specified source.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="item">The item.</param>
        public override void Map([NotNull] IEntity entity, [NotNull] Item item)
        {
            Assert.ArgumentNotNull(entity, "entity");
            Assert.ArgumentNotNull(item, "item");

            item.Fields.ReadAll();

            using (new EditContext(item))
            {
                foreach (PropertyInfo entityProperty in entity.GetType().GetProperties())
                {
                    if (!entityProperty.CanRead)
                    {
                        continue;
                    }

                    object entityValue = entityProperty.GetValue(entity, null);

                    object mappingItem = this.GetMappingItem(item, entityProperty);
                    if (mappingItem == null)
                    {
                        continue;
                    }

                    IEntityMemberConverter rule = this.Mappings.GetConverter(entityProperty);
                    if (mappingItem is Field && rule is IRequiresStorageObject <Field> )
                    {
                        ((IRequiresStorageObject <Field>)rule).StorageObject = (Field)mappingItem;
                    }

                    rule.ToStorage(entityValue);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Maps the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="entity">The entity.</param>
        public override void Map([NotNull] Item item, [NotNull] IEntity entity)
        {
            Assert.ArgumentNotNull(item, "item");
            Assert.ArgumentNotNull(entity, "entity");

            item.Fields.ReadAll();

            Assert.IsNotNull(this.Mappings, "Unable to convert an item to an entity. EntityMemberConverterLookupTable cannot be null.");

            foreach (PropertyInfo entityProperty in entity.GetType().GetProperties())
            {
                IEntityMemberConverter converter = this.Mappings.GetConverter(entityProperty);

                if (converter is IRequiresEntityMemberType)
                {
                    ((IRequiresEntityMemberType)converter).MemberType = entityProperty.PropertyType;
                }

                object mappingItem;
                if (converter is IRequiresStorageObject <Item> )
                {
                    mappingItem = item;
                }
                else
                {
                    mappingItem = this.GetMappingItem(item, entityProperty);
                }

                if (mappingItem == null)
                {
                    var defaultConverter = this.Mappings.DirectMappingEntityMemberConverter;
                    defaultConverter.StorageObject = item;

                    mappingItem = item.GetType().GetProperty(entityProperty.Name);
                    if (mappingItem == null)
                    {
                        continue;
                    }

                    converter = defaultConverter;
                }

                object entityValue = converter.ConvertFrom(mappingItem);

                if (entityProperty.CanWrite)
                {
                    entityProperty.SetValue(entity, entityValue, null);
                }
            }

            entity.Alias = item.ID.ToString();
        }