I'm designing the User Settings for my MVC application, and right now I have ~20 boolean settings that the user can toggle. Since every user will always have every setting, I was thinking about storing each setting as a boolean on the User table. Though this would become unwieldy as the application requirements grow.
First question - is there anything wrong with having a ton of columns on your table in this situation?
I then considered using Flags, and storing the settings as one bit each in an array:
[Flags]
public enum Settings
{
WantsEmail = 1,
WantsNotifications = 2,
SharesProfile = 4,
EatsLasagna = 8
}
And then each user will have a single "Settings" column in their User row, that stores a value of 2^20, if there are 20 settings.
I'd use this to guide my efforts: What does the [Flags] Enum Attribute mean in C#?
Is this better than the former approach? Any suggestions are welcome.
It depends on what should be considered atomic from the data management perspective.
Please note that some DBMSes (such as MS SQL Server) are very efficient at storing Booleans (just one bit per Boolean field under ideal circumstances). Even those that are less than perfect will typically not spend more than one byte per Boolean (Oracle, I'm looking at you), which may become a problem only if you have millions or billions of users.
If you have only 20 values and there is no chance some day it will be 100 values - it is ok to use any of these (preferably the enum). But if there is a chance to get 100 values - you really need to build a key-value pairs table (there is a table Users, table Settings and table UsersSettings that maps one to another).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With