Пример #1
0
        public ICustomer GetCustomer(long customerId, bool preloadOrders)
        {
            using (var command = _commandFactory.CreateStoredProcedure("dbo.sp_GetCustomer"))
            {
                command.AddParameter("CustomerID", customerId);
                command.AddParameter("IncludeOrders", preloadOrders);

                using (var context = _contextFactory.Create("PerformanceTest"))
                {
                    if (preloadOrders)
                    {
                        // In this case the stored procedure returns two result sets, this
                        // requires slightly more code but is more efficient than calling
                        // the database twice.
                        using (var reader = context.ExecuteReader(command))
                        {
                            if (reader.Read())
                            {
                                var customer = _mapper.Map <Customer>(reader);
                                if (reader.NextResult())
                                {
                                    using (var orderEnumerator = _dataEnumeratorFactory.Create <Order>(reader))
                                        customer.Orders = orderEnumerator.Cast <IOrder>().ToList();
                                }
                                return(customer);
                            }
                        }
                    }
                    else
                    {
                        // In this case the stored procedure returns a single set of data
                        // and mapping it to the customer model is very straigtforward.
                        using (var customers = context.ExecuteEnumerable <Customer>(command))
                        {
                            return(customers.FirstOrDefault());
                        }
                    }
                }
            }
            return(null);
        }
Пример #2
0
        public IDataEnumerator <T> EndExecuteEnumerable <T>(IAsyncResult asyncResult, string dataSetName = null, IFactory <T> dataContractFactory = null) where T : class
        {
            var reader = EndExecuteReader(asyncResult);

            return(_dataEnumeratorFactory.Create(reader, reader.Dispose, dataSetName, dataContractFactory));
        }