轻量级.NET CORE ORM框架Insql公用数据库上下文使用方式

项目主页:https://rainrcn.github.io/insql

在上一文中介绍了Insql的基本使用方式,这一文将介绍另一种公用数据库上下文的使用方式。废话就不多说了,直接上代码吧。

文件结构

先看一下例子的项目文件结构

我们将在ValuesController中直接通过CommonDbContext来调用ValuesController.insql.xml中的SQL

CommonDbContext

CommonDbContext<> 只建立一个并将它用在所有地方。

//TScope 是一个范围类型,可以随意指定,但是需要与 insql type 类型对应
public class CommonDbContext<TScope> : DbContext where TScope : class
{
    public CommonDbContext(CommonDbContextOptions<TScope> options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptions options)
    {
        //SQL解析器限定为`TScope`范围
        options.UseSqlResolver<TScope>();

        //指定所用数据库
        options.UseSqlServer("数据库连接字符串");
    }
}

public class CommonDbContextOptions<TScope> : DbContextOptions<CommonDbContext<TScope>> where TScope : class
{
    public CommonDbContextOptions(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}

Startup.cs 中注册 CommonDbContext

public void ConfigureServices(IServiceCollection services)
{
    //注册Insql
    services.AddInsql();

    //注册公用的数据库上下文
    services.AddScoped(typeof(CommonDbContext<>));
    services.AddSingleton(typeof(CommonDbContextOptions<>));
}

ValuesController

ValuesController.cs 或者在其他 Controller 中使用 CommonDbContext,也可以用在 Domain Service 中具体在哪里使用取决于自己。

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly DbContext dbContext;

    //CommonDbContext<TScope> TScope限定为当前Controller,这样在insql type中需要对应这个类型。
    public ValuesController(CommonDbContext<ValuesController> dbContext)
    {
        this.dbContext = dbContext;
    }

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        //查询用户
        var user = this.dbContext.Query<UserPo>("GetUser", new { userId = "tom" });

        //添加用户
        this.dbContext.Execute("InsertUser", new UserPo
        {
            UserId = "tom",
            UserName = "tom",
            CreateTime = DateTime.Now
        });

        //查询角色列表
        var roleList = this.dbContext.Query<RolePo>("GetRoleList");

        return new string[] { "value1", "value2" };
    }
}

ValuesController.insql.xml

<!--insql type与CommonDbContext的 TScope对应-->
<insql type="InsqlExample.Controllers.ValuesController,InsqlExample" >

  <!--定义UserPo类型数据库字段到对象属性映射-->
  <map type="InsqlExample.Models.UserPo,InsqlExample">
    <key name="user_id" to="UserId" />
    <column name="user_name" to="UserName" />
    <column name="create_time" to="CreateTime" />
  </map>

  <map type="InsqlExample.Models.RolePo,InsqlExample">
    <key name="role_code" to="RoleCode" />
    <column name="role_name" to="RoleName" />
    <column name="role_order" to="RoleOrder" />
  </map>

  <select id="GetUser">
    select * from user_info where user_id = @userId
  </select>

  <insert id="InsertUser">
    insert into user_info (user_id,user_name,create_time) values (@UserId,@UserName,@CreateTime)
  </insert>

  <select id="GetRoleList">
    select * from role_info order by role_order
  </select>
</insql>

这样可以在其他位置注入 CommonDbContext 来随意使用。

可以进项目主页查看详细文档:https://rainrcn.github.io/insql

之后将会介绍在新版本中,在不需要写XML SQL的情况下使用扩展方法来实现对象的简单CURD操作

猜你喜欢

转载自www.cnblogs.com/rainrcn/p/10449390.html