Example #1
0
        private static void AgileClient()
        {
            //Create db
            Console.WriteLine("#### AgileClient Start ####");
            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                DbType                = DatabaseType.SqlServer,
                ConnectionString      = Config.ConnectionString,
                InitKeyType           = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents             = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            //If no exist create datebase
            db.DbMaintenance.CreateDatabase();

            //Use db query
            var dt = db.Ado.GetDataTable("select 1");

            //Create tables
            db.CodeFirst.InitTables(typeof(OrderItem), typeof(Order));
            var id = db.Insertable(new Order()
            {
                Name = "order1", CustomId = 1, Price = 0, CreateTime = DateTime.Now
            }).ExecuteReturnIdentity();

            //Insert data
            db.Insertable(new OrderItem()
            {
                OrderId = id, Price = 0, CreateTime = DateTime.Now
            }).ExecuteCommand();
            Console.WriteLine("#### AgileClient End ####");
        }
Example #2
0
        public static void Init()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Queue Start ####");

            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                DbType                = DatabaseType.SqlServer,
                ConnectionString      = Config.ConnectionString,
                InitKeyType           = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents             = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            db.Insertable <Order>(new Order()
            {
                Name = "a"
            }).AddQueue();
            db.Insertable <Order>(new Order()
            {
                Name = "b"
            }).AddQueue();
            db.SaveQueues();


            db.Insertable <Order>(new Order()
            {
                Name = "a"
            }).AddQueue();
            db.Insertable <Order>(new Order()
            {
                Name = "b"
            }).AddQueue();
            db.Insertable <Order>(new Order()
            {
                Name = "c"
            }).AddQueue();
            db.Insertable <Order>(new Order()
            {
                Name = "d"
            }).AddQueue();
            var ar = db.SaveQueuesAsync();

            ar.Wait();

            db.Queryable <Order>().AddQueue();
            db.Queryable <Order>().AddQueue();
            db.AddQueue("select * from [Order] where id=@id", new { id = 10000 });
            var result2 = db.SaveQueues <Order, Order, Order>();

            Console.WriteLine("#### Queue End ####");
        }
Example #3
0
        public static void Init()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Insertable Start ####");

            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                DbType                = DatabaseType.SqlServer,
                ConnectionString      = Config.ConnectionString,
                InitKeyType           = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents             = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            var insertObj = new Order()
            {
                Id = 1, Name = "order1", Price = 0
            };
            var updateObjs = new List <Order> {
                new Order()
                {
                    Id = 11, Name = "order11", Price = 0
                },
                new Order()
                {
                    Id = 12, Name = "order12", Price = 0
                }
            };

            //Ignore  CreateTime
            db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
            db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();

            //Only  insert  Name and Price
            db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();
            db.Insertable(insertObj).InsertColumns("Name", "Price").ExecuteReturnIdentity();

            //ignore null columns
            db.Insertable(updateObjs).ExecuteCommand();//get change row count

            //Use Lock
            db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();

            Console.WriteLine("#### Insertable End ####");
        }
Example #4
0
        private static void CustomAttribute()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Custom Attribute Start ####");
            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                ConnectionString          = Config.ConnectionString,
                DbType                    = DatabaseType.SqlServer,
                IsAutoCloseConnection     = true,
                InitKeyType               = InitKeyType.Attribute,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (property, column) =>
                    {
                        var attributes = property.GetCustomAttributes(true); //get all attributes

                        if (attributes.Any(it => it is KeyAttribute))        // by attribute set primarykey
                        {
                            column.IsPrimarykey = true;
                        }
                    },
                    EntityNameService = (type, entity) =>
                    {
                        var attributes = type.GetCustomAttributes(true);
                        if (attributes.Any(it => it is TableAttribute))
                        {
                            entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name;
                        }
                    }
                }
            });

            db.CodeFirst.InitTables <AttributeTable>();//Create Table

            db.Insertable(new AttributeTable()
            {
                Id = Guid.NewGuid().ToString(), Name = "Name"
            }).ExecuteCommand();
            var list = db.Queryable <AttributeTable>().ToList();

            Console.WriteLine("#### Custom Attribute End ####");
        }
Example #5
0
        public static void Init()
        {
            Console.WriteLine("");
            Console.WriteLine("#### CodeFirst Start ####");
            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                DbType                = DatabaseType.SqlServer,
                ConnectionString      = Config.ConnectionString3,
                InitKeyType           = InitKeyType.Attribute,
                IsAutoCloseConnection = true
            });

            db.DbMaintenance.CreateDatabase();
            db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
            db.Insertable(new CodeFirstTable1()
            {
                Name = "a", Text = "a"
            }).ExecuteCommand();
            var list = db.Queryable <CodeFirstTable1>().ToList();

            Console.WriteLine("#### CodeFirst end ####");
        }
Example #6
0
        private static void MasterSlave()
        {
            Console.WriteLine("");
            Console.WriteLine("#### MasterSlave Start ####");
            AgileClient db = new AgileClient(new ConnectionConfig()
            {
                ConnectionString       = Config.ConnectionString,//Master Connection
                DbType                 = DatabaseType.SqlServer,
                InitKeyType            = InitKeyType.Attribute,
                IsAutoCloseConnection  = true,
                SlaveConnectionConfigs = new List <SlaveConnectionConfig>()
                {
                    new SlaveConnectionConfig()
                    {
                        HitRate = 10, ConnectionString = Config.ConnectionString2
                    },
                    new SlaveConnectionConfig()
                    {
                        HitRate = 10, ConnectionString = Config.ConnectionString2
                    }
                }
            });

            db.Aop.OnLogExecuted = (s, p) =>
            {
                Console.WriteLine(db.Ado.Connection.ConnectionString);
            };
            Console.WriteLine("Master:");
            db.Insertable(new Order()
            {
                Name = "abc", CustomId = 1, CreateTime = DateTime.Now
            }).ExecuteCommand();
            Console.WriteLine("Slave:");
            db.Queryable <Order>().First();
            Console.WriteLine("#### MasterSlave End ####");
        }
Example #7
0
        private static void Single()
        {
            var t1 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    singleDb.Insertable(new Order()
                    {
                        Name = "test", CreateTime = DateTime.Now
                    }).ExecuteCommand();
                    System.Threading.Thread.Sleep(1);
                }
            });
            var t2 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    singleDb.Insertable(new Order()
                    {
                        Name = "test2", CreateTime = DateTime.Now
                    }).ExecuteCommand();
                    System.Threading.Thread.Sleep(10);
                }
            });
            var t3 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    singleDb.Insertable(new Order()
                    {
                        Name = "test3", CreateTime = DateTime.Now
                    }).ExecuteCommand();
                    System.Threading.Thread.Sleep(6);
                }
            });

            t1.Start();
            t2.Start();
            t3.Start();

            Task.WaitAll(t1, t2, t3);
        }
Example #8
0
        private static void DistributedTransactionExample()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Distributed TransactionExample Start ####");
            AgileClient db = new AgileClient(new List <ConnectionConfig>()
            {
                new ConnectionConfig()
                {
                    ConfigId = "1", DbType = DatabaseType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true
                },
                new ConnectionConfig()
                {
                    ConfigId = "2", DbType = DatabaseType.SqlServer, ConnectionString = Config.ConnectionString2, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true
                }
            });

            //use db1
            db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));//
            db.Insertable(new Order()
            {
                Name = "order1", CreateTime = DateTime.Now
            }).ExecuteCommand();
            Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable <Order>().Count());

            //use db2
            db.ChangeDatabase("2");
            db.DbMaintenance.CreateDatabase();//Create Database2
            db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));
            db.Insertable(new Order()
            {
                Name = "order1", CreateTime = DateTime.Now
            }).ExecuteCommand();
            Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable <Order>().Count());

            // Example 1
            Console.WriteLine("Example 1");
            try
            {
                db.BeginTran();

                db.ChangeDatabase("1");//use db1
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                throw new Exception();
                db.CommitTran();
            }
            catch
            {
                db.RollbackTran();
                Console.WriteLine("---Roll back");
                db.ChangeDatabase("1");//use db1
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
            }



            // Example 2
            Console.WriteLine("Example 2");

            var result = db.UseTran(() =>
            {
                db.ChangeDatabase("1");//use db1
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
                throw new Exception("");
            });

            if (result.IsSuccess == false)
            {
                Console.WriteLine("---Roll back");
                db.ChangeDatabase("1");//use db1
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
            }

            // Example 3
            Console.WriteLine("Example 3");

            var result2 = db.UseTranAsync(() =>
            {
                db.ChangeDatabase("1");//use db1
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use db2
                db.Deleteable <Order>().ExecuteCommand();
                Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
                throw new Exception("");
            });

            result2.Wait();
            if (result.IsSuccess == false)
            {
                Console.WriteLine("---Roll back");
                db.ChangeDatabase("1");//use sqlserver
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());

                db.ChangeDatabase("2");//use mysql
                Console.WriteLine(db.CurrentConnectionConfig.DbType);
                Console.WriteLine(db.Queryable <Order>().Count());
            }

            Console.WriteLine("#### Distributed TransactionExample End ####");
        }