user137348
user137348

Reputation: 10332

ASP.NET MVC Controller design

Let's have an oject structure like this :

Company -> Department -> Person

-> means 1:M relation

Should I have one fat controller which serves all request for this objects or one controller per every object ?

There will be the usual CRUD operations for each object. The details (Department,Person) will be displayed on one page using grids.

Upvotes: 0

Views: 444

Answers (5)

Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi

Reputation: 44312

I don't think there is a direct relationship between your data model and your controllers. I view controllers as groupings of related functionality, from the prespective of the application. What I mean is that I typically create a controller for each big logical chunck of functionality. So if I were creating a contacts MVC web app, I might have:

  • AccountController (for authentication/authorization)
  • ContactsController (for the real business case, contacts)
  • AdminController (for super users to modify the app)

By in my data model I might have tables like, User, Contact, Address, Phone, etc.

Upvotes: 1

Martin
Martin

Reputation: 11041

I usually go with one controller per object per CRUD ... so one controller for Department and one for Person. Why?

You should either have a DTO or Repository for each. If you have a repository:

public class PersonController : Controller
{
    private IPersonRepository _personRepository;
    public PersonController(IPersonRepository personRepository)
    {
        _personRepository = personRepository;
    }
}

Then use an IoC container (I like StructureMap) for the dependencies.

Upvotes: 1

Matthew Manela
Matthew Manela

Reputation: 16752

I would have a separate controller for each. If one controller (like company) has to pull people information that is fine.

If you really want Render Action. This lets you view call into another controller to load that part of the view.

Upvotes: 1

Arnis Lapsa
Arnis Lapsa

Reputation: 47567

Following DDD - if only Company is aggregate root - then one (but still thin) controller

Upvotes: 2

Lukasz
Lukasz

Reputation: 8880

If its like an admin type thing, I would just use a single controller or use dynamic data. Otherwise I like to breakdown my controllers based on behavior for example, Room Booking, Time-sheet management etc.

Upvotes: 2

Related Questions