コード例 #1
0
		public Order GetById(int orderId)
		{
			Order order = null;

			var gmp = new GetMultiplePredicate();
			gmp.Add<Order>(Predicates.Field<Order>(x => x.OrderID, Operator.Eq, orderId));
			gmp.Add<OrderLine>(Predicates.Field<OrderLine>(x => x.OrderID, Operator.Eq, orderId));

			using (var conn = _connectionFactory())
			{
				var reader = conn.GetMultiple(gmp);

				order = reader.Read<Order>().SingleOrDefault();

				if (order != null)
				{
					//CoreObjectShim.SetReadOnlyProperty<Order>(x => x.Lines, reader.Read<OrderLine>().ToList());
					var setter = MemberShim.GetReadOnlySetter<Order, List<OrderLine>>(x => x.Lines);
					setter(order, reader.Read<OrderLine>().ToList());
				}
			}

			return order;
		}
コード例 #2
0
            public void ReturnsItems()
            {
                Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow.AddDays(-10) });
                Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow.AddDays(-10) });
                Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow.AddDays(-3) });
                Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow.AddDays(-1) });

                Db.Insert(new Animal { Name = "Foo" });
                Db.Insert(new Animal { Name = "Bar" });
                Db.Insert(new Animal { Name = "Baz" });

                GetMultiplePredicate predicate = new GetMultiplePredicate();
                predicate.Add<Person>(null);
                predicate.Add<Animal>(Predicates.Field<Animal>(a => a.Name, Operator.Like, "Ba%"));
                predicate.Add<Person>(Predicates.Field<Person>(a => a.LastName, Operator.Eq, "c1"));

                var result = Db.GetMultiple(predicate);
                var people = result.Read<Person>().ToList();
                var animals = result.Read<Animal>().ToList();
                var people2 = result.Read<Person>().ToList();

                Assert.AreEqual(4, people.Count);
                Assert.AreEqual(2, animals.Count);
                Assert.AreEqual(1, people2.Count);
            }
        protected SequenceReaderResultReader GetMultipleBySequence(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int?commandTimeout)
        {
            IList <SqlMapper.GridReader> items = new List <SqlMapper.GridReader>();

            foreach (var item in predicate.Items)
            {
                Dictionary <string, object> parameters = new Dictionary <string, object>();
                IClassMapper classMap      = SqlGenerator.Configuration.GetMap(item.Type);
                IPredicate   itemPredicate = item.Value as IPredicate;
                if (itemPredicate == null && item.Value != null)
                {
                    itemPredicate = GetPredicate(classMap, item.Value);
                }

                string            sql = SqlGenerator.Select(classMap, itemPredicate, item.Sort, parameters);
                DynamicParameters dynamicParameters = new DynamicParameters();
                foreach (var parameter in parameters)
                {
                    dynamicParameters.Add(parameter.Key, parameter.Value);
                }

                SqlMapper.GridReader queryResult = connection.QueryMultiple(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text);
                items.Add(queryResult);
            }

            return(new SequenceReaderResultReader(items));
        }
        protected GridReaderResultReader GetMultipleByBatch(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int?commandTimeout)
        {
            Dictionary <string, object> parameters = new Dictionary <string, object>();
            StringBuilder sql = new StringBuilder();

            foreach (var item in predicate.Items)
            {
                IClassMapper classMap      = SqlGenerator.Configuration.GetMap(item.Type);
                IPredicate   itemPredicate = item.Value as IPredicate;
                if (itemPredicate == null && item.Value != null)
                {
                    itemPredicate = GetPredicate(classMap, item.Value);
                }

                sql.AppendLine(SqlGenerator.Select(classMap, itemPredicate, item.Sort, parameters) + SqlGenerator.Configuration.Dialect.BatchSeperator);
            }

            DynamicParameters dynamicParameters = new DynamicParameters();

            foreach (var parameter in parameters)
            {
                dynamicParameters.Add(parameter.Key, parameter.Value);
            }

            SqlMapper.GridReader grid = connection.QueryMultiple(sql.ToString(), dynamicParameters, transaction, commandTimeout, CommandType.Text);
            return(new GridReaderResultReader(grid));
        }