I tried to create one more new ASP.NET MVC application (with Entity Framework) and a little frustrated again.
For example, I have a database with the following tables:
Table Users:
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Title] [nvarchar](max) NOT NULL,
[Company] [nvarchar](50) NOT NULL,
[Phone] [nvarchar](50) NULL,
[CompanyUrl] [nvarchar](max) NULL,
[EmailPlainText] [bit] NULL,
[ProfileImage] [nvarchar](max) NULL,
[ProfileDescription] [nvarchar](max) NULL,
[ProfileDocument] [nvarchar](max) NULL,
[ProfileWebSite] [nvarchar](max) NULL,
[Facebook] [nvarchar](max) NULL,
[Linkedin] [nvarchar](max) NULL,
[MySpace] [nvarchar](max) NULL,
[Twitter] [nvarchar](max) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[Services](
[ServiceID] [int] IDENTITY(1,1) NOT NULL,
[ServiceName] [nvarchar](250) NOT NULL,
[ServiceOrder] [int] NOT NULL,
CONSTRAINT [PK_Services] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[UserServices](
[UserID] [int] NOT NULL,
[ServiceID] [int] NOT NULL,
CONSTRAINT [PK_UserServices] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[GeographicalAreas](
[GeoAreaID] [int] IDENTITY(1,1) NOT NULL,
[GeoAreaName] [nvarchar](250) NOT NULL,
CONSTRAINT [PK_GeographicalAreas] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[UserGeoAreas](
[UserID] [int] NOT NULL,
[GeoAreaID] [int] NOT NULL,
CONSTRAINT [PK_UserGeoAreas] PRIMARY KEY CLUSTERED
so, how can we see, there is : one table for User info, 2 table-dictionaries (Services and GeographicalAreas) and 2 tables (UserServices and UserGeoAreas) for relationship many-to-many between User and table-dictionaries. Standard situation
Also we have 3 different pages :
- On the first page should be displayed :
FirstName, LastName, Title, Company, Services (linked to an user) and GeoAreas (linked to an user)
- On the second page:
FirstName, LastName, Title, Company, Facebook, Linkedin and Twitter
- On the third page only FirstName, LastName, ProfileImage and ProfileDocument
Also, on the filrst page should be validate attributes like “required” etc
so, how to realize it?
The first way:
Creating 3 different view-classes (3 different models) for each page, creating 3 different linq-requests (3 public methods in a class-repository), each method of controller (for each page) calls the appropriate method in class-repository
The second way:
Create one common view class (which includes all requires fields for 3 pages), one common method in a class-repository, which fills all fields, each method of controller calls the method of repository
The third way:
Creating 3 different view-classes (3 different models) for each page, one common class (which includes all requires fields for 3 pages), one method in a class-repository, which fills all fields, 3 converters to move data from the common class to the appropriate view-class.
When in doubt, go with the Single Responsibility Principle:
Your option 1 would be the correct one. Every view should have its own distinct controller and its own distinct view model (ideally being retrieved by its own distinct call to a repository).