I have a question about ASP.NET MVC architecture. I don't understand some things clearly.
For example, I have an ASP.NET MVC application with MS SQL 2008, which uses standard Membership and additional table named "Users" to extend information about user:
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserId] [uniqueidentifier] NOT NULL,
[Firstname] [nvarchar](max) NULL,
[Lastname] [nvarchar](max) NULL,
[VoteIteration] [int] NOT NULL,
[Type] [int] NOT NULL,
[Image] [nvarchar](max) NULL,
[TwitterLink] [nvarchar](max) NULL,
[TwitterName] [nvarchar](max) NULL,
[FacebookLink] [nvarchar](max) NULL,
[FacebookName] [nvarchar](max) NULL,
[LinkedInLink] [nvarchar](max) NULL,
[LinkedInName] [nvarchar](max) NULL,
[Introduction] [nvarchar](max) NULL,
[SendDailyDigest] [bit] NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_Users] UNIQUE NONCLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Users] WITH CHECK ADD CONSTRAINT [FK_Users_aspnet_Users] FOREIGN KEY([UserId])
REFERENCES [dbo].[aspnet_Users] ([UserId])
GO
I use Entity Framework to get this data.
I have 3 similar pages, where should I show similar, but a little different data.
seems, task is very easy, but.... I can't sure that I do correctly. I create 3 similar ViewModel classes:
public class ViewPage1
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Image { get; set; }
public string Introduction { get; set; }
public string LinkedInLink { get; set; }
public string LinkedInName { get; set; }
public string FacebookLink { get; set; }
public string FacebookName { get; set; }
public string TwitterLink { get; set; }
public string TwitterName { get; set; }
public bool SendDailyDigest { get; set; }
}
public class ViewPage2
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Image { get; set; }
public string TwitterLink { get; set; }
public string TwitterName { get; set; }
public bool SendDailyDigest { get; set; }
}
public class ViewPage3
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Image { get; set; }
public string FacebookLink { get; set; }
public string FacebookName { get; set; }
public string TwitterLink { get; set; }
public string TwitterName { get; set; }
public bool SendDailyDigest { get; set; }
}
then create 3 methods to get data from repository:
public ViewPage1 UserPage1(string Username)
{
var query = from i in _dataContext.Users
where i.aspnet_Users.UserName == Username
select new UserProfile()
{
Firstname = i.Firstname,
Lastname = i.Lastname,
Email = i.aspnet_Users.aspnet_Membership.Email,
Introduction = i.Introduction,
FacebookLink = i.FacebookLink,
FacebookName = i.FacebookName,
LinkedInLink = i.LinkedInLink,
LinkedInName = i.LinkedInName,
Image = i.Image,
SendDailyDigest = i.SendDailyDigest,
TwitterLink = i.TwitterLink,
TwitterName = i.TwitterName
};
return query.FirstOrDefault();
}
and similar for rest 2. Which things I don't like in it:
1. page1 and page2 can be very similar, for example only differences in 1 field, but need to create one more class, one more method. Of course, I can use the same class for both pages, but one field will be not used for one page
2. names of classes/methods. They are don't have sense in it. Like ViewPage1, ViewPage2....
2. names of classes/methods. They are don't have sense in it. Like ViewPage1, ViewPage2....
You can call ViewModel classes anything you like. It's your application. Not mine or anyone elses. You have to maintain it. Use language that you understand.
rover83
1. page1 and page2 can be very similar, for example only differences in 1 field, but need to create one more class, one more method. Of course, I can use the same class for both pages, but one field will be not used for one page
You can call ViewModel classes anything you like. It's your application. Not mine or anyone elses. You have to maintain it. Use language that you understand.
I understand it. I mean, that names don't make sense.
About methods to get data from DB. Is it normal to have 3 similar methods?
What happens if one of the subclasses needs another property or a change to an existing property? If you have just one superclass, you might have to go and make changes everywhere the superclass is referenced, even though most places do not need to know
about the change.
You could just have easily asked - why have separate tables in your database?
You could just have easily asked - why have separate tables in your database?
because tables are fully different and mean 1 entity = 1 table (approx).
my example with pages - model of pages are very similar, can be different only in one field. Why I think about it - there are many very similar methods to get data from DB. Each method is used only one time, have ugly name (like "GetUserForPageX")...
You can create one ViewModel if you like and reuse it in a lot of pages. So long as your application works, there is no right or wrong answer - just preferences based on a blend of pragmatism and recommended approaches.
So long as your application works, there is no right or wrong answer - just preferences based on a blend of pragmatism and recommended approaches.
I do not agree with you. Application should be flexible to change, code should be easy to understand and easy to localize and fix bugs. Therefore architecture is major
Thing, which excite me - there are many similar methods to get data from Data Layer without using inheritance. So, if I viewmodel class I can inheritance from base class, but method like
public UserProfileShort UserProfileShort(string Username)
{
var query = from i in _dataContext.Users
where i.aspnet_Users.UserName == Username
select new UserProfileShort()
{
ID = i.ID,
Firstname = i.Firstname,
Lastname = i.Lastname,
UserName = i.aspnet_Users.UserName,
Image = i.Image
};
return query.FirstOrDefault();
}
I can't inheritance and when I add new field for example I have to add changes to all similar methods
my example with pages - model of pages are very similar, can be different only in one field. Why I think about it - there are many very similar methods to get data from DB. Each method is used only one time, have ugly name (like "GetUserForPageX")...
There is a tradeoff for everything. If you create a monolithic 'God' class/object (see
http://en.wikipedia.org/wiki/God_object) it will have the propensity to be difficult to change without affecting other code within it. Typically the idea of 'Separation of Concerns (SoC) and the many different
methods like you eluded to that segregate functionality (method "GetUserForPageX") are actually a good way to have an application that is more resistant to change.
Lastly in all the years I have been working with code, adding a new field, is adding a new field. There might be some crafty ways with reflection to dynamically discover all fields at runtime, but typically the addition of a field at least involves some
amount of work. The idea is to minimize it and have to do it in as few places as possible, but I am not sure overall that your idea of a 'one-size-fits-all' class or even View is the answer.
rover83
Member
135 Points
157 Posts
ASP.NET MVC architecture
Jun 22, 2012 02:28 PM|LINK
Hello
I have a question about ASP.NET MVC architecture. I don't understand some things clearly.
For example, I have an ASP.NET MVC application with MS SQL 2008, which uses standard Membership and additional table named "Users" to extend information about user:
I use Entity Framework to get this data.
I have 3 similar pages, where should I show similar, but a little different data.
On Page1 I should show:
Firstname
Lastname
Email
Image
Introduction
LinkedInLink
LinkedInName
FacebookLink
FacebookName
TwitterLink
TwitterName
SendDailyDigest
On Page2 I should show approx:
Firstname
Lastname
Image
TwitterLink
TwitterName
SendDailyDigest
On Page3:
Firstname
Lastname
Email
Image
FacebookLink
FacebookName
TwitterLink
TwitterName
SendDailyDigest
seems, task is very easy, but.... I can't sure that I do correctly. I create 3 similar ViewModel classes:
public class ViewPage1 { public string Firstname { get; set; } public string Lastname { get; set; } public string Email { get; set; } public string Image { get; set; } public string Introduction { get; set; } public string LinkedInLink { get; set; } public string LinkedInName { get; set; } public string FacebookLink { get; set; } public string FacebookName { get; set; } public string TwitterLink { get; set; } public string TwitterName { get; set; } public bool SendDailyDigest { get; set; } }public class ViewPage2 { public string Firstname { get; set; } public string Lastname { get; set; } public string Image { get; set; } public string TwitterLink { get; set; } public string TwitterName { get; set; } public bool SendDailyDigest { get; set; } }public class ViewPage3 { public string Firstname { get; set; } public string Lastname { get; set; } public string Email { get; set; } public string Image { get; set; } public string FacebookLink { get; set; } public string FacebookName { get; set; } public string TwitterLink { get; set; } public string TwitterName { get; set; } public bool SendDailyDigest { get; set; } }then create 3 methods to get data from repository:
public ViewPage1 UserPage1(string Username) { var query = from i in _dataContext.Users where i.aspnet_Users.UserName == Username select new UserProfile() { Firstname = i.Firstname, Lastname = i.Lastname, Email = i.aspnet_Users.aspnet_Membership.Email, Introduction = i.Introduction, FacebookLink = i.FacebookLink, FacebookName = i.FacebookName, LinkedInLink = i.LinkedInLink, LinkedInName = i.LinkedInName, Image = i.Image, SendDailyDigest = i.SendDailyDigest, TwitterLink = i.TwitterLink, TwitterName = i.TwitterName }; return query.FirstOrDefault(); }and similar for rest 2. Which things I don't like in it:
1. page1 and page2 can be very similar, for example only differences in 1 field, but need to create one more class, one more method. Of course, I can use the same class for both pages, but one field will be not used for one page
2. names of classes/methods. They are don't have sense in it. Like ViewPage1, ViewPage2....
is it correct way to do it?
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: ASP.NET MVC architecture
Jun 22, 2012 03:07 PM|LINK
You can call ViewModel classes anything you like. It's your application. Not mine or anyone elses. You have to maintain it. Use language that you understand.
You can use standard inheritance:
public class ViewPage2 { public string Firstname { get; set; } public string Lastname { get; set; } public string Image { get; set; } public string TwitterLink { get; set; } public string TwitterName { get; set; } public bool SendDailyDigest { get; set; } } public class ViewPage3 : ViewPage2 { public string Email { get; set; } public string FacebookLink { get; set; } public string FacebookName { get; set; } } public class ViewPage1 : ViewPage3 { public string Introduction { get; set; } public string LinkedInLink { get; set; } public string LinkedInName { get; set; } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
rover83
Member
135 Points
157 Posts
Re: ASP.NET MVC architecture
Jun 22, 2012 03:37 PM|LINK
I understand it. I mean, that names don't make sense.
About methods to get data from DB. Is it normal to have 3 similar methods?
rover83
Member
135 Points
157 Posts
Re: ASP.NET MVC architecture
Jun 22, 2012 04:58 PM|LINK
why using common class is bad approach?
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: ASP.NET MVC architecture
Jun 22, 2012 05:38 PM|LINK
What happens if one of the subclasses needs another property or a change to an existing property? If you have just one superclass, you might have to go and make changes everywhere the superclass is referenced, even though most places do not need to know about the change.
You could just have easily asked - why have separate tables in your database?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
rover83
Member
135 Points
157 Posts
Re: ASP.NET MVC architecture
Jun 22, 2012 07:42 PM|LINK
because tables are fully different and mean 1 entity = 1 table (approx).
my example with pages - model of pages are very similar, can be different only in one field. Why I think about it - there are many very similar methods to get data from DB. Each method is used only one time, have ugly name (like "GetUserForPageX")...
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: ASP.NET MVC architecture
Jun 22, 2012 08:40 PM|LINK
You can create one ViewModel if you like and reuse it in a lot of pages. So long as your application works, there is no right or wrong answer - just preferences based on a blend of pragmatism and recommended approaches.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
rover83
Member
135 Points
157 Posts
Re: ASP.NET MVC architecture
Jun 25, 2012 10:44 AM|LINK
I do not agree with you. Application should be flexible to change, code should be easy to understand and easy to localize and fix bugs. Therefore architecture is major
Thing, which excite me - there are many similar methods to get data from Data Layer without using inheritance. So, if I viewmodel class I can inheritance from base class, but method like
public UserProfileShort UserProfileShort(string Username) { var query = from i in _dataContext.Users where i.aspnet_Users.UserName == Username select new UserProfileShort() { ID = i.ID, Firstname = i.Firstname, Lastname = i.Lastname, UserName = i.aspnet_Users.UserName, Image = i.Image }; return query.FirstOrDefault(); }I can't inheritance and when I add new field for example I have to add changes to all similar methods
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: ASP.NET MVC architecture
Jun 25, 2012 06:15 PM|LINK
Obviously.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
atconway
All-Star
16846 Points
2756 Posts
Re: ASP.NET MVC architecture
Jun 28, 2012 05:27 PM|LINK
There is a tradeoff for everything. If you create a monolithic 'God' class/object (see http://en.wikipedia.org/wiki/God_object) it will have the propensity to be difficult to change without affecting other code within it. Typically the idea of 'Separation of Concerns (SoC) and the many different methods like you eluded to that segregate functionality (method "GetUserForPageX") are actually a good way to have an application that is more resistant to change.
Lastly in all the years I have been working with code, adding a new field, is adding a new field. There might be some crafty ways with reflection to dynamically discover all fields at runtime, but typically the addition of a field at least involves some amount of work. The idea is to minimize it and have to do it in as few places as possible, but I am not sure overall that your idea of a 'one-size-fits-all' class or even View is the answer.