我正在 ASP.NET Core MVC 中构建一个 Web 销售平台,构建 DbContext 的最佳方法是什么?

发布于 2025-01-17 07:57:34 字数 1437 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2025-01-24 07:57:34

在开始之前,让我们澄清一些事情,如果您现在可以观察到,所有典型的现代应用程序都会处理来自移动设备或 Web 浏览器(所有平台,如 Windows、Mac、Linux)的请求。以及来自桌面平台。
见下图:

应用程序架构:

在此处输入图像描述

因此,请求来自不同的设备,我们称之为(跨平台)但我们可以使端点通用。那里有不同的技术。您可以考虑 Asp.net Web API

现在说说你的观点。

“如何确保模型共享,这样我就没有多余的代码可以重复?”

有几种方法可以限制代码冗余。你可以考虑
SOLID 原则。在 SOLID 中,您可以专门实现
单一职责意味着一个类应该只承担一个职责此外,您可以实现继承
OOP 这样您可以在派生类中重用基本代码
当您的项目/应用程序不断扩展时。

您可以在网上找到有关上述术语的大量示例。

所以我想解释的是让你的后端应用程序
通用,以便它可以处理所有请求,无论它们在哪里
都来自.这样你处理代码会更容易
可重用性,最终也会限制冗余。

现在考虑您的域模型:

在您的 ApplicationDbContext 中,您现在可以定义所有
领域模型就像您可以将您的业务模型视为领域
模型例如:RegularVisitorsMerchantsadmins and inside team 作为 Users 域模型,您可以设计您的
ApplicationDbContext如下:

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

        public DbSet<Merchants> Merchants{ get; set; }
        public DbSet<RegularVisitors> RegularVisitors{ get; set; }
        public DbSet<User> User { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity<Merchants>().ToTable("Merchants");
            modelBuilder.Entity<RegularVisitors>().ToTable("RegularVisitors");
            modelBuilder.Entity<User>().ToTable("User");
        }
       
    }

考虑到您的要求,您可以这样继续前进。为了
您可以考虑的详细实现< code>此线程来实现
根据您的要求

希望上述步骤和实施能够

Let's clarify few things before starting, if you could observe now a days, all typical modern application handle request either from mobile device or web browser (all the platforms like Windows, Mac, Linux). and from the desktop platform.
See the drawing below:

Application Architecture:

enter image description here

Therefore, the request are coming from different devices, which we call (cross platform) but we can make the endpoint universal. There are different technology out there. You could consider Asp.net Web API

Now come to your point.

"How do I ensure the models are shared so I don't have redundant code to duplicate?"

There are couple of way to restrict code redundancy. You can consider
SOLID principle. Within the SOLID you could specifically implement
Single Responsibility means a class should only have one responsibility additionally you could implement Inheritance of
OOP so that you can reuse of the base code in your derive class
when your project/application would continuously extended.

There are bundle of example you could found online for above terms.

So what I am trying to explain is make your backend application
universal so that it can handle all the request no matter where they
are coming from. So that it would be easier for you to handle code
reusability and eventually it would restrict the redundancy as well.

Now Consider Your Domain Model:

Into your ApplicationDbContext you can now define all of your
domain model like you can think of your business model as the domain
model for example: RegularVisitors, Merchants and admins and internal team as Users domain model, You can design your
ApplicationDbContext as below:

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

        public DbSet<Merchants> Merchants{ get; set; }
        public DbSet<RegularVisitors> RegularVisitors{ get; set; }
        public DbSet<User> User { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity<Merchants>().ToTable("Merchants");
            modelBuilder.Entity<RegularVisitors>().ToTable("RegularVisitors");
            modelBuilder.Entity<User>().ToTable("User");
        }
       
    }

This is how you could move forward considering your requirement. For
details implementation you could consider this thread to implement
your requirement accordingly

Hope above steps and implementations guided you accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文