<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Insql輕量級 .NET ORM 類庫

          聯(lián)合創(chuàng)作 · 2023-09-30 01:47

          Insql 是一個輕量級的.NET ORM類庫。對象映射基于Dapper, Sql配置靈感來自于Mybatis。

          功能特點:

          • 支持 DoNet Core 2.0+ && DotNet Framework 4.6.1+

          • 支持依賴注入系統(tǒng)

          • MyBatis sql xml 語法

          • 多數據庫支持

          • 靈活擴展性

          • 使用簡單

          基本用法:

          添加Insql

          public void ConfigureServices(IServiceCollection services)
          {
              services.AddInsql();
          
              services.AddInsqlDbContext<UserDbContext>(options =>
              {
                //options.UseSqlServer(this.Configuration.GetConnectionString("sqlserver"));
                options.UseSqlite(this.Configuration.GetConnectionString("sqlite"));
              });
          }

            創(chuàng)建 DbContext 

            public class UserDbContext : Insql.DbContext  
            {
                public UserDbContext(Insql.DbContextOptions<UserDbContext> options) 
            		: base(options)
                {
                }
            
                public IEnumerable<UserInfo> GetUserList(string userName)
                {
                    //sqlId = "GetUserList"
                    //sqlParam is PlainObject or IDictionary<string,object>
                    return this.Query<UserInfo>(nameof(GetUserList), new { userName, userGender = Gender.W });
                }
            
                public void InsertUser(UserInfo info)
                {
                    var userId = this.ExecuteScalar<int>(nameof(InsertUser),info);
            
                    info.UserId = userId;
                }
            
                public void UpdateUserSelective(UserInfo info)
                {
                    this.Execute(nameof(UpdateUserSelective), info);
                }
            }
            	
            //user model
            public class UserInfo
            {
                public int UserId { get; set; }
            
                public string UserName { get; set; }
            
                public Gender? UserGender { get; set; }
            }
            
            public enum Gender
            {
                M,
                W
            }

            創(chuàng)建 DbContext.insql.xml

            創(chuàng)建 UserDbContext.insql.xml 文件并且修改這個文件的屬性為嵌入式文件類型 . insql type 與 UserDbContext 類型對應.

            <insql type="Example.Domain.Contexts.UserDbContext,Example.Domain" >
              
                <sql id="selectUserColumns">
                  select user_id as UserId,user_name as UserName,user_gender as UserGender from user_info
                </sql>
            
                <select id="GetUserList">
                  <include refid="selectUserColumns" />
                  <where>
                    <if test="userName != null">
                      <bind name="likeUserName" value="'%' + userName + '%'" />
                      user_name like @likeUserName
                    </if>
                    <if test="userGender != null and userGender != 'M' ">
                      and user_gender = @userGender
                    </if>
                  </where>
                  order by  user_id
                </select>
            
                <insert id="InsertUser">
                  insert into user_info (user_name,user_gender) values (@UserName,@UserGender);
                  select last_insert_rowid() from user_info;
                </insert>
            
                <update id="UpdateUserSelective">
                  update user_info
                  <set>
                    <if test="UserName != null">
                      user_name=@UserName,
                    </if>
                    <if test="UserGender != null">
                      user_gender=@UserGender
                    </if>
                  </set>
                  where user_id = @UserId
                </update>
            	
            </insql>

            使用 DbContext

            使用 UserDbContext 在Domain Service中或者Web Controller中

            public class ValuesController : ControllerBase
            {
                private readonly UserDbContext userDbContext;
            
                public ValuesController(UserDbContext userDbContext)
                {
                    this.userDbContext = userDbContext;
                }
            
                [HttpGet]
                public ActionResult<IEnumerable<string>> Get()
                {
                    //可以這樣使用事務
                    this.userDbContext.DoWithTransaction(() =>
                    {
                        var userInfo = new Domain.UserInfo
                        {
                            UserName = "loveW",
                            UserGender = Domain.Gender.M
                        };
            
                        this.userDbContext.InsertUser(userInfo);
            
                        this.userDbContext.UpdateUserSelective(new Domain.UserInfo
                        {
                            UserId = userInfo.UserId,
                            UserName = "loveWWW",
                        });
                    });
            
                    var list = this.userDbContext.GetUserList("love");
            	//todo return
                }
            }
            瀏覽 11
            點贊
            評論
            收藏
            分享

            手機掃一掃分享

            編輯 分享
            舉報
            評論
            圖片
            表情
            推薦
            點贊
            評論
            收藏
            分享

            手機掃一掃分享

            編輯 分享
            舉報
            <kbd id="afajh"><form id="afajh"></form></kbd>
            <strong id="afajh"><dl id="afajh"></dl></strong>
              <del id="afajh"><form id="afajh"></form></del>
                  1. <th id="afajh"><progress id="afajh"></progress></th>
                    <b id="afajh"><abbr id="afajh"></abbr></b>
                    <th id="afajh"><progress id="afajh"></progress></th>
                    一区二区三区白嫩在线 | 成人开心五月天 | 北条麻妃熟女60分钟 | 中国航空少妇一级毛片 | 狠狠操夜夜操 |