0

I am developing a site with MVC 3 and Entity Framework 4.1. I am new to MVC 3 Development with Entity Framework so was hoping for some help with some design decisions. My site only has approx 15 pages that a user can go to. Rather than have controllers for each page - I was going to just have one controller class - (but have models for each page). And in the controller class I will keep my methods for each page in regions so they are easy to expand up. So for example I was going to have a LogOn Method in my SiteController that has RedirectToAction Homepage - and the Hompage() Method will live in my SiteController in the HomePage region.

I already have a Database Designed from a colleague (there are approx 12 or so Tables in the DB) - I have been looking at how Entity Framework will create code from that - however would it be advised to keep this code in a DAL folder and still have my own models for each page? So for example - I have a FileName that will be saved in the Database. On my homepage I want to show that FileName - so is it good practice to have a string FileName on my Homepage Model and then in the HomePage() view method the model.FileName is set from db.context(get filename from db).

Would it be best then to instansiate all the db context at the instanstiation point of my SiteController or just instansiate them in the methods that require them? i.e - if Homepage View needs table 1 and table 2 then new them in the Hompage() method and if another view method needs data from or has to save data to table 3/4 then new them at start of that method?

2 Answers 2

1
  1. Use the EF Power tools to reverse engineer your database, that will create the folder structure.
  2. KEEP the one controller per page. Unless those pages are part of a wizard type data entry scenario that really are part of the same process, separate them out into multiple controllers.
  3. Right click, add new controller - use the builtin scaffolding to generate your controller using an entity framework context (make sure you build your application after step 1 above, select your context class in the "Add Controller" dialog). It will generate the views and controller for you. Repeat this for each item you want data entry for.
  4. Your context will be instantiated as part of the controllers that are already created for you - you shouldn't need to do anything else (including dispose -the generated code will dispose it for you as well since the controller is disposed)
Sign up to request clarification or add additional context in comments.

2 Comments

Adam - Thanks for your comments - I have went with the One Controller because my site as you have picked up on is a wizard type data entry - i.e - each page on the site you go to is related to each other. In Saying that from the Menu User can select reporting and admin - I think I will create seperate Controllers for them.
I also have decided to have a model for each page - because my page can show data from more than one of the DB Tables. So for example - on a page I need to show Data from File Details db table, from contact db table and from company db table. So I create them on my Model so then I can do Linq query and return what I need to, too the View.
1

First observation when you wrote:

Rather than have controllers for each page - I was going to just have one controller class

I totally disagree with that. For multiple reasons.

Google a litle about the following topics:

  • SRP (Single Responsibility Principle) aka SoC (Separation of Concerns)
  • IoC (Inversion of Control)
  • SPot (Single Point of Truth)
  • LSP (Liskov Substitution Principle)
  • OCP - Open-Close Principle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.