zoukankan      html  css  js  c++  java
  • 轻量ORM-SqlRepoEx (十六)最佳实践之Dapper(2)

    简介:SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM。解决了Lambda转Sql语句这一难题,SqlRepoEx使用的是Lambda表达式,所以,对c#程序员来说,是非常简单的,其语法特点与Linq to Sql极为相似。不仅实现了完整的Select、Insert、Update、Delete等语句解析器,同时,也实现了Select、where、order by等子句,这些语句与子句均支持导出SQL语句,使得拼接复杂场景SQL语句变得轻松,SqlRepoEx很快其原生数据访问与Dapper不相上下,SqlRepoEx本身支持Sql Server与MySql方言,同时通过SqlRepoEx.Normal支持非方言SQL。SqlRepoEx没侵入性,仅通过简单的几个特性,就能让类与数据库关联起来;

     

    *本系列以静态工厂为例;

    *数据来源于Northwind数据库;

    *完整的代码见 https://github.com/AzThinker/SqlRepoEx2.0StartGuid  https://gitee.com/azthinker/SqlRepoEx2.0StartGuid

     

    五、@ 参数构建

    1、对于类属性 ,SqlRepoEx 为IInsertStatement,IUpdateStatement提供了 ParamSql()方法和 ParamSqlWithEntity()方法

    (1)、 ParamSql(),是一个简单返回参数语句的方法

    var repository = MsSqlRepoFactory.Create<AzProducts>();

                var resultUpdate = repository

                                        .Update()

        // 注意此处的 ParamSet 方法,此时不需要指定属性的值

        // 这为构建完全的  @ 参数 提供了方便

        // 特别是在批更新时,最为方便

                                        .ParamSet(p => p.ProductName2, p => p.CategoryID)

                                        .Where(p => p.ProductID == p.ProductID);

                Console.WriteLine(resultUpdate.ParamSql());

     此句会生成

    UPDATE [dbo].[Products]

    SET ProductName  = @ProductName2, CategoryID  = @CategoryID

    WHERE (([dbo].[Products].[ProductID] = @ProductID));

        // 使用 Dapper 更新

       AzProducts products = new AzProducts() { ProductID = 84, ProductName2 = "testvalue100", CategoryID = 7 };

       //  Dapper 更新

       int result = dbConnection.Execute(resultUpdate.ParamSql(), products);

     

    (2) ParamSqlWithEntity()方法,一个返Tupe (string paramsql, TEntity entity)

     

                var repository = MsSqlRepoFactory.Create<AzProducts>();

                var resultUpdate = repository

                                        .Update()

        // 此处使用 Set 方法,并将属值指定

                                        .Set(p => p.ProductName2, "testvalue234")

                                        .Set(p => p.CategoryID, 5)

                                        .Where(p => p.ProductID == 84);

                var ret = resultUpdate.ParamSqlWithEntity();

                Console.WriteLine(ret.paramsql);

     

     此句会生成

    UPDATE [dbo].[Products]

    SET ProductName  = @ProductName2, CategoryID  = @CategoryID

    WHERE (([dbo].[Products].[ProductID] = 84));

    同时 Set 方法中的属值会生在一个 AzProducts 类实例,这样为使用 Dapper提供了方便

      int result = dbConnection.Execute(ret.paramsql, ret.entity);

     

     2、对于条件语句的  @ 参数

     条件语句中,只需要使用

     

     形如:.Where(p => p.ProductID == p.ProductID); 即可产生  WHERE (([dbo].[Products].[ProductID] = @ProductID));

     参见上例 1 (1)

     

    总结:

    1、对于与Dapper结合使用,SqlRepoEx提供了两种方式,一种是使用 SqlRepoEx.Adapter.Dapper ,采用与其他SqlRepoEx的操作一至的方式来访问数据;二是,采用《最佳实践之Dapper》中的方式,以SQL语句结合Dapper来操作;两种方式可同时并存,不影响使用,具体采用哪形式,由编程人员自行决定。

    2、 SqlRepoEx提供了以Lamdba的方式 ,其操作类似于Linq的操作,但更接近于Sql本身的语义顺序

    3、参数字段的构建非常方便,减少拼接语句容易出错问题,同时使用 Table,Column等特性,为异构提供了简易的操作;

     

     

  • 相关阅读:
    Discuz!NT 系统架构分析
    jquery pager
    Nhibernate分页方法
    Discuz nt模板机制
    WTclient创建OPC client方法
    OPC
    Regular Expressions in Java
    How to use VS to manipulate Access
    OPC客户端设计
    How to use VS to manipulate Excel使用MFC读写Excel
  • 原文地址:https://www.cnblogs.com/athinker/p/9854974.html
Copyright © 2011-2022 走看看