开发者

Master-Detail View with multiple repositories using controller dependency injection

开发者 https://www.devze.com 2023-01-30 13:13 出处:网络
I\'m attempting to use Ninject to inject repositories into controllers of my MVC project. public class HomeController : Controller

I'm attempting to use Ninject to inject repositories into controllers of my MVC project.

public class HomeController : Controller
    {
        private readonly ICustomerRepository _customerRepository;
        //
        // GET: /Home/
        public HomeController(ICustomerRepository customerRepository)
        {
            _customerRepository = customerRepository;
        }
     }

This all makes sense an开发者_StackOverflowd is easy enough, but when the view gets more complicated and needs to display a master detail scenario, do I inject both repository interfaces in? Does that change if it gets 4 or 5 levels deep? (e.g. User picks customer, project, group, division, and then gets a list of people)

There were three things I came up with.

  1. Inject all the necessary repositories via the constructor.
  2. Create the concept of a super repository, or
  3. Create partial views and controllers for each of repositories.

Is there a best-practice on the pattern I should use for this? Any insight would be great.


Option 3 is the best - option 1 increases the number of dependencies for HomeController which will make it overly complicated as the number of data sources increases. Option 2 creates a similar problem for your 'super-repository' as it will need a large number of methods to support all the queries in your system.

Creating a controller each for a number of smaller forms will allow each one to be fairly simple and easier to reason about.


I've been learning toward thinking of the controller as just that --a controller of everything. If you take that approach then I think its totally fine to have it know about multiple repositories/services.

The master repository approach works as well as there's less instantiation needed on the controller side, but its a bit less explicit on what you're doing and makes the controller rely more on internal knowledge of the repository/service.

0

精彩评论

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