patterncsharpMinor
Concerned with Enums, JSON, and ASP.NET MVC
Viewed 0 times
mvcwithconcernedandnetjsonenumsasp
Problem
I am writing an application that uses
The problem comes with front end interaction. I have many places where I want enums to be selectable via drop down lists and the like. This works okay on the surface, but I ran across an issue with the actual relationship between enums and JSON as a language/format. These issues were not overcome by using the traditional
Essentially put, MSDN reports the following; from the MSDN
Enumeration member values are treated as numbers in JSON, which is
different from how they are treated in data contracts, where they are
included as member names. For more information about the data contract
treatment, see Enumeration Types in Data Contracts.
For example, if you have public enum Color {red, green, blue, yellow,
pink}, serializing yellow produces the number 3 and not the string
"yellow".
All enum members are serializable. The EnumMemberAttribute and the
NonSerializedAttribute attributes are ignored if used.
It is possible to deserialize a nonexistent enum value - for example,
the value 87 can be deserialized into the previous Color enum even
though there is no corresponding color name defined.
A flags enum is not special and is treated the same as any other enum.
Now what this meant is that even if I used fancy footwork to convert my enums into strings for my dropdownlists, they still get saved back to the database as integers; Or rather it is more sufficient to say that they get saved in Raven as strings, but still 'considered' as integers upon deserialization back to the javascript components/MVC.
Using a lot of time and patience, I did manage to find "work arounds" for this; They were cumbersome and obtuse, I did not like them
ASP.NET MVC for its front end, and for the back end, it uses RavenDB to store data. This has so far worked out great, but I am hitting a huge wall with the use of enum in selecting things.The problem comes with front end interaction. I have many places where I want enums to be selectable via drop down lists and the like. This works okay on the surface, but I ran across an issue with the actual relationship between enums and JSON as a language/format. These issues were not overcome by using the traditional
[JsonConverter(typeof(StringEnumConverter))] attribute in my models.Essentially put, MSDN reports the following; from the MSDN
Enumeration member values are treated as numbers in JSON, which is
different from how they are treated in data contracts, where they are
included as member names. For more information about the data contract
treatment, see Enumeration Types in Data Contracts.
For example, if you have public enum Color {red, green, blue, yellow,
pink}, serializing yellow produces the number 3 and not the string
"yellow".
All enum members are serializable. The EnumMemberAttribute and the
NonSerializedAttribute attributes are ignored if used.
It is possible to deserialize a nonexistent enum value - for example,
the value 87 can be deserialized into the previous Color enum even
though there is no corresponding color name defined.
A flags enum is not special and is treated the same as any other enum.
Now what this meant is that even if I used fancy footwork to convert my enums into strings for my dropdownlists, they still get saved back to the database as integers; Or rather it is more sufficient to say that they get saved in Raven as strings, but still 'considered' as integers upon deserialization back to the javascript components/MVC.
Using a lot of time and patience, I did manage to find "work arounds" for this; They were cumbersome and obtuse, I did not like them
Solution
It seems like you've gone to more trouble to avoid using
Short story: Your co-worker is right,
Edit: Use Json.Net to better serialize enums
https://stackoverflow.com/a/2870420/1812944
enum than you would have had just using them. enums are just numbers with a mask, converting to an int to an enum and vica versa is quite simple in C#, they also have a ToString which makes using their string value super simple as-well. I probably don't grasp the troubles you were having with using enums, but they are certainly more efficient than using strings. I think the helper methods you've created are great and it the implementation details of your project are certainly up to your team, and depending on the size and longevity of the project you may decide to continue using the string system. However in the future I would highly recommend using enums instead, and perhaps make similar helper methods which help with the troublesome conversions.Short story: Your co-worker is right,
enums are better, but it is ultimately up to your team on the implementation of this. If it is worth your time, change it, else use enums on your next project.Edit: Use Json.Net to better serialize enums
https://stackoverflow.com/a/2870420/1812944
Context
StackExchange Code Review Q#44066, answer score: 3
Revisions (0)
No revisions yet.