轻量级 .NET ORM 类库 Insql

码农软件 · 软件分类 · ORM/持久层框架 · 2019-09-22 14:59:26

软件介绍

Insql 是一个轻量级的.NET ORM类库。对象映射基于Dapper, Sql配置灵感来自于Mybatis。

功能特点:

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

  • 支持依赖注入系统

  • 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"));
    });
}

    创建 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
    }

    创建 DbContext.insql.xml

    创建 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
        }
    }

    本文地址:https://codercto.com/soft/d/15160.html

    Programming Collective Intelligence

    Programming Collective Intelligence

    Toby Segaran / O'Reilly Media / 2007-8-26 / USD 39.99

    Want to tap the power behind search rankings, product recommendations, social bookmarking, and online matchmaking? This fascinating book demonstrates how you can build Web 2.0 applications to mine the......一起来看看 《Programming Collective Intelligence》 这本书的介绍吧!

    CSS 压缩/解压工具
    CSS 压缩/解压工具

    在线压缩/解压 CSS 代码

    在线进制转换器
    在线进制转换器

    各进制数互转换器

    MD5 加密
    MD5 加密

    MD5 加密工具