/// <summary> /// Gets the template. /// </summary> /// <param name="type">The type of the object.</param> /// <returns>The template Id.</returns> public virtual string GetTemplate(Type type) { Assert.ArgumentNotNull(type, "type"); if (Context.Entity.HasRegistration(type)) { type = Context.Entity.Resolve(type).GetType(); } EntityAttribute entityAttribute = Attribute.GetCustomAttribute(type, typeof(EntityAttribute), true) as EntityAttribute; if (entityAttribute != null && !string.IsNullOrEmpty(entityAttribute.TemplateId)) { return(entityAttribute.TemplateId); } return(string.Empty); }
/// <summary> /// Gets the name of the property value by attribute. /// </summary> /// <typeparam name="T">The object type.</typeparam> /// <typeparam name="Tr">The type of the business object.</typeparam> /// <param name="entity">The entity instance.</param> /// <param name="fieldName">Name of the field.</param> /// <returns>The object value.</returns> public virtual T GetPropertyValueByField <T, Tr>(Tr entity, string fieldName) { Assert.ArgumentNotNull(entity, "entity"); Assert.ArgumentNotNullOrEmpty(fieldName, "fieldName"); var entityType = entity.GetType(); object obj = Context.Entity.HasRegistration(entityType) ? Context.Entity.Resolve(entityType) : entity; foreach (PropertyInfo info in entity.GetType().GetProperties()) { EntityAttribute entityAttribute = Attribute.GetCustomAttribute(obj.GetType().GetProperty(info.Name), typeof(EntityAttribute), true) as EntityAttribute; if (entityAttribute == null || string.IsNullOrEmpty(entityAttribute.FieldName) || !string.Equals(fieldName, entityAttribute.FieldName)) { continue; } return(TypeUtil.TryParse(info.GetValue(entity, null), default(T))); } return(default(T)); }
/// <summary> /// Copies the properties values. /// </summary> /// <typeparam name="T">The type of the source entity</typeparam> /// <typeparam name="Tr">The type of the target entity.</typeparam> /// <param name="sourceEntity">The source entity.</param> /// <param name="targetEntity">The target entity.</param> public virtual void CopyPropertiesValues <T, Tr>(T sourceEntity, ref Tr targetEntity) { Assert.ArgumentNotNull(sourceEntity, "Argument source entity is null"); Assert.ArgumentNotNull(targetEntity, "Argument target entity is null"); var sourceEntityType = sourceEntity.GetType(); object sourceObject = Context.Entity.HasRegistration(sourceEntityType) ? Context.Entity.Resolve(sourceEntityType) : sourceEntity; var targetEntityType = targetEntity.GetType(); object targetObject = Context.Entity.HasRegistration(targetEntityType) ? Context.Entity.Resolve(targetEntityType) : targetEntity; foreach (PropertyInfo targetInfo in targetEntity.GetType().GetProperties()) { EntityAttribute targetEntityAttribute = Attribute.GetCustomAttribute(targetObject.GetType().GetProperty(targetInfo.Name), typeof(EntityAttribute)) as EntityAttribute; if (targetEntityAttribute == null || string.IsNullOrEmpty(targetEntityAttribute.FieldName)) { continue; } foreach (PropertyInfo sourceInfo in sourceEntity.GetType().GetProperties()) { EntityAttribute sourcerEntityAttribute = Attribute.GetCustomAttribute(sourceObject.GetType().GetProperty(sourceInfo.Name), typeof(EntityAttribute)) as EntityAttribute; if (sourcerEntityAttribute == null || string.IsNullOrEmpty(sourcerEntityAttribute.FieldName) || !string.Equals(sourcerEntityAttribute.FieldName, targetEntityAttribute.FieldName, StringComparison.OrdinalIgnoreCase)) { continue; } if (!sourceInfo.CanRead || !targetInfo.CanWrite || sourceInfo.PropertyType != targetInfo.PropertyType) { continue; } object source = sourceInfo.GetValue(sourceEntity, null); if (source != null) { targetInfo.SetValue(targetEntity, source, null); } } } }
/// <summary> /// Gets the entity. /// </summary> /// <param name="item">The container item.</param> /// <param name="entity">The container contract.</param> /// <returns>The container instance.</returns> public virtual object GetEntity(Item item, object entity) { Assert.ArgumentNotNull(entity, "The instance of the entity is null"); Assert.ArgumentNotNull(item, "Container item is null"); if (entity is IEntity) { ((IEntity)entity).Alias = item.ID.ToString(); } foreach (PropertyInfo info in entity.GetType().GetProperties()) { EntityAttribute entityAttribute = Attribute.GetCustomAttribute(info, typeof(EntityAttribute), true) as EntityAttribute; if (entityAttribute == null || string.IsNullOrEmpty(entityAttribute.FieldName)) { continue; } Field field = item.Fields[entityAttribute.FieldName]; if (field == null || field.Value == null) { continue; } Type instanceType = info.PropertyType; object instanceValue = null; if (info.CanRead) { instanceValue = info.GetValue(entity, null); } if (instanceValue != null) { instanceType = instanceValue.GetType(); } if (entityAttribute.Rule == FieldMappingRule.GetItemName) { string itemId = instanceValue as string; if (!string.IsNullOrEmpty(itemId) && ID.IsID(itemId)) { Item targetItem = Sitecore.Context.Database.GetItem(itemId) ?? Sitecore.Context.ContentDatabase.GetItem(itemId); if (info.CanWrite) { info.SetValue(entity, Utils.TypeUtil.Parse(targetItem.Name, info.PropertyType), null); } } continue; } if (typeof(IEntity).IsAssignableFrom(instanceType)) { string itemPath = Utils.TypeUtil.TryParse(this.GetFieldValue(field), string.Empty); if (!string.IsNullOrEmpty(itemPath) && ID.IsID(itemPath)) { Item nestedContainerItem = Sitecore.Context.Database.GetItem(itemPath) ?? Sitecore.Context.ContentDatabase.GetItem(itemPath); if (nestedContainerItem != null) { object nestedContainer = this.GetEntity(nestedContainerItem, info.PropertyType); if (info.CanWrite) { info.SetValue(entity, nestedContainer, null); } } } continue; } if (info.CanWrite) { info.SetValue(entity, Utils.TypeUtil.Parse(this.GetFieldValue(field), info.PropertyType), null); } } return(entity); }