Beispiel #1
0
        public IList <A> BuildObject <A, B>(VO.IParametros pParametros, VO.IOrdinal pOrdinal)
            where A : VO.IVO, new()
            where B : IAbstractDal, new()
        {
            B lObject = default(B);

            try
            {
                IList <A> objRetorno = null;

                lObject = PrepareObjectForOutput <A, B>(pParametros, pOrdinal);

                CommandText = lObject.Query.ToString();
                CommandType = CommandType.Text;
                ExecuteReader();

                objRetorno = new List <A>();
                while (Reader.Read() && lObject.GetData(Reader, objRetorno))
                {
                    ;
                }

                if (!Reader.IsClosed)
                {
                    Reader.Close();
                }

                if (lObject.Query.Paginate.Enabled)
                {
                    if (Parameter("@totalRows").Value != DBNull.Value)
                    {
                        lObject.Query.Paginate.TotalLinhas = (long)Parameter("@totalRows").Value;
                    }

                    Paginate = lObject.Query.Paginate;
                }

                Conexao.Command.Parameters.Clear();

                return(objRetorno);
            }
            catch (DALException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (lObject != null)
                {
                    throw new DALException(ex.Message + "\r\n\r\nQuery:" + lObject.Query.ToString(), "TransactionControler", "BuildObject", "Não foi possível materializar o(s) objeto(s).", ex);
                }
                else
                {
                    throw new DALException(ex.Message, "TransactionControler", "BuildObject", "Não foi possível materializar o(s) objeto(s).", ex);
                }
            }
        }
Beispiel #2
0
        public B PrepareObjectForOutput <A, B>(VO.IParametros pParametros, VO.IOrdinal pOrdinal)
            where A : VO.IVO, new()
            where B : IAbstractDal, new()
        {
            B lDal = new B();

            try
            {
                if (pOrdinal.Fields.All() == 0)
                {
                    throw new DALException("Não foram encontrados campos selecionados para a consulta.", MethodBase.GetCurrentMethod().DeclaringType.Name,
                                           MethodBase.GetCurrentMethod().Name, "Não foi possível preparar o objeto.", null);
                }

                lDal.Ordinal = pOrdinal;
                lDal.Query.AddEntityFrom(
                    lDal.Database,
                    lDal.Schema,
                    lDal.Entity);

                lDal.EvaluateParameters(pParametros);

                lDal.GetOrdinal();

                lDal.Query.Top           = pParametros.Top;
                lDal.Query.OrderBy       = pParametros.OrderBy;
                lDal.Query.Paginate      = pParametros.Paginate;
                lDal.Query.EnableGroupBy = pParametros.EnableGroup;

                lDal.Query.Parametros.BuildWhereDefaults(lDal, pParametros);

                if (pParametros.OrderBy.Length == 0 && !string.IsNullOrEmpty(lDal.EntityOrderField))
                {
                    lDal.Query.OrderBy = new string[] { string.Format("[{0}].[{1}]", lDal.EntityAlias, lDal.EntityOrderField) }
                }
                ;

                lDal.Query.BuildQuery();

                foreach (var lPar in lDal.Query.Parametros.QueryParameters)
                {
                    AddParameter(lPar.Key, lPar.Value);
                }

                if (lDal.Query.Paginate.Enabled)
                {
                    if (lDal.Query.Paginate.Pagina == 0)
                    {
                        lDal.Query.Paginate.Pagina = 1;
                    }

                    if (lDal.Query.Paginate.LinhasPorPagina == 0)
                    {
                        lDal.Query.Paginate.LinhasPorPagina = 10;
                    }

                    AddOutputParameter("@totalRows", default(long));
                    AddParameter("@PageNumber", lDal.Query.Paginate.Pagina);
                    AddParameter("@RowspPage", lDal.Query.Paginate.LinhasPorPagina);
                }

                return(lDal);
            }
            catch (DALException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, "TransactionControler", "PrepareObjectForOutput", "Não foi possível preparar o objeto.", ex);
            }
        }