开发者

ASP.NET MVC 3 areas and DDD aggregate roots

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?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜