Ejemplo n.º 1
0
        public static IEnumerable <T> Read <T>(this IHop hopper, string whereClause) where T : new()
        {
            string cmdText = null;

            try
            {
                cmdText = string.Format("{0} WHERE {1}", SelectFrom <T>(), whereClause);

                using (var sqlCommand = new SqlCommand(cmdText))
                {
                    return(hopper.ReadInternal <T>(sqlCommand));
                }
            }
            catch (SqlException sqlException)
            {
                throw new HopWhereClauseParseException(cmdText, sqlException);
            }
        }
Ejemplo n.º 2
0
        public static IEnumerable <T> Read <T>(this IHop hopper, IEnumerable <T> instances) where T : new()
        {
            if (instances == null)
            {
                throw new ArgumentNullException("instances", "Read extension method expects an array of instances to read");
            }

            var listInstances = instances as List <T> ?? instances.ToList();

            if (!listInstances.Any())
            {
                return(Enumerable.Empty <T>());
            }

            var idExtractorService = HopBase.GetIdExtractorService();

            var ids = idExtractorService.GetIds(listInstances).Select((x, i) =>
            {
                if (HopBase.GetDefault(x.GetType()).Equals(x))
                {
                    throw new HopReadWithoutKeyException(listInstances[i]);
                }

                return(new SqlParameter(string.Format("@param{0}", i), x));
            }).ToArray();

            var idField     = idExtractorService.GetIdField <T>();
            var whereClause = ids.Select(p => p.ParameterName).Aggregate((p1, p2) => p1 + " , " + p2);
            var selectFrom  = SelectFrom <T>();

            var cmdText = string.Format("{0} WHERE {2} IN ( {1} )", selectFrom, whereClause, idField);

            using (var command = new SqlCommand())
            {
                command.CommandText = cmdText;
                command.Parameters.AddRange(ids);

                return(hopper.ReadInternal <T>(command));
            }
        }
Ejemplo n.º 3
0
 public static IEnumerable <T> ReadAll <T>(this IHop hopper) where T : new()
 {
     return(hopper.ReadInternal <T>(new SqlCommand(SelectFrom <T>())));
 }