I've heard some people saying that enums are evil and shouldn't be used in web services because of the mismatches that could occur between the server and the client if some values are assigned, or if the enum is marked with the Flags attribute. They also said that web services exposing enums are harder to maintain but couldn't really give me viable arguments. So from your experience what are the pros and cons of using enums in a WCF web service?
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them. In addition to providing a way of defining and grouping sets of integral constants, enumerations are useful for variables that have a small number of possible values.
Advantages of using Enums Enumeration provides efficient way to assign multiple constant integral values to a single variable. Reduces errors caused by transposing or mistyping numbers. Ensures forward compatibility as it is easy to change constants without affecting throughout the project. Easy maintenance.
So basically Enums are usually part of Domain, and if you dont want to map, map, map, then you should add additional dependencies to your presentation and application layers (depend on domain). If you want to keep your architecture clean as a wistle, then all you can do is - map.
The reason people recommend to avoid enums in webservices is because they create subtle backwards compatible problems.
The same applies to regular enums but in web services the problem is even more clear specially in .NET-generated proxies (see below).
By defining the parameter as a string you signal the user of your API that the value may change in the future. Even if you think that the value will never change is a good practice to be ready.
There is a good post by Dare Obasanjo on this topic.
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