This is a design question and I am confused about how to design my user object.
ID: 639414 • Letter: T
Question
This is a design question and I am confused about how to design my user object. As in most systems, user is the central part of my application and a lot of information scattered around my database points back to the user for example a user can have multiple orders, mulitple Addresses, and he may have won multiple prizes. If we want to talk about ONLY user and his basic information we can be done with ID, firstnmae, lastname etc. So I have two clear choices
Create a light weight user object which looks like following:
public Class User
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string SomeMoreInformation {get;set;}
}
and get all the infromation at the other information at the run time like orders and Prizes on need basis.
OR
Design a more contained object which carries all the required information with it but is little heavy:
public Class User
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string SomeMoreInformation {get;set;}
public Address HomeAddress {get;set;}
public List<Prizes> Prizes {get;set;}
}
Which Does any design model align to a good design philosophy? Are there any best practices in the kind of situation that can help me make a decision?
Explanation / Answer
To answer this question you should distinguish between information analysis phase, and design phase; even though modern software engineering methods often combine them, the model (doesn't have to be a 1073 page document, it can be in your mind or in the code as well) and level of detail differs:
In the analysis phase, your model will closely match reality, meaning the is a single concept user and some ideas about what data about a user is required.
In the design phase, it depends on what information about the user your application needs in various situations and how easy it is to retrieve the information.
For example, from looking at the user side of Stack Exchange we can deduct that:
Often <user name, avatar, reputation, gold medal count, silver medal count, bronze medal count> suffices (e.g. to display the author of a post or comment).
To display comments, only the user name is sufficient.
When someone views a user profile, much more information is required: <user name, avatar, reputation, gold medal count, silver medal count, bronze medal count, all questions, all answers, all comments, all badges, reputation changes>.
When a user logs in, authentication information and browser session are required; but these should definitely not be shown in post author information.
So here we, conceptually, have four different User classes. We might opt to use the <user name, avatar, reputation, gold medal count, silver medal count, bronze medal count>-structure for the comment author as well, since its available anyway.
You could use inheritance to combine the different kinds of UserInfo, i.e.:
class BasicUserInfo
{
public string Name { get; }
public Image Avatar { get; }
public int Reputation { get; }
public int GoldBadgeCount { get; }
public int SilverBadgeCount { get; }
public int BronzeBadgeCount { get; }
}
class UserProfileInfo: BasicUserInfo
{
public List<Post> Questions { get; }
public List<Post> Answers { get; }
public List<Comment> Comments { get; }
public List<Badge> Badges { get; }
public List<ReputationChange> Reputation { get; }
}
class UserAuthentication: BasicUserInfo
{
public string PlainTextPassword { get; } // Not really, I hope everyone knows why and
// how to do this correctly
}
class UserSession: BasicUserInfo
{
public List<Cookey> Cookey { get; set; }
public UserAuthentication Authentication { get; }
}
And then use some kind of repository to find UserInfo objects, e.g.:
class UserRepository {
public UserProfileInfo FindUserProfile(BasicUserInfo user)
{
//
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.