예제 #1
0
 private void SetDefaultValues(T model)
 {
     if (((model as IChildEmpresaObject) != null) && EmpresaId.HasValue)
     {
         var empresaProp = ClassManipulation.GetColumn <T>("EmpresaId");
         if (empresaProp != null)
         {
             empresaProp.SetValue(model, EmpresaId.Value);
         }
     }
     if (((model as IChildUsuarioObject) != null) && UsuarioId > 0)
     {
         var usuarioProp = ClassManipulation.GetColumn <T>("UsuarioId");
         if (usuarioProp != null)
         {
             usuarioProp.SetValue(model, UsuarioId);
         }
     }
 }
예제 #2
0
        public List <T> List(FilterObject <T> filter)
        {
            var    model = filter.Model;
            string sql   = $@"
                Select * 
                  From {ClassManipulation.GetTableName<T>()}";

            IConnection conn = ConnectionFactory.GetConnection();

            var command = conn.Db.CreateCommand();

            if (model != null && filter != null && filter.Properties.Count > 0)
            {
                sql += " Where ";
                for (var i = 0; i < filter.Properties.Count; i++)
                {
                    var prop = filter.Properties[i];

                    sql += prop.LogicalOperator;

                    var property = ClassManipulation.GetColumn <T>(prop.Property);
                    sql += property.Name + " "
                           + prop.Operator + " "
                           + ConnectionFactory.SqlParameter + property.Name;

                    var param = command.CreateParameter();
                    param.ParameterName = property.Name;

                    if (prop.OperatorEnum == OperatorEnum.Like)
                    {
                        param.Value = prop.PreAppend + property.GetValue(model) + prop.PosAppend;
                    }
                    else
                    {
                        param.Value = property.GetValue(model);
                    }

                    command.Parameters.Add(param);
                }
                if (typeof(T) is IChildEmpresaObject)
                {
                    if (!EmpresaId.HasValue)
                    {
                        throw new BrokenRulesException("Não foi informado o código da empresa logada");
                    }
                    sql += $" And EmpresaId == {EmpresaId.Value}";
                }
            }
            else
            {
                if (typeof(T) is IChildEmpresaObject)
                {
                    if (!EmpresaId.HasValue)
                    {
                        throw new BrokenRulesException("Não foi informado o código da empresa logada");
                    }
                    sql += $" Where EmpresaId == {EmpresaId.Value}";
                }
            }

            command.CommandText = sql;

            using (DbDataReader reader = command.ExecuteReader())
                return(DataReaderManipulation.DataReaderMapToList <T>(reader));
        }