Exemple #1
0
        public static void Main(string[] args)
        {
            //string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\DataSource\\" + ConfigHelper.GetConnectionString("SQLiteConnection");
            string connectionString = ConfigHelper.GetConnectionString("SqlConnection");

            #region 框架测试
            ///此部分最好在全局中注册,或者在IOC注册中注册
            ProviderFactory.RegisterProviderFactory(connectionString, DateProvider.SqlServer);

            DbContext context = new DbContext(connectionString);
            //强类型
            Repository <tb_User> repository = context.CreateRepository <tb_User>();
            //无类型
            var nonrepository = context.CreateNonRepository();

            ///以下方法都是支持无类型的

            //列表获取
            repository.GetList(e => e.name == "cxb" || e.mobile == "15989027156");
            nonrepository.GetList <tb_User>(e => e.name == "cxb" || e.mobile == "15989027156");
            //新增
            var inum = repository.Insert(new tb_User()
            {
                mobile = "15989027256", name = "bosco", password = "******", sex = 1, roleid = 1
            });
            //更新
            int unum = repository.Update(new tb_User {
                mobile = "15989027255", name = "bosco", password = "******", sex = 1, isDelete = true
            }, e => e.name == "cxb");
            //部分更新
            repository.UpdatePart(e => new tb_User()
            {
                name = "123", mobile = "15989027156"
            }, a => a.name == "cxb");
            //删除
            int dnum = repository.Detele(e => e.name == "b");
            //分页查找
            int pageindex   = 1;
            int pagesize    = 100;
            var nonpagelist = nonrepository.GetPageList <UserInfo>(e => true, pageindex, pagesize);
            var pagelist    = repository.GetPageList(e => true, pageindex, pagesize);
            //支持事务
            context.CreateTransaction();
            repository.Insert(new tb_User()
            {
                mobile = "15989027256", name = "bosco", password = "******", sex = 1, roleid = 1
            });
            repository.Update(new tb_User {
                mobile = "15989027255"
            }, e => e.name == "cxb");
            context.CommitTransaction();

            #endregion
            #region 支持根和聚合方式
            nonrepository.Insert <tb_Role>(new tb_Role {
                rid = 1, rname = "cxb", risDelete = 2
            });
            tb_Role roles = new tb_Role();
            repository.Query <tb_Role, tb_UserX, tb_Role>("select * from tb_User u left join tb_Role r on u.roleid=r.rid", (tb_Role, tb_UserX) =>
            {
                if (tb_Role.rid != roles.rid)
                {
                    roles = tb_Role;
                }
                roles.UserList.Add(tb_UserX);
                return(roles);
            });
            #endregion
            #region 支持多结果集
            QueryReader    reader       = repository.QueryMultiple("select * from tb_User;select * from tb_Role");
            List <tb_User> ReadUserList = reader.ReadList <tb_User>().ToList();
            List <tb_Role> ReadRoleList = reader.ReadList <tb_Role>().ToList();

            #endregion
            #region 支持多结果
            QueryReader readerOne = repository.QueryMultiple("select * from tb_User; select* from tb_Role");
            var         nonUser   = readerOne.Read <tb_User>();
            var         role      = readerOne.Read <tb_Role>();
            #endregion
            #region 查询对象方法
            //QueryObjectRepository<tb_User> query = new QueryObjectRepository<tb_User>(connectionString);
            //var userq = new tb_User() { mobile = "15989027256", name = "bosco", password = "******", sex = 1 };
            //query.Insert(userq);
            //userq = query.Get(new Condition() { field = "mobile", operarorsign = "=", value = "15989027256" });
            //Console.WriteLine(userq.mobile);
            //List<Condition> conditions = new List<Condition>();
            //conditions.Add(new Condition() { field = "mobile", operarorsign = "=", value = "15989027256" });
            //userq = query.Get(conditions);
            //Console.WriteLine(userq.mobile);
            #endregion
            string sqlRecord = "select * from tb_User";

            #region SimpleOrm性能测试
            var watcher = new Stopwatch();
            watcher.Start();

            for (int i = 0; i < 100; i++)
            {
                repository.Query <tb_User>(sqlRecord, null).FirstOrDefault();
            }
            watcher.Stop();
            var duration = watcher.Elapsed.TotalMilliseconds;
            Console.WriteLine("SimpleOrm的效率:" + duration);
            #endregion

            #region Dapper性能测试
            var watcher0 = new Stopwatch();
            watcher0.Start();

            IDbConnection conn = new SqlConnection(connectionString);
            for (int i = 0; i < 100; i++)
            {
                conn.Query <tb_User>(sqlRecord, null).ToList().FirstOrDefault();
            }
            watcher0.Stop();
            var duration0 = watcher0.Elapsed.TotalMilliseconds;
            Console.WriteLine("Dapper的效率:" + duration0);
            #endregion

            #region 反射性能测试

            var watcher1 = new Stopwatch();
            watcher1.Start();
            for (int i = 0; i < 100; i++)
            {
                repository.QueryReflect <tb_User>(sqlRecord, null).FirstOrDefault();
            }
            watcher1.Stop();
            var duration1 = watcher1.Elapsed.TotalMilliseconds;
            Console.WriteLine(".net反射的效率:" + duration1);
            #endregion

            Console.Read();
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            //string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\DataSource\\" + ConfigHelper.GetConnectionString("SQLiteConnection");
            string connectionString = ConfigHelper.GetConnectionString("SqlConnection");

            #region 框架测试
            ///此部分最好在全局中注册,或者在IOC注册中注册
            ProviderFactory.RegisterProviderFactory(connectionString, DateProvider.SqlServer);

            DbContext context = new DbContext(connectionString);
            //强类型
            StoreSimpleClient <tb_User> client = context.CreateStoreSimpleClient <tb_User>();
            //无类型
            var nonClient = context.CreateNonSimpleClient();

            ///以下方法都是支持无类型的

            //列表获取
            client.GetList(e => e.name == "cxb" || e.mobile == "15989027156");
            nonClient.GetList <tb_User>(e => e.name == "cxb" || e.mobile == "15989027156");
            //新增
            var inum = client.Insert(new tb_User()
            {
                mobile = "15989027256", name = "bosco", password = "******", sex = 1, roleid = 1
            });
            //更新
            int unum = client.Update(new tb_User {
                mobile = "15989027255", name = "bosco", password = "******", sex = 1, isDelete = true
            }, e => e.name == "cxb");
            //部分更新
            client.UpdatePart(e => new tb_User()
            {
                name = "123", mobile = "15989027156"
            }, a => a.name == "cxb");
            //删除
            int dnum = client.Detele(e => e.name == "b");
            //分页查找
            int pageindex   = 1;
            int pagesize    = 100;
            var nonPagelist = nonClient.GetPageList <UserInfo>(e => true, pageindex, pagesize);
            var pagelist    = client.GetPageList(e => true, pageindex, pagesize);
            //普通SQL查询
            var result = client.Query <tb_User>("select * from tb_User", new { });
            //单条数据返回
            var resultSingle = client.QuerySingle <tb_User>("select * from tb_User", new { });

            //支持事务
            context.CreateTransaction();
            client.Insert(new tb_User()
            {
                mobile = "15989027256", name = "bosco", password = "******", sex = 1, roleid = 1
            });
            client.Update(new tb_User {
                mobile = "15989027255"
            }, e => e.name == "cxb");
            context.CommitTransaction();

            #endregion
            #region 支持根和聚合方式
            nonClient.Insert <tb_Role>(new tb_Role {
                rid = 1, rname = "cxb", risDelete = 2
            });
            tb_Role roles = new tb_Role();
            client.Query <tb_Role, tb_UserX, tb_Role>("select * from tb_User u left join tb_Role r on u.roleid=r.rid", (tb_Role, tb_UserX) =>
            {
                if (tb_Role.rid != roles.rid)
                {
                    roles = tb_Role;
                }
                roles.UserList.Add(tb_UserX);
                return(roles);
            });
            #endregion
            #region 支持多结果集
            QueryReader    reader       = client.QueryMultiple("select * from tb_User;select * from tb_Role");
            List <tb_User> ReadUserList = reader.ReadList <tb_User>().ToList();
            List <tb_Role> ReadRoleList = reader.ReadList <tb_Role>().ToList();

            #endregion
            #region 查询对象方法
            //QueryObjectclient<tb_User> query = new QueryObjectclient<tb_User>(connectionString);
            //var userq = new tb_User() { mobile = "15989027256", name = "bosco", password = "******", sex = 1 };
            //query.Insert(userq);
            //userq = query.Get(new Condition() { field = "mobile", operarorsign = "=", value = "15989027256" });
            //Console.WriteLine(userq.mobile);
            //List<Condition> conditions = new List<Condition>();
            //conditions.Add(new Condition() { field = "mobile", operarorsign = "=", value = "15989027256" });
            //userq = query.Get(conditions);
            //Console.WriteLine(userq.mobile);
            #endregion
        }