Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any general patterns for designing classes for schemaless databases in .NET?

I have done a little bit of work with mongoDB in C# but all of my code is still in development. I am wondering what useful patterns people have found in evolving their domain classes over time as new properties are created, altered and removed. I am clear that I will need to either run updates on all my stored data or make sure my domain classes know how to deal with the older format records, but over time I could imagine this becoming chaotic if a class has know how to deal with all possible form formats.

Am I over thinking this? Is this mostly just a case of using good defensive programming?

like image 886
Matthew Nichols Avatar asked Mar 06 '12 23:03

Matthew Nichols


1 Answers

  1. Adding new properties to your data objects can’t be easier. You just add them. Unless you worry about these properties being null for object which exist in the database, you don’t have to do anything else. If some users/machines use the older version of your application and your classes were marked with BsonIgnoreExtraElementsAttribute, they may not even need to update their software.
  2. Removing obsolete properties can’t be easier. You just remove them in your classes. If your classes are marked with BsonIgnoreExtraElementsAttribute, then you don’t even have to remove them in your database (in case, for example, your users have several versions of your app).
  3. Renaming class properties is also easy. The BsonElementAttribute constructor has a parameter, so you can map it to the correct property name in the database.
  4. Changing property type may require you to run an update on your data. But seriously, how often do you change property type from string to int in production?

So in many cases you won't even need to run updates on your data (unless, you change data type or your property affects index). Another point is that adding the BsonIgnoreExtraElementsAttribute is often a good practice, particularly if you are worried about properties being added and/or removed frequently. Following this practice, you can to provide older and newer versions of your application to work with all versions of records, enjoying the benefits of "schemalessness".

like image 65
Primary Key Avatar answered Sep 20 '22 13:09

Primary Key