Ejemplo n.º 1
0
        public T TransformFromAzureObject <T>(DynamicPersistentEntity persistantEntity)
        {
            var obj        = Activator.CreateInstance <T>();
            var properties = obj.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                var attrs = property.GetCustomAttributes(false);
                if (attrs.Any(attr => String.Equals(attr.GetType().Name, "IgnoreProperty", StringComparison.Ordinal)))
                {
                    continue;
                }

                var value = ((EntityProperty)persistantEntity[property.Name]).GetValue();
                if (value == null || string.IsNullOrEmpty(value.ToString()))
                {
                    continue;
                }

                //special handling for conversion back to decimal...ATS does not support decimals.
                if (property.PropertyType == typeof(decimal) || property.PropertyType == typeof(decimal?))
                {
                    decimal decimalValue;
                    try
                    {
                        decimalValue = Convert.ToDecimal(value);
                    }
                    catch (OverflowException)
                    {
                        try
                        {
                            decimalValue = Convert.ToDecimal(Math.Round((double)value, 4));
                        }
                        catch (OverflowException)
                        {
                            decimalValue = Convert.ToDecimal(Math.Round((double)value, 2));
                        }
                    }
                    property.SetValue(obj, decimalValue);
                    continue;
                }
                property.SetValue(obj, value);
            }
            //PartitionKey and Id are marked as IgnoreProperties becaue they are used as Partition Key and Row Key, need to add them back in manually.
            if (obj as PersistentEntity != null)
            {
                (obj as PersistentEntity).PartitionKey = persistantEntity.PartitionKey;
                (obj as PersistentEntity).Id           = new Guid(persistantEntity.RowKey);
            }
            return(obj);
        }
Ejemplo n.º 2
0
        //   public AzureEntityManager EntityManager { get; set; }
        public DynamicPersistentEntity TransformToAzureObject(PersistentEntity obj)
        {
            dynamic entity     = new DynamicPersistentEntity();
            var     properties = obj.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttributes <AzureCollectionAttribute>().Any());

            foreach (var property in properties)
            {
                var list = property.GetValue(obj) as IList;
                if (list == null)
                {
                    continue;
                }
                var attribute = property.GetCustomAttributes <AzureCollectionAttribute>().FirstOrDefault();
            }
            return(entity);
        }
Ejemplo n.º 3
0
        public DynamicPersistentEntity TransformToAzureObject(PersistentEntity obj)
        {
            dynamic entity     = new DynamicPersistentEntity();
            var     properties = obj.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);

            entity.PartitionKey        = obj.PartitionKey;
            entity.RowKey              = obj.Id.ToString();
            entity["LastModifiedDate"] = DateTime.UtcNow;

            var userName = Thread.CurrentPrincipal.Identity.Name;

            entity["LastModifiedBy"] = userName;

            foreach (var property in properties)
            {
                var attrs = property.GetCustomAttributes(false);
                if (!attrs.Any(attr => String.Equals(attr.GetType().Name, "IgnoreProperty", StringComparison.Ordinal)))
                {
                    var value = property.GetValue(obj);
                    entity[property.Name] = value;
                    //DateTime.MinValue causes 400...if we have an uninitialized date set it to earliest ATS can handle.
                    if (value is DateTime && value.Equals(DateTime.MinValue))
                    {
                        entity[property.Name] = new DateTime(1601, 1, 1, 0, 0, 0);
                    }
                }
            }

            var createdDate = properties.First(p => String.Equals(p.Name, "CreatedDate", StringComparison.Ordinal));

            if (createdDate.GetValue(obj).Equals(DateTime.MinValue))
            {
                entity[createdDate.Name] = DateTime.UtcNow;
            }

            var createdBy = properties.First(p => String.Equals(p.Name, "CreatedBy", StringComparison.Ordinal));

            if (createdBy.GetValue(obj) == null || string.IsNullOrEmpty(createdBy.GetValue(obj).ToString()))
            {
                entity[createdBy.Name] = userName;
            }

            return(entity);
        }
Ejemplo n.º 4
0
        public T TransformFromAzureObject <T>(DynamicPersistentEntity persistantEntity)
        {
            var obj        = Activator.CreateInstance <T>();
            var properties = obj.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                var value = ((EntityProperty)persistantEntity[property.Name]).GetValue();
                if (value == null || string.IsNullOrEmpty(value.ToString()))
                {
                    continue;
                }

                property.SetValue(obj,
                                  property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(IList <>)
                                      ? JSONStringToCollection(property.PropertyType, (string)value)
                                      : value);
            }
            return(obj);
        }
Ejemplo n.º 5
0
        public DynamicPersistentEntity TransformToAzureObject(PersistentEntity obj)
        {
            dynamic entity     = new DynamicPersistentEntity();
            var     properties = obj.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);

            entity.PartitionKey = obj.PartitionKey;
            entity.RowKey       = obj.Id;

            foreach (var property in properties)
            {
                var list = property.GetValue(obj) as IList;
                if (list == null)
                {
                    entity[property.Name] = property.GetValue(obj);
                }
                else
                {
                    entity[property.Name] = CollectionToJSONString(list);
                }
            }
            return(entity);
        }
Ejemplo n.º 6
0
 public T TransformFromAzureObject <T>(DynamicPersistentEntity obj)
 {
     throw new NotImplementedException();
 }