代码之家  ›  专栏  ›  技术社区  ›  clenard

使用ADO.NET对于中的数据访问层ASP.NET核心

  •  0
  • clenard  · 技术社区  · 6 年前

    我正试着为我的客户推出一个高性能的DALASP.NET核心API。我想用ADO.NET我在设计软件架构时遇到了困难。我正在寻求帮助讨论一个好的方法。

    我所拥有的

    • 我的应用程序.API
    • MyApp.存储库(数据访问层)
    • MyApp.服务(业务逻辑)

    我来实施 IUnitOfWork 在内部 MyApp.Repositories SqlUnitOfWork 在里面 MyApp.API . Startup.cs 将注册 I单元 . 稍后,当我获得更多的数据源(Mongo等)时,我可以合并一个 UnitOfWorkFactory

    问题

    1. 我应该在中注册每个存储库吗 或者简单地将它们添加为 I单元 I单元 .

    2. 如何将连接字符串传递到 ? 我知道连接字符串应该在 我的应用程序.API

    2 回复  |  直到 6 年前
        1
  •  3
  •   clenard    6 年前

    另一个答案是不相关的,因为它是建议我使用EF,我想避免。

    这是我实现的一个原始原型。 https://github.com/lenardchristopher/AdoAspDotNetCoreTest

        2
  •  0
  •   Timothy Macharia astallaslions    6 年前

    Entity Framework

    IUnitOfWork 然后将其作为服务添加到DI容器中,然后将其注入控制器中。

    SqlUnitOfWork 实现的构造函数接收 DbContext 实例。

    在ASP.NET核心,核心 数据库上下文 添加到DI容器中 中需要或依赖于DbContext的任何其他服务 容器。

    首先,请记住在中定义连接字符串 appsettings.json 同样地。

    enter image description here

    数据库上下文 Futher reading on Configuring DbContext in EF Core

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Database")));
    }
    

    之后,将需要我们上下文的其他服务注册到DI容器将非常容易,因为容器将为我们解决该依赖关系。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<IUnitOfWork, SqlUnitOfWork>();
    }