ASP.NET MVC 3 领域和 DDD 聚合根

发布于 2024-10-17 10:05:02 字数 888 浏览 4 评论 0原文

我正在构建一个网站,并正在考虑使用区域来覆盖与我即将描述的场景类似的场景。

我目前有一个包含 4 个部分的网站,我们将其称为“创建”、“管理”,第 3 部分和第 4 部分的

“创建”和“管理”是对我正在使用的域对象的操作。域对象有许多与其相关的子对象的集合。这些也需要创建和管理。

我使用产品作为示例,以免泄露任何内容,但它不太适合同一个域 - 所以请不要说“为什么你没有产品部分”

我当前的实现有一个 ManageController,它有像类别、类别、ProductsForCategory 这样的操作

我想我需要区域,但是,某些 URL 需要限定范围,所以我想要

  • /Manage/Category/8/Products
  • /Manage/Category/8/Product/1

这可以使用区域吗?我需要设置新的路由规则吗?

我的 CategoryController 的操作是否有 2 个参数,例如

public ActionResult Product(int categoryId, int productId)
{
    //get category
    var cat = GetCategory(categoryId);

    //get product
    var product = cat.Products.SingleOrDefault(x=>x.Id == productId);

    if(product == null) 
       return RedirectToAction("Index","Manage");

    return View(product);
}

,然后我会有一个传入类别 id 的路由规则?

我的这个想法正确吗?

I'm building a site and am considering using areas to cover a similar scenario to the one I'm about to describe.

I currently have a site with 4 sections, lets call these Create, Manage, Section 3 and Section 4

Create and Manage are actions on the domain object that I'm working with. The domain object has a number of collections of sub objects that relate to it. These need to be created and managed as well.

I am using Products as an example so as not to give anything away but it doesn't quite fit the same domain - so please don't say "Why don't you have a Products section"

My current implementation has a ManageController which has Actions like Categories, Category, ProductsForCategory

I'm thinking I need areas, however, some URLs will need to be scoped so I want

  • /Manage/Category/8/Products
  • /Manage/Category/8/Product/1

Is this possible using Areas? Do I need to set up new routing rules?

Would my CategoryController have 2 parameters on the action e.g.

public ActionResult Product(int categoryId, int productId)
{
    //get category
    var cat = GetCategory(categoryId);

    //get product
    var product = cat.Products.SingleOrDefault(x=>x.Id == productId);

    if(product == null) 
       return RedirectToAction("Index","Manage");

    return View(product);
}

Then I would have a routing rule that passed in the category id?

Is my thinking on this correct?

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

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

发布评论

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

评论(1

猫七 2024-10-24 10:05:02

这对于 Areas 是可能的..尽管据我了解,区域主要用于将代码构建为有意义的文件夹结构以处理大型 MVC 应用程序,而看起来您想使用它来实现嵌套路由?

要将嵌套路由映射到 /Manage/Category/8/Product/1,您可以创建“管理”区域,然后添加一条路由,如下所示:

  context.MapRoute(null,
      "Manage/{controller}/{categoryId}/{action}/{id}",  
      new
      {
          action = "Product",
          id = "1",
          categoryId = "2"
      });

然后创建一个操作方法来接受这些参数:

  public ActionResult Product(string categoryId, string id)

但是,您的问题涉及聚合 DDD 根源,所以我怀疑我只回答了问题的一部分?

This is possible with Areas.. although it's my understanding that areas are mainly recommended for structuring your code into a meaningful folder structure to deal with large-ish MVC apps, whereas it looks like you want to use it for achieving nested routes?

To map your nested route to /Manage/Category/8/Product/1 you could create your "Manage" area, and then add a route like so:

  context.MapRoute(null,
      "Manage/{controller}/{categoryId}/{action}/{id}",  
      new
      {
          action = "Product",
          id = "1",
          categoryId = "2"
      });

You then create an action method to accept those params:

  public ActionResult Product(string categoryId, string id)

However, your question talks about aggregate DDD roots, so I suspect I've only answered part of the question?

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