Exemplo n.º 1
0
        public IHttpActionResult Get([FromUri] string customerName)
        {
            if (customerCache == null)
            {
                lock (cacheLockObject)
                {
                    if (customerCache == null)
                    {
                        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString))
                        {
                            connection.Open();

                            var addressTypePrimary = BasicSearchController.FetchMainOfficeAddressTypeID(connection);

                            CachingPocoSearchController.customerCache = connection.Query <CustomerResult>(
                                new SelectBuilder()
                            {
                                IncludeNameFilter = false
                            }.TransformText(),
                                new { AddressTypeID = addressTypePrimary })
                                                                        .ToArray();
                        }
                    }
                }
            }

            var customerNameUppercase = customerName.ToUpper();
            var tempResult            = CachingPocoSearchController.customerCache
                                        .Where(r => r.UpperFirstName.Contains(customerNameUppercase) || r.UpperLastName.Contains(customerNameUppercase));

            return(Ok(tempResult));
        }
Exemplo n.º 2
0
        public IHttpActionResult Get([FromUri] string customerName)
        {
            // Note double null-checking here. Reason: null-check is much faster than locking.
            if (customerCache == null)
            {
                lock (cacheLockObject)
                {
                    if (customerCache == null)
                    {
                        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString))
                        {
                            connection.Open();

                            var addressTypePrimary = BasicSearchController.FetchMainOfficeAddressTypeID(connection);

                            CachingSearchController.customerCache = new DataTable();
                            BasicSearchController.QueryCustomers(connection, customerName, addressTypePrimary, false, customerCache);
                        }
                    }
                }
            }

            // This approach uses an ADO.NET DataView to query the cache.
            //var view = new DataView(CachingSearchController.customerCache);
            //view.RowFilter = "LastName LIKE '%" + customerName + "%' OR FirstName LIKE '%" + customerName + "%'";
            //return Ok(CachingSearchController.ConvertToJson(view.Cast<DataRowView>(), (row, colName) => row[colName]));

            // This approach replaces ADO.NET DataView with (stupid) LINQ.
            var rows       = CachingSearchController.customerCache.Rows.Cast <DataRow>().ToArray();
            var tempResult = rows.Where(
                r => r["LastName"].ToString().ToUpper().Contains(customerName.ToUpper()) ||
                r["FirstName"].ToString().ToUpper().Contains(customerName.ToUpper())).ToArray();

            return(Ok(CachingSearchController.ConvertToJson(tempResult, (row, col) => row[col])));

            // And now with less stupid LINQ.
            //var customerNameUppercase = customerName.ToUpper();
            //var lastNameOrdinal = CachingSearchController.customerCache.Columns.IndexOf("UpperLastName");
            //var firstNameOrdinal = CachingSearchController.customerCache.Columns.IndexOf("UpperFirstName");
            //var tempResult = CachingSearchController.customerCache
            //	.Rows
            //	.Cast<DataRow>()
            //	.Where(r => r[lastNameOrdinal].ToString().Contains(customerNameUppercase)
            //			|| r[firstNameOrdinal].ToString().Contains(customerNameUppercase));
            //return Ok(CachingSearchController.ConvertToJson(tempResult, (row, col) => row[col]));
        }
Exemplo n.º 3
0
        public IHttpActionResult Get([FromUri] string customerName)
        {
            try
            {
                using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString))
                {
                    connection.Open();

                    var addressTypePrimary = BasicSearchController.FetchMainOfficeAddressTypeID(connection);

                    var result = new DataTable();
                    BasicSearchController.QueryCustomers(connection, customerName, addressTypePrimary, true, result);

                    var jsonResult = BasicSearchController.ConvertToJson(result.Rows.Cast <DataRow>());
                    return(Ok(jsonResult));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }