Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use enum types in your WCF web services?

Tags:

enums

wcf

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?

like image 954
Darin Dimitrov Avatar asked Nov 28 '08 18:11

Darin Dimitrov


People also ask

Where do we use enum?

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.

What is the use of enum data type?

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.

What is the benefit of enum in C#?

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.

Should enums be in domain?

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.


1 Answers

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).

  • If the enumerate is input only you have no issues.
  • If the enumerate can be an out parameter then if you add a new element and you return it, old clients could have problems:
    • If the client is using a .NET-generated proxy it will break before the caller can handle it (in the deserialization)
    • Even if the generated code for the proxy supported the change (for example if it maps the enumerate to a string) the user code in the client may not process properly the new unexpected value (it could easily be a never executed path)

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.

like image 79
MMind Avatar answered Nov 10 '22 06:11

MMind