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

            Test test = dataAccess.GetById(1, "Test.GetObjectByIdTest");

            Assert.IsNotNull(test);
        }
Пример #2
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
        }
Пример #3
0
        public void TestUpdateById()
        {
            SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString);

            Test updatetData = new Test
            {
                TestDate   = System.DateTime.Now,
                TestInt    = 1,
                TestString = "String"
            };

            dataAccess.Update(1, updatetData, "Test.UpdateByIdTest");
        }
Пример #4
0
        public void TestInsert()
        {
            SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString);

            Test insertData = new Test
            {
                TestDate   = System.DateTime.Now,
                TestInt    = 1,
                TestString = "String"
            };

            int id = dataAccess.Insert(insertData, "Test.InsertTest");
        }
        public void ProcessRequest(HttpContext context)
        {
            object     returnObject = null;
            ReturnData returnData   = new ReturnData();

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

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

                // Get the JSON that was posted
                string json;
                using (Stream st = context.Request.InputStream)
                {
                    byte[] buf   = new byte[context.Request.InputStream.Length];
                    int    iRead = st.Read(buf, 0, buf.Length);
                    json = Encoding.UTF8.GetString(buf);
                }

                //Deserialize the Javascript
                JavaScriptSerializer        ser      = new JavaScriptSerializer();
                Dictionary <string, object> ajaxData = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object> >(json);

                TID id = (TID)Convert.ChangeType(ajaxData["Id"], typeof(TID));



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

                dataAccess.IdCall(id, StoredProcedureName);
                returnData.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);
        }
        public void ProcessRequest(HttpContext context)
        {
            object     returnObject = null;
            ReturnData returnData   = new ReturnData();

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

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

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

                // Get the JSON that was posted
                string json;
                using (Stream st = context.Request.InputStream)
                {
                    byte[] buf   = new byte[context.Request.InputStream.Length];
                    int    iRead = st.Read(buf, 0, buf.Length);
                    json = Encoding.UTF8.GetString(buf);
                }

                //Deserialize the Javascript
                JavaScriptSerializer        ser      = new JavaScriptSerializer();
                Dictionary <string, object> ajaxData = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object> >(json);
                T obj = (T)Activator.CreateInstance(typeof(T), new object[] { });

                //Convert data posted from Javascript to an object of Type T that will be passed into the dataaccess insert method
                foreach (var prop in typeof(T).GetProperties())
                {
                    obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ChangeType(ajaxData[prop.Name], prop.PropertyType));
                }

                SqlDataAccessBasic <TID, T>    dataAccess  = new SqlDataAccessBasic <TID, T>(ConnectionString);
                SqlDataAccessBasic <object, B> dataAccess2 = new SqlDataAccessBasic <object, B>(ConnectionString);
                TID id = dataAccess.Insert(obj, InsertStoredProcedureName);
                returnData.Value        = dataAccess2.GetById(id, GetByIdStoredProcedureName);
                returnData.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);
        }
        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);
        }
Пример #8
0
        public void TestNoParameterCall()
        {
            SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString);

            dataAccess.NoParameterCall("Test.NoParameterCallTest");
        }
Пример #9
0
        public void TestIdCall()
        {
            SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString);

            dataAccess.IdCall(1, "Test.IdCallTest");
        }