Ejemplo n.º 1
0
        /// <summary>
        /// Converts an entity into a business entitity.
        /// </summary>
        /// <typeparam name="TSource">The Entity type.</typeparam>
        /// <typeparam name="TResult">The business entity type.</typeparam>
        /// <param name="source">The entity to convert.</param>
        /// <returns>A converted business entity.</returns>
        public static TResult Convert <TSource, TResult>(TSource source) where TResult : class, new()
        {
            if (source == null)
            {
                return(null);
            }

            Type    sourceType = typeof(TSource);
            Type    targetType = typeof(TResult);
            TResult result     = null;

            if (sourceType.IsSubclassOf(typeof(DbEntity)) &&
                targetType.IsSubclassOf(typeof(BusinessEntity)))
            {
                var idprop = sourceType.GetProperty("Id");
                if (idprop != null && idprop.PropertyType == typeof(Int32))
                {
                    var id = System.Convert.ToInt32(idprop.GetValue(source, null));
                    result = BaseUserContext.TryGetDynamicInstance(targetType, id) as TResult;
                }
            }

            if (result == null)
            {
                result = new TResult();
                if (result is BusinessEntity)
                {
                    BaseUserContext.StoreDynamicInstance(targetType, result as BusinessEntity);
                }
            }
            result.InjectFrom <CustomFlatLoopValueInjection>(source);
            result.ReadJsonData(source);
            Cryptography.DecryptProperties(result);
            var businessEntity = result as BusinessEntity;
            var dbEntity       = source as DbEntity;

            if (businessEntity != null && dbEntity != null)
            {
                dbEntity.BusinessEntity = businessEntity;
                businessEntity.Init();
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets an object on which to do a safe lock() unto to prevent simultaneous writes on the same item.
        /// </summary>
        /// <param name="key">The lock key.</param>
        /// <returns>An object on which to lock() to.</returns>
        public static object Get(string key)
        {
            lock (AccessLock)
            {
                var cacheKey    = "Lock?key=" + key;
                var cache       = BaseUserContext.GetDataCache();
                var currentLock = cache.Get(cacheKey) as Lock;

                if (currentLock == null)
                {
                    currentLock = new Lock();
                    cache.Add(cacheKey, currentLock,
                              new CacheItemPolicy
                    {
                        SlidingExpiration = new TimeSpan(0, 15, 0),
                        Priority          = CacheItemPriority.NotRemovable
                    });
                }

                return(currentLock._lockObject);
            }
        }
        protected override void Inject(object source, object target)
        {
            if (source == null)
            {
                return;
            }

            foreach (PropertyDescriptor targetProperty in target.GetProps())
            {
                var t1 = targetProperty;
                var es = UberFlatter.Flat(targetProperty.Name, source, type => TypesMatch(type, t1.PropertyType));

                var sourceType = source.GetType();

                if (!es.Any() && !sourceType.IsPrimitive && targetProperty.PropertyType != typeof(String) && !targetProperty.PropertyType.IsInterface)
                {
                    var sourceProperty = source.GetType().GetProperty(targetProperty.Name);
                    if (sourceProperty != null)
                    {
                        var sourceValue = sourceProperty.GetValue(source, null);
                        if (sourceValue != null)
                        {
                            object targetValue = null;
                            if (sourceProperty.PropertyType.IsSubclassOf(typeof(DbEntity)) &&
                                targetProperty.PropertyType.IsSubclassOf(typeof(BusinessEntity)))
                            {
                                var idprop = sourceProperty.PropertyType.GetProperty("Id");
                                if (idprop != null && idprop.PropertyType == typeof(Int32))
                                {
                                    targetValue = BaseUserContext.TryGetDynamicInstance(targetProperty.PropertyType,
                                                                                        Convert.ToInt32(idprop.GetValue(sourceValue, null)));
                                }
                            }
                            if (targetValue == null)
                            {
                                targetValue = Activator.CreateInstance(targetProperty.PropertyType);
                                if (targetValue != null)
                                {
                                    Inject(sourceValue, targetValue);
                                }
                                if (targetValue is BusinessEntity)
                                {
                                    BaseUserContext.StoreDynamicInstance(targetProperty.PropertyType, targetValue as BusinessEntity);
                                }
                            }
                            targetProperty.SetValue(target, targetValue);
                        }
                    }
                }

                Debug.Assert(es != null, "es != null");
                var endpoint = es.FirstOrDefault(el => el != null);
                if (endpoint == null)
                {
                    continue;
                }
                var val = endpoint.Property.GetValue(endpoint.Component);

                if (AllowSetValue(val))
                {
                    targetProperty.SetValue(target, SetValue(val));
                }
            }
        }