轻量级 .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://www.codercto.com/soft/d/15160.html

    Tensorflow:实战Google深度学习框架

    Tensorflow:实战Google深度学习框架

    郑泽宇、顾思宇 / 电子工业出版社 / 2017-2-10 / 79

    TensorFlow是谷歌2015年开源的主流深度学习框架,目前已在谷歌、优步(Uber)、京东、小米等科技公司广泛应用。《Tensorflow实战》为使用TensorFlow深度学习框架的入门参考书,旨在帮助读者以最快、最有效的方式上手TensorFlow和深度学习。书中省略了深度学习繁琐的数学模型推导,从实际应用问题出发,通过具体的TensorFlow样例程序介绍如何使用深度学习解决这些问题。......一起来看看 《Tensorflow:实战Google深度学习框架》 这本书的介绍吧!

    Markdown 在线编辑器
    Markdown 在线编辑器

    Markdown 在线编辑器

    HEX CMYK 转换工具
    HEX CMYK 转换工具

    HEX CMYK 互转工具

    HSV CMYK 转换工具
    HSV CMYK 转换工具

    HSV CMYK互换工具