Ejemplo n.º 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);
        }
Ejemplo n.º 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", "" }
            }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }