MVC: How to work with entities with many child entities?

后端 未结 5 2156
温柔的废话
温柔的废话 2020-12-28 22:13

In the current examples on ASP.NET MVC I see quite basic entities, with simple CRUD methods.
But I\'m not sure about what to do with more advanced models. Let me give an

5条回答
  •  粉色の甜心
    2020-12-28 22:36

    Your "CarPart" entity (class CarPartModel) can be in "stock" state (class StockCarPartModel : CarPartModel) or "replaced" state (class ReplacedCarPartModel : CarPartModel). Then:

    • GET to /carpart/index should return all entities (CarPartModel)
    • GET to /carpart/replaced should return replaced parts (ReplacedCarPartModel)
    • GET to /carpart/bycar/{id} should return parts, related to specific car (ReplacedCarPartModel)
    • GET to /carpart/inventory should return parts in stock (StockCarPartModel)

    This all is handled by "Default" route and in your CarPartController your have actions:

    public ActionResult Index() { ... }
    public ActionResult Replaced() { ... }
    public ActionResult ByCar(string carId) { ... }
    public ActionResult Inventory() { ... }
    

    I think your controller is not fat. It is normal for controller to deal with Model inheritance. The main complexity will be in your ViewModels and Views

提交回复
热议问题