Пример #1
0
        public void TestGetList()
        {
            SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString);

            List <Test> test = dataAccess.GetList("Test.GetListTest");

            Assert.IsNotNull(test);
            Assert.AreNotEqual(0, test.Count); //Fails if no records returned
            Assert.AreNotEqual(1, test.Count); //Fails if only 1 record returned
        }
        public void ProcessRequest(HttpContext context)
        {
            // Make sure new results are fetched every time
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            object     returnObject = null;
            ReturnData returnData;

            try
            {
                if (StoredProcedureName == string.Empty)
                {
                    throw new ArgumentException("Property StoredProcedureName must be set in handler");
                }

                if (PreExecution != null)
                {
                    PreExecutionResult result = PreExecution(context);
                    if (result != PreExecutionResult.NoErrors)
                    {
                        throw new PreExecutionFailException(result, "PreExecution Failed");
                    }
                }

                SqlDataAccessBasic <object, T> dataAccess = new SqlDataAccessBasic <object, T>(ConnectionString);

                returnData = new ReturnData
                {
                    List         = dataAccess.GetList(StoredProcedureName),
                    IsSuccessful = true
                };

                if (BusinessLogic != null)
                {
                    returnObject = BusinessLogic(returnData);
                }
                else
                {
                    returnObject = returnData;
                }
            }
            catch (Exception ex)
            {
                //The child class of this class should implement an ErrorHandling funciton.
                //Error message should be logged and a more user friendly error message should be sent in the string ErrorMessage
                //CallingMethod should be logged and cleared before being serialized into JSON
                returnData = new ReturnData
                {
                    ErrorMessage  = ex.ToString(),
                    CallingMethod = this.GetType().Name,
                    IsSuccessful  = false
                };

                if (ErrorHandling != null)
                {
                    returnObject = ErrorHandling(returnData, ex);
                }
                else
                {
                    returnObject = returnData;
                }
            }

            //Convert the results to JSON
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string JSON = jss.Serialize(returnObject);

            //Send JSON string back to browser
            context.Response.ContentType = "application/json";
            context.Response.Write(JSON);
        }