Beispiel #1
0
        public void TestMethod1()
        {
            var scopeFactory = new ContextExtension("bak");

            using (var scope = scopeFactory.GetContextScope())
            {
                var db = scopeFactory.GetDbContext <CommonDbContext>();
                using (var dbContextScope = scopeFactory.GetContextScope())
                {
                    db.Users.Add(new User()
                    {
                        Id = Guid.NewGuid(), Name = "Marshall1", Email = "[email protected]",
                        WelcomeEmailSent = false,
                        CreatedOn        = DateTime.UtcNow
                    });
                    dbContextScope.SaveChanges();
                }

                using (var dbContextScope = scopeFactory.GetContextScope())
                {
                    db.Users.FirstOrDefault().Name = "bababab1";
                    dbContextScope.SaveChanges();
                }

                using (var dbContextScope = scopeFactory.GetContextScope())
                {
                    db.Users.FirstOrDefault().Email = "dddd1";
                    dbContextScope.SaveChanges();
                }

                scope.SaveChanges();
            }
        }
Beispiel #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int     allRecordCount = 0;
            string  sortname       = context.Request.Params["sortname"];
            string  sortorder      = context.Request.Params["sortorder"];
            int     page           = Convert.ToInt32(context.Request.Params["page"]) - 1; // 系统的索引从0开始,所以此处需要减1
            int     pagesize       = Convert.ToInt32(context.Request.Params["pagesize"]);
            string  fromWhere      = context.Request.QueryString["fromwhere"];
            string  qylx           = context.Request.QueryString["qylx"];
            string  qyId           = context.Request.QueryString["qyid"];
            string  rowid          = context.Request.QueryString["rowid"];
            string  befrom         = context.Request.QueryString["befrom"];
            string  dwlx           = context.Request.QueryString["dwlx"];
            object  sessionAppUser = context.Session[ConfigManager.GetSignInAppUserSessionName()];
            AppUser workUser       = (AppUser)sessionAppUser;

            BLL = new SzqyBLL(workUser);

            //排序
            string orderby = @" " + sortname + " " + sortorder + " ";
            //通过检索翻译 生成查询条件
            FilterTranslator ft = ContextExtension.GetGridData(context);
            //分页
            DataTable dt = new DataTable();

            switch (fromWhere)
            {
            case "Qyxx":
                dt = BLL.RetrieveQyxxList(qylx, ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "JsdwSsgc":
                string jsdwID = context.Request.QueryString["jsdwid"];
                dt = BLL.RetrieveJsdwSsgc(jsdwID).Result;
                break;

            case "Qyclgc":
                dt = BLL.RetrieveQyclgc(qyId, befrom, dwlx).Result;
                break;

            case "Qyzs":
                dt = BLL.RetrieveQyzs(qyId, befrom).Result;
                break;

            case "Zyry":
                dt = BLL.RetrieveZyry(qyId, ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "QyxxView":
                dt = BLL.RetrieveQyxxViewList(qyId).Result;
                break;
            }
            ft.Parms.Clear();
            string result = JSONHelper.DataTableToJson(dt);

            json = @"{""Rows"":[" + result + @"],""Total"":""" + allRecordCount + @"""}";
            context.Response.Write(json);
            context.Response.End();
        }
Beispiel #3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int     allRecordCount = 0;
            string  sortname       = context.Request.Params["sortname"];
            string  sortorder      = context.Request.Params["sortorder"];
            int     page           = Convert.ToInt32(context.Request.Params["page"]) - 1; // 系统的索引从0开始,所以此处需要减1
            int     pagesize       = Convert.ToInt32(context.Request.Params["pagesize"]);
            string  fromWhere      = context.Request.QueryString["fromwhere"];
            string  rowid          = context.Request.QueryString["rowid"];
            object  sessionAppUser = context.Session[ConfigManager.GetSignInAppUserSessionName()];
            AppUser workUser       = (AppUser)sessionAppUser;

            xmxxBLL = new XmxxBLL(workUser);

            //排序
            string orderby = @" " + sortname + " " + sortorder + " ";
            //通过检索翻译 生成查询条件
            FilterTranslator ft = ContextExtension.GetGridData(context);
            //分页
            DataTable dt   = new DataTable();
            string    json = String.Empty;

            switch (fromWhere)
            {
            case "ajxxList":
                dt = xmxxBLL.RetrieveAjxxList(ft, pagesize, page, orderby, out allRecordCount);
                break;


            case "zjxxList":
                dt = xmxxBLL.RetrieveZjxxList(ft, pagesize, page, orderby, out allRecordCount);
                break;

            case "ajxx_ryxxList":
                string aqjdbm = context.Request.QueryString["aqjdbm"];
                dt = xmxxBLL.RetrieveAjxx_RyxxList(aqjdbm, ft, pagesize, page, orderby, out allRecordCount);
                break;

            case "zjxx_ryxxList":
                string zljdbm = context.Request.QueryString["zljdbm"];
                dt = xmxxBLL.RetrieveZjxx_RyxxList(zljdbm, ft, pagesize, page, orderby, out allRecordCount);
                break;

            case "lxxmList":
                dt = xmxxBLL.RetrieveLxxmList(ft, pagesize, page, orderby, out allRecordCount);
                break;
            }
            ft.Parms.Clear();

            string result = JSONHelper.DataTableToJson(dt);

            //result = Regex.Replace(result, @"[/n/r]", ""); //去掉字符串里所有换行符
            //result = result.TrimEnd((char[])"\n\r".ToCharArray());  //去掉换行符
            json = @"{""Rows"":[" + result + @"],""Total"":""" + allRecordCount + @"""}";
            context.Response.Write(json);
            context.Response.End();
        }
Beispiel #4
0
        public void TestMethod3()
        {
            var scopeFactory = new ContextExtension("demo");

            using (var scope = scopeFactory.GetReadOnlyContextScope())
            {
                var x = TestMethod3_sub(scope);
                Console.WriteLine(x.FirstOrDefault());
            }
        }
Beispiel #5
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int     allRecordCount = 0;
            string  json           = "";
            string  sortname       = context.Request.Params["sortname"];
            string  sortorder      = context.Request.Params["sortorder"];
            int     page           = Convert.ToInt32(context.Request.Params["page"]) - 1; // 系统的索引从0开始,所以此处需要减1
            int     pagesize       = Convert.ToInt32(context.Request.Params["pagesize"]);
            string  fromWhere      = context.Request.QueryString["fromwhere"];
            string  type           = context.Request.QueryString["type"];
            object  sessionAppUser = context.Session[ConfigManager.GetSignInAppUserSessionName()];
            AppUser workUser       = (AppUser)sessionAppUser;

            xytxBLL = new XytxBLL(workUser);
            //排序
            string orderby = @" " + sortname.ToString2() + " " + sortorder.ToString2() + " ";

            orderby = orderby.Trim();
            //通过检索翻译 生成查询条件
            FilterTranslator ft = ContextExtension.GetGridData(context);
            //分页
            DataTable dt = new DataTable();

            switch (fromWhere)
            {
            case "qyxykp":
                string qylx = context.Request.QueryString["qylx"];

                dt = xytxBLL.RetrieveQyxykp(qylx, ft, pagesize, page, orderby, out allRecordCount);

                break;

            case "xzcf":
                //throw new Exception("aaa");
                dt = xytxBLL.RetrieveXzcf(type, ft, pagesize, page, orderby, out allRecordCount);
                break;
            }
            ft.Parms.Clear();

            string result = JSONHelper.DataTableToJson(dt);

            //result = Regex.Replace(result, @"[/n/r]", ""); //去掉字符串里所有换行符
            //result = result.TrimEnd((char[])"\n\r".ToCharArray());  //去掉换行符
            json = @"{""Rows"":[" + result + @"],""Total"":""" + allRecordCount + @"""}";
            context.Response.Write(json);
            context.Response.End();
        }
Beispiel #6
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int    allRecordCount = 0;
            string sortname       = context.Request.Params["sortname"];
            string sortorder      = context.Request.Params["sortorder"];
            int    page           = Convert.ToInt32(context.Request.Params["page"]) - 1; // 系统的索引从0开始,所以此处需要减1
            int    pagesize       = Convert.ToInt32(context.Request.Params["pagesize"]);
            string fromWhere      = context.Request.QueryString["fromwhere"];

            object sessionAppUser = context.Session[ConfigManager.GetSignInAppUserSessionName()];

            BLL = new ZlctBLL((AppUser)sessionAppUser);

            //排序
            string orderby = @" " + sortname + " " + sortorder + " ";
            //通过检索翻译 生成查询条件
            FilterTranslator ft = ContextExtension.GetGridData(context);
            //分页
            DataTable dt = new DataTable();

            switch (fromWhere)
            {
            case "Gzzs_List":
                dt = BLL.RetrieveGzzs_List(ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "Zshf_all_List":
                dt = BLL.RetrieveZshf_all_List(ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "DxjbSendRecords_List":
                dt = BLL.RetrieveDxjbSendRecords_List(ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "YzDxjb_List":
                dt = BLL.RetrieveYzDxjb_List(ft, pagesize, page, orderby, out allRecordCount).Result;
                break;
            }
            ft.Parms.Clear();
            string result = JSONHelper.DataTableToJson(dt);

            json = @"{""Rows"":[" + result + @"],""Total"":""" + allRecordCount + @"""}";
            context.Response.Write(json);
            context.Response.End();
        }
Beispiel #7
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int    allRecordCount = 0;
            string sortname       = context.Request.Params["sortname"];
            string sortorder      = context.Request.Params["sortorder"];
            int    page           = Convert.ToInt32(context.Request.Params["page"]) - 1; // 系统的索引从0开始,所以此处需要减1
            int    pagesize       = Convert.ToInt32(context.Request.Params["pagesize"]);
            string fromWhere      = context.Request.QueryString["fromwhere"];
            //string qylx = context.Request.QueryString["qylx"];
            //string qyId = context.Request.QueryString["qyid"];
            string rowid = context.Request.QueryString["rowid"];

            object  sessionAppUser = context.Session[ConfigManager.GetSignInAppUserSessionName()];
            AppUser workUser       = (AppUser)sessionAppUser;

            BLL = new GzyjBLL((AppUser)sessionAppUser);

            //排序
            string orderby = @" " + sortname + " " + sortorder + " ";
            //通过检索翻译 生成查询条件
            FilterTranslator ft = ContextExtension.GetGridData(context);
            //分页
            DataTable dt = new DataTable();

            //switch (fromWhere)
            //{
            //case "qyzsgq":
            dt = BLL.Retrieve(fromWhere, workUser, ft, pagesize, page, orderby, out allRecordCount).Result;
            //        break;
            //    case "JsdwAjxm":
            //        dt = BLL.RetrieveJsdwAjxm(rowid, pagesize, page, orderby, out allRecordCount).Result;
            //        break;

            //    case "QyxxView":
            //        dt = BLL.RetrieveQyxxViewList(qyId, ft).Result;
            //        break;
            //}
            ft.Parms.Clear();
            string result = JSONHelper.DataTableToJson(dt);

            result = Regex.Replace(result, @"[\n\r]", ""); //去掉字符串里所有换行符
            json   = @"{""Rows"":[" + result + @"],""Total"":""" + allRecordCount + @"""}";
            context.Response.Write(json);
            context.Response.End();
        }
Beispiel #8
0
        public void TestMethod2()
        {
            var scopeFactory = new ContextExtension("demo");

            using (var scope = scopeFactory.GetContextScope())
            {
                var db = scopeFactory.GetDbContext <CommonDbContext>();
                db.Users.Add(new User()
                {
                    Id               = Guid.NewGuid(),
                    Name             = "Marshall1",
                    Email            = "[email protected]",
                    WelcomeEmailSent = false,
                    CreatedOn        = DateTime.UtcNow
                });
                scope.SaveChanges();
            }
        }
Beispiel #9
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int     allRecordCount = 0;
            string  sortname       = context.Request.Params["sortname"];
            string  sortorder      = context.Request.Params["sortorder"];
            int     page           = Convert.ToInt32(context.Request.Params["page"]) - 1; // 系统的索引从0开始,所以此处需要减1
            int     pagesize       = Convert.ToInt32(context.Request.Params["pagesize"]);
            string  fromWhere      = context.Request.QueryString["fromwhere"];
            string  xmlx           = context.Request.QueryString["xmlx"];
            string  rowid          = context.Request.QueryString["rowid"];
            string  zljdid         = context.Request.QueryString["zljdid"];
            string  aqjdid         = context.Request.QueryString["aqjdid"];
            string  sgxkid         = context.Request.QueryString["sgxkid"];
            string  sgxmtybh       = context.Request.QueryString["sgxmtybh"];
            string  qyId           = context.Request.QueryString["qyid"];
            string  befrom         = context.Request.QueryString["befrom"];
            object  sessionAppUser = context.Session[ConfigManager.GetSignInAppUserSessionName()];
            AppUser workUser       = (AppUser)sessionAppUser;

            BLL = new SzgcBLL(workUser);
            bll = new SzqyBLL(workUser);

            //排序
            string orderby = @" " + sortname + " " + sortorder + " ";
            //通过检索翻译 生成查询条件
            FilterTranslator ft = ContextExtension.GetGridData(context);
            //分页
            DataTable dt = new DataTable();

            switch (fromWhere)
            {
            case "Lxbd":
                dt = BLL.RetrieveLxbd(rowid, ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "Xmxx":
                dt = BLL.RetrieveSzgc(xmlx, workUser, ft, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "GcxmHtba":
                dt = BLL.RetrieveGcxmHtba(rowid, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "GcxmAqjd":
                dt = BLL.RetrieveGcxmAqjd(rowid, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "GcxmZljd":
                dt = BLL.RetrieveGcxmZljd(rowid, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "GcxmSgxk":
                dt = BLL.RetrieveGcxmSgxk(rowid, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "GcxmJgys":
                dt = BLL.RetrieveGcxmJgys(rowid, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "DwgcList":
                dt = BLL.RetrieveDwgcList(rowid, pagesize, page, orderby, out allRecordCount).Result;
                break;

            case "Aqbjxmzcy":
                dt = BLL.RetrieveAqbjxmzcy(rowid, befrom).Result;
                break;

            case "AqjdZljd":
                dt = BLL.RetrieveAqjdZljd(rowid).Result;
                break;

            case "AqjdSgxk":
                dt = BLL.RetrieveAqjdSgxk(rowid).Result;
                break;

            case "AqjdJgba":
                dt = BLL.RetrieveAqjdJgba(rowid).Result;
                break;

            case "zljdDtgc":
                dt = BLL.RetrieveZljdDtgc(rowid, befrom).Result;
                break;

            case "zljdSgxk":
                dt = BLL.RetrieveZljdSgxk(rowid).Result;
                break;

            case "zljdJgba":
                dt = BLL.RetrieveZljdJgba(rowid).Result;
                break;

            case "sgxk":
                dt = BLL.RetrieveSgxk(rowid).Result;
                break;

            case "sgxkJgba":
                dt = BLL.RetrieveSgxkJgba(rowid).Result;
                break;

            case "QyxxView":
                dt = bll.RetrieveQyxxViewList(qyId).Result;
                break;
            }
            ft.Parms.Clear();

            string result = JSONHelper.DataTableToJson(dt);

            //result = Regex.Replace(result, @"[/n/r]", ""); //去掉字符串里所有换行符
            //result = result.TrimEnd((char[])"\n\r".ToCharArray());  //去掉换行符
            json = @"{""Rows"":[" + result + @"],""Total"":""" + allRecordCount + @"""}";
            context.Response.Write(json);
            context.Response.End();
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            using (var scope = ContextExtension.GetReadOnlyContextScope("demo"))
            {
                var db = scope.DbContexts.Get <CommonDbContext>();
                Console.WriteLine(string.Join(".", db.Users.ToList().Select(c => c.Name)));
                var u = from ux in db.Users where ux.Name.Length > 2 select ux.Email;
                Console.WriteLine(string.Join(".", u));
            }
            using (var scope = ContextExtension.GetReadOnlyContextScope("bak"))
            {
                var db1 = scope.DbContexts.Get <CommonDbContext>();
                Console.WriteLine(string.Join(".", db1.Users.ToList().Select(c => c.Name)));
            }


            //-- Poor-man DI - build our dependencies by hand for this demo
            var dbContextScopeFactory   = new DbContextScopeFactory();
            var ambientDbContextLocator = new AmbientDbContextLocator();
            var userRepository          = new UserRepository(ambientDbContextLocator);

            var userCreationService    = new UserCreationService(dbContextScopeFactory, userRepository);
            var userQueryService       = new UserQueryService(dbContextScopeFactory, userRepository);
            var userEmailService       = new UserEmailService(dbContextScopeFactory);
            var userCreditScoreService = new UserCreditScoreService(dbContextScopeFactory);

            try
            {
                Console.WriteLine(
                    "This demo application will create a database named DbContextScopeDemo in the default SQL Server instance on localhost. Edit the connection string in UserManagementDbContext if you'd like to create it somewhere else.");
                Console.WriteLine("Press enter to start...");
                Console.ReadLine();

                //-- Demo of typical usage for read and writes
                Console.WriteLine("Creating a user called Mary...");
                var marysSpec = new UserCreationSpec("Mary", "*****@*****.**");
                userCreationService.CreateUser(marysSpec);
                Console.WriteLine("Done.\n");

                Console.WriteLine("Trying to retrieve our newly created user from the data store...");
                var mary = userQueryService.GetUser(marysSpec.Id);
                Console.WriteLine("OK. Persisted user: {0}", mary);

                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();

                //-- Demo of nested DbContextScopes
                Console.WriteLine("Creating 2 new users called John and Jeanne in an atomic transaction...");
                var johnSpec   = new UserCreationSpec("John", "*****@*****.**");
                var jeanneSpec = new UserCreationSpec("Jeanne", "*****@*****.**");
                userCreationService.CreateListOfUsers(johnSpec, jeanneSpec);
                Console.WriteLine("Done.\n");

                Console.WriteLine("Trying to retrieve our newly created users from the data store...");
                var createdUsers = userQueryService.GetUsers(johnSpec.Id, jeanneSpec.Id);
                Console.WriteLine("OK. Found {0} persisted users.", createdUsers.Count());

                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();

                //-- Demo of nested DbContextScopes in the face of an exception.
                // If any of the provided users failed to get persisted, none should get persisted.
                Console.WriteLine(
                    "Creating 2 new users called Julie and Marc in an atomic transaction. Will make the persistence of the second user fail intentionally in order to test the atomicity of the transaction...");
                var julieSpec = new UserCreationSpec("Julie", "*****@*****.**");
                var marcSpec  = new UserCreationSpec("Marc", "*****@*****.**");
                try
                {
                    userCreationService.CreateListOfUsersWithIntentionalFailure(julieSpec, marcSpec);
                    Console.WriteLine("Done.\n");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine();
                }

                Console.WriteLine("Trying to retrieve our newly created users from the data store...");
                var maybeCreatedUsers = userQueryService.GetUsers(julieSpec.Id, marcSpec.Id);
                Console.WriteLine(
                    "Found {0} persisted users. If this number is 0, we're all good. If this number is not 0, we have a big problem.",
                    maybeCreatedUsers.Count());

                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();

                //-- Demo of DbContextScope within an async flow
                Console.WriteLine(
                    "Trying to retrieve two users John and Jeanne sequentially in an asynchronous manner...");
                // We're going to block on the async task here as we don't have a choice. No risk of deadlocking in any case as console apps
                // don't have a synchronization context.
                var usersFoundAsync = userQueryService.GetTwoUsersAsync(johnSpec.Id, jeanneSpec.Id).Result;
                Console.WriteLine("OK. Found {0} persisted users.", usersFoundAsync.Count());

                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();

                //-- Demo of explicit database transaction.
                Console.WriteLine("Trying to retrieve user John within a READ UNCOMMITTED database transaction...");
                // You'll want to use SQL Profiler or Entity Framework Profiler to verify that the correct transaction isolation
                // level is being used.
                var userMaybeUncommitted = userQueryService.GetUserUncommitted(johnSpec.Id);
                Console.WriteLine("OK. User found: {0}", userMaybeUncommitted);

                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();

                //-- Demo of disabling the DbContextScope nesting behaviour in order to force the persistence of changes made to entities
                // This is a pretty advanced feature that you can safely ignore until you actually need it.
                Console.WriteLine("Will simulate sending a Welcome email to John...");

                using (var parentScope = dbContextScopeFactory.Create())
                {
                    var parentDbContext = parentScope.DbContexts.Get <CommonDbContext>();

                    // Load John in the parent DbContext
                    var john = parentDbContext.Users.Find(johnSpec.Id);
                    Console.WriteLine("Before calling SendWelcomeEmail(), john.WelcomeEmailSent = " +
                                      john.WelcomeEmailSent);

                    // Now call our SendWelcomeEmail() business logic service method, which will
                    // update John in a non-nested child context
                    userEmailService.SendWelcomeEmail(johnSpec.Id);

                    // Verify that we can see the modifications made to John by the SendWelcomeEmail() method
                    Console.WriteLine("After calling SendWelcomeEmail(), john.WelcomeEmailSent = " +
                                      john.WelcomeEmailSent);

                    // Note that even though we're not calling SaveChanges() in the parent scope here, the changes
                    // made to John by SendWelcomeEmail() will remain persisted in the database as SendWelcomeEmail()
                    // forced the creation of a new DbContextScope.
                }

                Console.WriteLine("Press enter to continue...");
                Console.ReadLine();

                //-- Demonstration of DbContextScope and parallel programming
                Console.WriteLine(
                    "Calculating and storing the credit score of all users in the database in parallel...");
                userCreditScoreService.UpdateCreditScoreForAllUsers();
                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.WriteLine();
            Console.WriteLine("The end.");
            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }