[CSHARP-385] Add BsonIgnoreIfDefault Attribute Created: 27/Jan/12 Updated: 02/Apr/15 Resolved: 14/Feb/12 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.3.1 |
| Fix Version/s: | 1.4 |
| Type: | New Feature | Priority: | Minor - P4 |
| Reporter: | Thomas Tucker | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 0 |
| Labels: | attribute | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
C# |
||
| Description |
|
We need a BsonIgnoreIfDefault attribute. To work exactly the same as the BsonIgnoreIfNull attribute. I constantly am putting Nullable<> types in my data model to keep rarely used properties out of the database. But on the UI I have to do special magic in a ViewModel to make the type not Nullable. This would be most usefull for Bool Flags that you only care about when they are True. but also usefull for the other built in struct types. |
| Comments |
| Comment by Robert Stam [ 08/Feb/12 ] | ||
|
There is an initial implementation of BsonIgnoreIfDefault in github. This version does NOT deprecate BsonIgnoreIfNull, which can continue to be used with reference classes when all you want to do is ignore nulls. An option would be to deprecate BsonIgnoreIfNull, since anything you can do with it you can also do with BsonIgnoreIfDefault (although it requires you to make the small mental leap of realizing that null is just another default value). Deprecating BsonIgnoreIfNull would have the downside of breaking backward compatibility. | ||
| Comment by Alexander Nagy [ 07/Feb/12 ] | ||
|
Also the work around Robert mentions is not fully complete - you will not be able to use it in cases where the default value is not a constant such as DateTime.MinValue. Robert and I have previously discussed this and I'm working on a fix. | ||
| Comment by Thomas Tucker [ 01/Feb/12 ] | ||
|
Thanks for the workaround I didnt know about it. I asked in the forums and was sent here | ||
| Comment by Robert Stam [ 01/Feb/12 ] | ||
|
That's not to say we shouldn't add a BsonIgnoreIfDefault attribute. If you couldn't find this functionality others probably haven't either. It's also been suggested that BsonIgnoreIfNull is really just a special case of BsonIgnoreIfDefault for reference classes. But if you need an immediate workaround you can use the code above. | ||
| Comment by Robert Stam [ 01/Feb/12 ] | ||
|
You can already do this, just that it's part of defining what the default value is. For example:
The Flag property will only be serialized if it's not equal to its default value of false. Let me know if this doesn't meet your needs. |