tags:

views:

48

answers:

3

I am having issues to understand MVC model structure.I have 3 tables connected by Foreign keys. Users,UsersInCommittee,Committees .Tables can be though of as below.

User: UserId FirstName Last Name

Committees: CommitteeId Name

UsersIncommitee: Id UserID CommitteeID ExpirationDate

I need to create a Users create , edit , list page so it will show like this

First Name:John Last Name: Smith Committee : Health(1/1/2011) , Education(12/4/2010) , Management(5/3/2011)

How should be my model in this case ? Also can I directly use child object in view or I have to add them in model also ?

I really need your help . Thanks.

A: 

You have to decide what you are trying to model. When I look at MVC I can see several models

  • Database - how you store it
  • Domain - the business domain
  • View - fully prepared data ready for display
  • Request - HTTP request

You may not need one Type per conceptual model above but you should try and ensure that you don't pollute one model with the needs of another, for example don't add properties that are only about display to your domain model.

For your ViewModel you should be aiming to have little or no logic in the View. Make use of partial views to try and keep you ViewModels and Views simple

adam straughan
A: 

Using a UML diagram may help you to understand the relationships between your Model objects, View objects, and Controller(s). For Windows, you could use StarUML to generate diagrams, or ArgoUML for a non-platform specific program. It is an excellent way to have a spatial representation of everything and the way they are interacting with each other. They can even help generate code for certain languages!

Here are some general rules for MVC design:

  • Model objects handle raw data and all manipulation of it.
  • View objects should NEVER directly manipulate the data itself. This should all be handled by the Controller.
  • Generally, you should think of the Controller as the middle man who tells everyone else what to do.

Example: You have a User object that sends a message to the Controller anytime it changes. The View object is notified by the Controller that the User object has changed and that it should update the view to reflect the changes.

I would imagine your Model objects would be User, Committee. Then you could have a UserView that displays the User object's information and include another view (as a subview) called CommitteeView that displays a committee and an expiration date.

All of this depends on how you want the Model objects to interact. UML is a great place to start!

D.R.
A: 

Since it is not clear what technology you are using for your model (ideally it doesnt matter but say you use Linq2SQL model is almost like your table structure or you will need a ViewModel). In any case IMO this is how your model would look

public class User{
    public int Id {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public ISet<Commitee> BelongsToCommitee {get;}
}

public class Commitee {
    public int Id {get;set;}
    public string CommiteeName {get;set;}       
    public ISet<User> UsersInCommitee {get;set;}
}

You may want to leverage lazy loding for for these navigation/relationship properties. This object model can be simplified/translated using view models if you wish. To implement the intelligent model concept you may want to take a look at INotifyPropertyChanged

Perpetualcoder
Thanks ! I am using ASP.NET MVC with Entity Frame work
John
If you take code first approach in EF4. The above model will work for you but you will need to write mappings on your own.
Perpetualcoder