1

Newbie question on ASP.Net MVC: I have a project and I manage two "Models" - let's say "Products" and "Clients". Each Model has it's own Controller, and set of Views to implement the basic CRUD operations.

Now I want to list from a different View (lets say Home Page) all Products and all Clients.

Should I create new methods in Products and Clients Controlers to list their items and call these methods from the Index view from Home? Should the Home Controller call the Products and Clients methods?

How should I correctly address this?

Thansk in advance!

Pedro

  • 1
    Possible duplicate of [Two models in one view in ASP MVC 3](https://stackoverflow.com/questions/5550627/two-models-in-one-view-in-asp-mvc-3) – Rob Anthony Sep 17 '17 at 18:40

1 Answers1

1

The answer to this question is to some degree both subjective and opinion-based. That being said..

It is fine for HomeController to call Product and Client related methods to return what will effectively become your HomeIndexViewModel (which would comprise of some kind of ProductViewModel and ClientViewModel properties - your view would access the data via Model.ProductViewModel, Model.ClientViewModel, etc).

I would go a step further and add an orchestration component, which will be responsible for handling the request and application logic required by the Index action of your HomeController. This should keep your controller thin and decoupled from the backend.

public class HomeController : Controller
{

   private readonly IOrchestrator orchestrator;

   public HomeController() : this(IOrchestrator orchestrator)
   {
   }

   public HomeController(IOrchestrator orchestrator)
   {
      this.orchestrator = orchestrator;
   }

   public async Task<ActionResult> Index()
   {
       var homeIndexViewModel = await orchestrator.GetHomeProductsAndClientsAsync();
       return View(homeIndexViewModel);
   }

}

GetHomeProductsAndClientsAsync() will obviously be a method of your FooOrchestrator class that will be handling the application logic to return a HomeIndexViewModel.

The orchestrator is passed in to HomeController as an interface (IOrchestrator, rather than the FooOrchestrator class which will implement the IOrchestrator interface) to facilitate dependency injection (though, IMHO, the approach is still beneficial without DI).

Some reading:

"Never Mind the Controller, Here is the Orchestrator" (Dino Esposito)

trashr0x
  • 6,457
  • 2
  • 29
  • 39
  • Hi trashr0x, Thank you for your opinion. That is exactly what I asked for :) Never heard of Orchestrators. I'll read what you just left me. I do like things tidy and clean. Been reading about Services and Dependency Injection. Should this be the case? Should I move the logic from the Product and Client Controllers to Product and Client Services and handle it by Dependency Injection? In this case I'd just inject both Services (NOT controllers) on my HomeController and use both of them? Thank you again for your opinion. – Pedro Cabral Sep 17 '17 at 20:34
  • 1
    Yes, you inject the services you need rather than tightly bounding any particular service to a controller - the orchestrator then becomes the middleware between your controller and data layer. – trashr0x Sep 17 '17 at 20:45
  • Hi trashr0x. Marked it as Answer! Thank you for your opinion and help! – Pedro Cabral Sep 17 '17 at 21:20
  • Glad I could help. – trashr0x Sep 17 '17 at 21:35