I'm designing a solution in .Net which will be hosted on Azure. I have a system which will allow users to specify custom characteristics for their objects which they will track over time. I am certain that I'll allow a maximum of five such properties in addition to Id, Name, and Description. In addition, I can probably restrict these to be strings. What's a good way to architect this?

有帮助吗?

解决方案

I am certain that I'll allow a maximum of five such properties in addition to Id, Name, and Description. In addition, I can probably restrict these to be strings. What's a good way to architect this?

If you know there are five, just have

  • CustomProp1
  • CustomProp2
  • CustomProp3...etc

And so forth. Also might want to have a table describing each property by user:

User, Property, Name, Description etc.
123, CustomProp1, Favorite Color, My Favorite Color 
456, CustomProp1, Favorite Song, My Favorite Song

By defining them up front you will be able to search on them, index them etc in a relational sort of way if you wanted and users would be able to add up to 5 custom properties.

Another way to is store the properties as JSON. You could store the definition of each property as well in the JSON. This gives you the ability to add more properties easily since it does not require a database change. Then, it's just an additional column on the record to store the custom properties. This is a non relational way to solve the problem and provides more flexibility to add more props in the future.

You can still add props the relational way but it requires a database change. I am not sure how often it will be required to add more custom properties.

Since you kind of know the number of custom attributes up front, an EAV solution is not the best choice. EAV is used when you don't have any idea what kind of data could be stored. It provides the maximum flexibility. I don't think in this case it's use is warranted, but you could certainly design an EAV schema to solve your problem. But you will probably end up with a complex system just to maintain 5 custom properties.

许可以下: CC-BY-SA归因
scroll top