예제 #1
0
        public ICustomer GetCustomer(long customerId, bool preloadOrders)
        {
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PriusPerformanceTests"].ConnectionString))
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "dbo.sp_GetCustomer";

                    var customerIdParameter = new SqlParameter("@CustomerID", customerId);
                    command.Parameters.Add(customerIdParameter);

                    var includeOrdersParameter = new SqlParameter("@IncludeOrders", SqlDbType.Bit) {Value = 0};
                    command.Parameters.Add(includeOrdersParameter);

                    connection.Open();

                    var reader = command.ExecuteReader();
                    try
                    {
                        if (!reader.Read()) return null;

                        var customer = new Customer
                        {
                            CustomerId = Convert.ToInt64(reader["CustomerId"]),
                            GivenNames = reader["GivenNames"].ToString(),
                            FamilyName = reader["FamilyName"].ToString(),
                            DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]),
                            Email = reader["Email"].ToString()
                        };

                        if (preloadOrders)
                            customer.Orders = GetCustomerOrders(customerId);

                        return customer;
                    }
                    finally
                    {
                        reader.Close();
                    }
                }
            }
        }
예제 #2
0
        public IList<ICustomer> GetCustomers(Func<IEnumerable<ICustomer>, IEnumerable<ICustomer>> selector, bool preloadOrders)
        {
            var customers = new List<Customer>();

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PriusPerformanceTests"].ConnectionString))
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "dbo.sp_GetAllCustomers";

                    connection.Open();

                    var reader = command.ExecuteReader();
                    try
                    {
                        while (reader.Read())
                        {
                            var customer = new Customer
                            {
                                CustomerId = Convert.ToInt64(reader["CustomerId"]),
                                GivenNames = reader["GivenNames"].ToString(),
                                FamilyName = reader["FamilyName"].ToString(),
                                DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]),
                                Email = reader["Email"].ToString()
                            };

                            customers.Add(customer);
                        }
                    }
                    finally
                    {
                        reader.Close();
                    }
                }
            }

            var filteredCustomers = selector(customers).ToList();

            if (!preloadOrders)
                return filteredCustomers;

            foreach (var customer in filteredCustomers.Cast<Customer>())
                customer.Orders = GetCustomerOrders(customer.CustomerId);

            return filteredCustomers;
        }