Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In mvc generally we keep separate class for different properties according to our requirement(as given below). now my question is what are the drawback if i put all properties in one modal class(as given below)?

C#
public class category
{
public int CatId { get; set; }
public string CatName { get; set; }
}

public class subcategory
{
public int subcatId { get; set; }
public string catid { get; set; }
public string subcatname { get; set; }
}

public class item
{
public string itemid { get; set; }
public int catid { get; set; }
public string subcatid { get; set; }
public string item { get; set; }
}


=========== all properties ====

C#
public class allitems
{

public string CatName { get; set; }
public string subcatname { get; set; }
public string itemid { get; set; }
public int catid { get; set; }
public string subcatid { get; set; }
public string item { get; set; }
}
Posted
Updated 30-Aug-15 20:34pm
v2

Programmatically, you have bigger object to pass around when you need to edit parts of the object (such as category name based on category id).

Logically, you have single blob of properties instead of domain objects that accurately model your app workspace.

This particular situation also shows some design problems (but it may be a requirement so take this considering your own domain):
- you should not have category and subcategory as separate objects (assuming parent - child relationship implicated by subcategory catid property) - they are both categories of various "levels" in a tree
- item could also have multiple categories so you could have List<category> categories; instead of single category (again, depending on the requirements)
- in the item, single category id would take care of sub and main category due to parent-child relationship



Instead (I would) create category like this (standard for C# classes is upper case first letter, full names are also recommended, intellisense takes care of extra typing) :
C#
public class Category {
/* since you're in a class named Category, you don't need property CategoryId, just Id suffices (this is personal preference so feel free to disregard this) :)
*/

    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
}

public class Item {
/* since you're in a class named Item, you don't need property ItemId, just Id suffices (this is personal preference so feel free to disregard this) :)
*/
int Id ;
string Name;
string OtherItemProperties...

List<category> categories;
}
</category>


Now you can have any kind of tree structure between categories and subcategories, not just one category and one subcategory.
 
Share this answer
 
The issue is that you don't know what your model actually is. Given all views see all properties you don't know which ones are going to be populated and available to you. Likewise if you need to make a change to your model it is harder to know which views are affected.

This is assuming you are using a single model for all your views. If you actually mean that one particular view needs three items sent to it then there is nothing inherently wrong with creating a new model for that view that has each of the items as a property.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900