James Andrew Smith
James Andrew Smith

Reputation: 1566

User Class Design Issue

Hi I wonder if anyone can solve my problem. I tried looking for a solution but no idea where to even start looking.

I am trying to create my own user system where user details are kept in a database.

I have a user class, this contains user information, for example, id, first name, last name and user type. User type is a normal user, moderator, admin.

my problem is the UserType. Sometimes its a string and sometimes its an int and I am not sure how to create a class to handle this without dupplicating the class but one with UserType as int and another as string.

For example 1 = admin 2 = moderator

Also the user class brings back different information, for example I only want to contain first name and surname and sometimes I want to contain username and active and not active.

Again I do not want to create a seperate class for each but I don't want to bring back the data I do not need.

What would be the best approach?

Upvotes: 1

Views: 298

Answers (1)

Chris
Chris

Reputation: 27609

You have several questions here which should probably be split up...

my problem is the UserType. Sometimes its a string and sometimes its an int

Don't do that then. Its a slightly trite answer but your user type is either a string or it is an int. Work out which one it really is or you want it to be and make it that.

You can use enums though of course these are hardcoded so you can't easily add or remove but they do at least have the benefit of allowing you to bridge the string to int divide.

Probably better if you need to use both would be to have a lookup in your database (ie a table that matches strings to ints) and you can then have a method somewhere that looks it up (possibly only getting the conversions from the database once and then caching them rather than do a DB call for each lookup).

Also the user class brings back different information, for example I only want to contain first name and surname and sometimes I want to contain username and active and not active.

Again I do not want to create a seperate class for each but I don't want to bring back the data I do not need.

How much data is there? In most cases you wouldn't be looking at much overhead if you bought back all the data each time and it would be easier because you would always know that a user object contained all the info it needed. If you are particularly worried about getting large amounts of data you don't need you could look at Lazy Loading which may do what you want. You will probably find though that adding an "active" flag into a data request to the DB is a lot less overhead than making a second DB call would be if you do decide you want that info.

In essence this is very likely to ever be a performance bottleneck unless the user has some massive bits of data associated with it. A username, first name and second name and things that are generally measured in bytes like that are unlikely to cause problems for you so I wouldn't worry about it until you can prove they are a performance bottleneck. At that point you can worry about Lazy Loading or similar to sort out your problems.

Upvotes: 2

Related Questions