コード例 #1
0
        private bool Save <TEntity>(TEntity entity, string keyName, bool autoIncrement = true) where TEntity : class
        {
            bool result;
            var  key = Insert(entity, autoIncrement);

            if ((ImplementationUtil.ContainsProperty(entity.GetType(), keyName) && autoIncrement))
            {
                MapperUtil.SetValue(entity, keyName, key);
            }
            result = key > 0;
            return(result);
        }
コード例 #2
0
        private bool IsUpdate <TEntity>(TEntity entity, string keyName) where TEntity : class
        {
            var isExists        = false;
            var entitiIdPattern = $"{typeof(TEntity).Name}Id";

            if (ImplementationUtil.ContainsInterface(entity.GetType(), nameof(IEntityState)))
            {
                isExists = ((IEntityState)entity).IsExists;
            }
            else if (ImplementationUtil.ContainsProperty(entity.GetType(), keyName))
            {
                isExists = !new string[] { "0", "" }
            }
コード例 #3
0
        internal static void NavigationMapping <TEntity>(TEntity entity)
            where TEntity : class
        {
            var type = typeof(TEntity);
            var name = type.Name;

            foreach (var prop in type.GetProperties().Where(p => p.PropertyType.Namespace != _systemNamespace))
            {
                try
                {
                    if (ImplementationUtil.ContainsInterface(prop.PropertyType, nameof(IEnumerable)))
                    {
                        var array = prop.GetValue(entity);
                        if (array != null)
                        {
                            foreach (var obj in array as IEnumerable)
                            {
                                try
                                {
                                    if (obj != null && obj.GetType().GetProperty(name) != null)
                                    {
                                        MapperUtil.SetValue(obj, name, entity);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                        }
                    }
                    else if (prop.PropertyType.IsClass)
                    {
                        var obj = prop.GetValue(entity);
                        if (obj != null && obj.GetType().GetProperty(name) != null)
                        {
                            MapperUtil.SetValue(obj, name, entity);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
コード例 #4
0
        private static bool Exists <TEntity>(TEntity entity, string keyName) where TEntity : class
        {
            var isExists = false;

            if (ImplementationUtil.ContainsInterface(entity.GetType(), nameof(IEntityState)))
            {
                isExists = ((IEntityState)entity).IsExists;
            }
            else if (ImplementationUtil.ContainsProperty(entity.GetType(), "Id"))
            {
                isExists = (entity as dynamic).Id > 0;
            }
            else if (ImplementationUtil.ContainsProperty(entity.GetType(), keyName))
            {
                isExists = MapperUtil.GetValue(entity, keyName).ToString() != "0";
            }
            return(isExists);
        }
コード例 #5
0
        public bool SetComposition <TEntity>(ICollection <TEntity> collection, string propertyName, long?propertyValue, bool autoIncrement)
            where TEntity : class
        {
            var result       = new List <TEntity>();
            var resultStatus = true;

            if (collection != null)
            {
                foreach (var entity in collection)
                {
                    try
                    {
                        if ((ImplementationUtil.ContainsProperty(entity.GetType(), propertyName) && 0 == (long)MapperUtil.GetValue(entity, propertyName)) ||
                            (ImplementationUtil.ContainsInterface(entity.GetType(), nameof(IEntityState)) && !((IEntityState)entity).IsExists))
                        {
                            if (propertyValue.HasValue && !string.IsNullOrWhiteSpace(propertyName))
                            {
                                MapperUtil.SetValue(entity, propertyName, propertyValue);
                            }
                            if (Insert(entity, autoIncrement) > 0)
                            {
                                result.Add(entity);
                            }
                        }
                        else if (((IAssociationOfData)entity).IsKeep && Update(entity))
                        {
                            result.Add(entity);
                        }
                        else
                        {
                            Delete(entity);
                        }
                    }
                    catch (SQLException ex)
                    {
                        resultStatus = false;
                        throw ex;
                    }
                }
            }

            collection = result;
            return(resultStatus);
        }
コード例 #6
0
        public bool SetComposition <TEntity>(ICollection <TEntity> collection)
            where TEntity : class
        {
            var result       = new List <TEntity>();
            var resultStatus = true;

            string[]    keys          = null;
            List <Type> keyType       = null;
            bool?       autoIncrement = null;

            if (collection != null)
            {
                foreach (var entity in collection)
                {
                    try
                    {
                        Mapper.BindForeign(entity);
                        if (!autoIncrement.HasValue)
                        {
                            autoIncrement = Mapper.IsAutoIncrement(entity);
                        }
                        if (keys == null)
                        {
                            keys    = Mapper.GetKeyName(entity).Split('-');
                            keyType = new List <Type>();
                            foreach (var key in keys)
                            {
                                keyType.Add(entity.GetType().GetProperty(key).PropertyType);
                            }
                        }
                        if (Mapper.CheckIfIsNew(keys[0], entity) || (ImplementationUtil.ContainsInterface(entity.GetType(), nameof(IEntityState)) && !((IEntityState)entity).IsExists))
                        {
                            var value = Insert(entity, autoIncrement.Value);
                            if (autoIncrement.Value)
                            {
                                MapperUtil.SetValue(entity, keys[0], Convert.ChangeType(value, keyType[0]));
                            }
                            if (value > 0)
                            {
                                result.Add(entity);
                            }
                        }
                        else if ((ImplementationUtil.ContainsInterface(entity.GetType(), nameof(IAssociationOfData)) && ((IAssociationOfData)entity).IsKeep))
                        {
                            if (Update(entity))
                            {
                                result.Add(entity);
                            }
                        }
                        else
                        {
                            Delete(entity);
                        }
                    }
                    catch (SQLException)
                    {
                        resultStatus = false;
                    }
                    catch (Exception)
                    {
                        resultStatus = false;
                    }
                }
            }
            collection = result;
            return(resultStatus);
        }