Beispiel #1
0
        public static TEntity ToEntity <TEntity>(IBindingModel requestModel, IComponentContext container, bool lazyLoading, int level) where TEntity : IEntity, new()
        {
            TEntity entity = new TEntity();

            Type t = typeof(TEntity);

            if (requestModel != null)
            {
                Type c = requestModel.GetType();
                foreach (var propInfo in c.GetProperties())
                {
                    var attr = propInfo.GetCustomAttributes(true).OfType <ColumnDefinitionAttribute>().FirstOrDefault();
                    if (t.GetProperty(propInfo.Name) != null)
                    {
                        if (!t.GetProperty(propInfo.Name).PropertyType.IsAssignableTo <IBindingModel>())
                        {
                            t.GetProperty(propInfo.Name).SetValue(entity, propInfo.GetValue(requestModel, null), null);
                        }
                    }
                    else if (t.GetProperty(propInfo.Name) == null && propInfo.PropertyType != typeof(DropDownOptions) && propInfo.PropertyType != typeof(DropDownStringOptions) && attr != null && t.GetProperty(attr.EntityProperty) != null)
                    {
                        if (propInfo.PropertyType.IsArray)
                        {
                            Type nestedEntityType = Type.GetType(string.Format(System.Web.Configuration.WebConfigurationManager.AppSettings["DataAssemblyFormat"], attr.EntityProperty));
                            var  list             = Activator.CreateInstance(typeof(HashSet <>).MakeGenericType(nestedEntityType));
                            var  nestedEntity     = Activator.CreateInstance(nestedEntityType);

                            foreach (var obj in (IEnumerable)propInfo.GetValue(requestModel, null))
                            {
                                Type[] signature      = new[] { typeof(IBindingModel) };
                                var    methodInfo     = typeof(BaseEntity).GetMethod("ToEntity", signature);
                                var    toEntityMethod = methodInfo.MakeGenericMethod(new[] { nestedEntityType });

                                var nestedEntityObj = toEntityMethod.Invoke(nestedEntity, new object[] { obj });

                                var miAdd = list.GetType().GetMethod("Add");
                                miAdd.Invoke(list, new object[] { nestedEntityObj });
                            }

                            t.GetProperty(attr.EntityProperty).SetValue(entity, list, null);
                        }
                        else //!propInfo.PropertyType.IsArray
                        {
                            Type[] signature      = new[] { typeof(IBindingModel) };
                            var    methodInfo     = typeof(BaseEntity).GetMethod("ToEntity", signature);
                            var    toEntityMethod = methodInfo.MakeGenericMethod(new[] { Type.GetType(string.Format(System.Web.Configuration.WebConfigurationManager.AppSettings["DataAssemblyFormat"], attr.EntityProperty)) });

                            t.GetProperty(attr.EntityProperty).SetValue(entity, toEntityMethod.Invoke(entity, new object[] { propInfo.GetValue(requestModel, null) }), null);
                        }
                    }
                }

                return(entity);
            }

            return(default(TEntity));
        }
        public static void CopyProperties(this IEntity entity, IBindingModel requestModel)
        {
            Type t = entity.GetType();
            Type c = requestModel.GetType();

            foreach (var propInfo in t.GetProperties())
            {
                if (c.GetProperty(propInfo.Name) != null && !propInfo.Name.Equals("Id") && !propInfo.PropertyType.IsAssignableTo <IBindingModel>())
                {
                    propInfo.SetValue(entity, c.GetProperty(propInfo.Name).GetValue(requestModel, null), null);
                }
            }
        }
Beispiel #3
0
        public static TEntity ToEntityUpdate <TEntity>(IBindingModel requestModel) where TEntity : IEntity, new()
        {
            TEntity entity = new TEntity();

            Type t = typeof(TEntity);
            Type c = requestModel.GetType();

            foreach (var propInfo in c.GetProperties())
            {
                var attr = propInfo.GetCustomAttributes(true).OfType <ColumnDefinitionAttribute>().FirstOrDefault();
                if (t.GetProperty(propInfo.Name) != null)
                {
                    if (!t.GetProperty(propInfo.Name).PropertyType.IsAssignableTo <IBindingModel>())
                    {
                        t.GetProperty(propInfo.Name).SetValue(entity, propInfo.GetValue(requestModel, null), null);
                    }
                }
            }

            return(entity);
        }
        public static IBindingModel GetDiff(this IBindingModel source, IBindingModel target)
        {
            if (target == null)
            {
                return(source);
            }

            var metadata = EntityMetadata.GetMetadata(source.GetType());

            var result = (IBindingModel)Activator.CreateInstance(source.GetType());

            if (target.Id != Guid.Empty)
            {
                result.Id = target.Id;
            }
            else if (source.Id != Guid.Empty)
            {
                result.Id = source.Id;
            }

            foreach (var attribute in metadata.CrmAttributes.Where(a => a.CrmMapping.IsValidForUpdate))
            {
                if (source is BindingModelBase)
                {
                    if (!((BindingModelBase)source).InitializedProperties.Contains(attribute.PropertyName))
                    {
                        continue;
                    }
                }

                var valueSource = attribute.Property.GetValue(source);
                var valueTarget = attribute.Property.GetValue(target);


                switch (attribute.AttributeType)
                {
                case AttributeTypeCode.DateTime:
                    if (attribute.DateTimeBehavior == DateTimeBehavior.UserLocal)
                    {
                        valueSource = ((DateTime?)valueSource)?.ToUniversalTime();
                        valueTarget = ((DateTime?)valueTarget)?.ToUniversalTime();
                    }
                    else
                    {
                        valueSource = valueSource != null ? (DateTime?)new DateTime(((DateTime)valueSource).Ticks, DateTimeKind.Utc) : null;
                    }
                    break;

                case AttributeTypeCode.String:
                case AttributeTypeCode.Memo:
                    if (string.Empty == (valueSource as string))
                    {
                        valueSource = null;
                    }
                    break;
                }

                if ((!attribute.IsKey || result.Id != Guid.Empty) &&
                    (
                        (valueSource == null && valueTarget == null) ||
                        (valueSource != null && valueSource.Equals(valueTarget)) ||
                        (valueTarget != null && valueTarget.Equals(valueSource))
                    )
                    )
                {
                    continue;
                }

                attribute.Property.SetValue(result, valueSource);
            }

            return(result);
        }