在DotNet Core 2.2 Web API C#中创建3层架构 [英] Create 3 Tier Architecture In DotNet Core 2.2 Web API C#

查看:137
本文介绍了在DotNet Core 2.2 Web API C#中创建3层架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Web API Core 2.2,需要设计3层体系结构. 我该怎么办.

I am working on Web API Core 2.2 and need to design 3 tier architecture. How can I do it.

我的项目结构如下

在Web API项目中.

In Web API Project..

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<HrmsDbContext>(opt =>
              opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

在DAL(图书馆项目中,我制作了我的DBContext并提供了如下所示的连接字符串.

In DAL (Library project, I have made my DBContext and provided connectionstring like below.

有没有更好的选择,所以我没有在两个地方提供连接字符串?并以良好的方式编写3层架构.

Is there any better so that I had not provide connectionstring at two places? and write 3 tier architecture in good way.

任何帮助将不胜感激.

Any help will be appreciated.

推荐答案

层vs层

您的问题是围绕层而不是层.

You question is around layers and not tiers.

层级-层级只是应用程序组件的物理隔离.

Tiers- A Tier is simply the physical separation of your app components.

层-层充当了更多逻辑分隔符,用于分隔和组织您的实际代码.您经常会听到诸如业务逻辑层",演示层"之类的术语.这些只是在应用程序中组织所有代码的简单方法.

Layers- Layers act as more logical separators that exist to separate and organize your actual code. You'll often hear terms like "Business Logic Layer", "Presentation Layer" and others. These are simply ways to organize all of the code within your application.

如果您的Web应用程序包含在同一台计算机/服务器上运行的数据访问和业务逻辑,则您将在1层中拥有3层应用程序.

If you have a web app that contains your Data Access and Business Logic which is running on the same machine/Server, then you will have 3-layered app in 1-Tier.

现在,如果您的数据访问权限托管在其他计算机/服务器上,而您的业务也托管在其他计算机/服务器上,那么您现在将在3层中拥有3层应用程序.

Now if your Data Access is hosted on different machine/server, and your Business is also hosted in different machine/server, then you will now have a 3-layered app in 3-Tier.

设置连接字符串

您已在启动时引用了连接字符串并将其添加到服务中.您无需再次定义连接字符串,也无需通过内置DI使用db上下文. 代码看起来像这样!

You have referenced connection string in your start up and added to services. You don't need to define the connection string again and use the db context using Built in DI. The code could look like this !

开始上课

public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration)
{

    // Add DbContext using SQL Server Provider
    services.AddDbContext<PaymentDbContext>(options =>
        options.UseSqlServer(configuration.GetConnectionString("myconnectionstring"), x => x.MigrationsAssembly("Payment.Persistence")));

    return services;
}

上下文类

public class PaymentDbContext : DbContext
    {
        public PaymentDbContext(DbContextOptions<PaymentDbContext> options)
            : base(options)
        {

        }

        public DbSet<Payments> Payments { get; set; }    


    }    

使用DI访问上下文

 private readonly PaymentDbContext _context;


 public PaymentsRepository(PaymentDbContext dbContext)
 {
 _context = dbContext;
}

这篇关于在DotNet Core 2.2 Web API C#中创建3层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆