快速实现 dapper 注入

2020年4月3日 4077点热度 6人点赞 0条评论
内容纲要

新建一个静态类吗,注入 dapper 服务。

    /// <summary>
    /// 注入dapper
    /// </summary>
    public static class DapperService
    {
        /// <summary>
        /// 注入dapper
        /// </summary>
        public static IServiceCollection AddDapperContext(this IServiceCollection services, string connectStr)
        {
            services.AddTransient<IDbConnection, SqliteConnection>(context =>
            {
                return new SqliteConnection(connectStr);
            });
            return services;
        }
    }

Startup.ConfigureServices 方法中,添加

```

使用示例
```c#
  public class TestController : ControllerBase
    {
        private readonly IDbConnection _dapperContext;

        /// 
        /// 构造函数
        /// 
        /// 
        public TestController(IDbConnection dapperCon)
        {
            _dapperContext = dapperCon;
        }

痴者工良

高级程序员劝退师

文章评论