3

We have a large web site written in WebForms and have begun implementing some new modules in MVC. We have a custom CMS in place. I put the data structure to hold CMS data in a BaseModel class. Now I`m wondering where would be the best palce to put the logic which calls the CMS data manager to get data relevant to the page.

On one hand, I can create a BaseModel, which in its constructor receives a page Id, and calls the CMS manager. Then build all models on that model.

On the other hand, I can do the same in a BaseController.

I can't decide which is more correct, since I cannot think of any advantage one will have over the other. Most of my experience is in WebForms, so I am wondering what would be the more ASP.NET MVC way of placing this logic.

migrated from stackoverflow.com Mar 20 '14 at 22:02

This question came from our site for professional and enthusiast programmers.

2

As with many options, both would work. Between those two I think the base controller is the more correct option. The "models" you tend to see in MVC projects these days are leaner with just properties and few to no methods.

If you want a more correct alternative, consider putting this logic in a separate "repository" class. This way you can keep the controller code lean as well. Download this sample mid-sized ASP.Net MVC sample solution which is a good tutorial on how the repository pattern can be implemented. See how the Student model is populated using the StudentRepository.

Sample ASP.NET MVC Repository Pattern Solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.