<p>My dilemma is, basically, how to share an enumeration between two applications.</p>

<p>The users upload documents through a front-end application that is on the web. This application calls a web service of the back-end application and passes the document to it. The back-end app saves the document and inserts a row in the <em>Document</em> table.</p>

<p>The document type (7 possible document types: <em>Invoice</em>, <em>Contract</em> etc.) is passed as a parameter to the web service's <em>UploadDocument</em> method. The question is, what should the type (and possible values) of this parameter be?</p>

<p>Since you need to hardcode these values in both applications, I think it is O.K. to use a descriptive string (<em>Invoice</em>, <em>Contract</em>, <em>WorkOrder</em>, <em>SignedWorkOrder</em>). </p>

<p>Is it maybe a better approach to create a <em>DocumentTypes</em> enumeration in the first application, and to reproduce it also in the second application, and then pass the corresponding integer value to the web service between them?</p>

[{'answer_id': 1721, 'author': 'Peter Burns', 'author_id': 101, 'author_profile': 'https://Stackoverflow.com/users/101', 'pm_score': 3, 'selected': False, 'text': "<p>I'd suggest against passing an integer between them, simply for purposes of readability and debugging.  Say you're going through your logs and you see a bunch of 500 errors for DocumentType=4.  Now you've got to go look up which DocumentType is 4.  Or if one of the applications refers to a number that doesn't exist in the other, perhaps due to mismatched versions.</p>\n\n<p>It's a bit more code, and it rubs the static typing part of the brain a bit raw, but in protocols on top of HTTP the received wisdom is to side with legible strings over opaque enumerations.</p>\n"}, {'answer_id': 1724, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 3, 'selected': True, 'text': '<p>I can only speak about .net, but if you have an ASP.net Webservice, you should be able to add an enumeration directly to it.</p>\n\n<p>When you then use the "Add Web Reference" in your Client Application, the resulting Class should include that enum</p>\n\n<p>But this is from the top of my head, i\'m pretty sure i\'ve done it in the past, but I can\'t say for sure.</p>\n'}, {'answer_id': 1733, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 3, 'selected': False, 'text': '<p>I would still use enumeration internally but would expect consumers to pass me only the name, not the numeric value itself.</p>\n\n<p>just some silly example to illustrate:</p>\n\n<pre><code>public enum DocumentType\n{\n  Invoice,\n  Contract,\n  WorkOrder,\n  SignedWorkOrder\n}\n\n[WebMethod]\npublic void UploadDocument(string type, byte[] data)\n{\n  DocumentType docType = (DocumentType)Enum.Parse(typeof(DocumentType), type);\n}\n</code></pre>\n'}, {'answer_id': 1789, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>In .NET, enumeration values are (by default) serialized into xml with the name. For instances where you can have multiple values (<a href="http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx" rel="nofollow noreferrer">flags</a>), then it puts a space between the values. This works because the enumeration doesn\'t contain spaces, so you can get the value again by splitting the string (ie. "Invoice Contract SignedWorkOrder", using lubos\'s example).</p>\n\n<p>You can control the serialization of values of in asp.net web services using the <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx" rel="nofollow noreferrer">XmlEnumAttribute</a>, or using the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.enummemberattribute.aspx" rel="nofollow noreferrer">EnumMember</a> attribute when using WCF.</p>\n'}, {'answer_id': 1798, 'author': 'Seibar', 'author_id': 357, 'author_profile': 'https://Stackoverflow.com/users/357', 'pm_score': 2, 'selected': False, 'text': '<p>If you are consuming your Web service from a .NET page/application, you should be able to access the enumeration after you add your Web reference to the project that is consuming the service.</p>\n'}, {'answer_id': 48885, 'author': 'Bermo', 'author_id': 5110, 'author_profile': 'https://Stackoverflow.com/users/5110', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve noticed that when using "Add Service Reference" as opposed to "Add Web Reference" from VS.net, the actual enum values come across as well as the enum names.   This is really annoying as I need to support both 2.0 and 3.5 clients. I end up having to go into the 2.0 generated web service proxy code and manually adding the enum values every time I make a change!</p>\n'}, {'answer_id': 48899, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 2, 'selected': False, 'text': '<p>If you are not working with .NET to .NET SOAP, you can still define an enumerator provided both endpoints are using WSDL.</p>\n\n<pre><code>&lt;s:simpleType name="MyEnum"&gt;    \n     &lt;s:restriction base="s:string"&gt;\n          &lt;s:enumeration value="Wow"/&gt;\n          &lt;s:enumeration value="This"/&gt;\n          &lt;s:enumeration value="Is"/&gt;\n          &lt;s:enumeration value="Really"/&gt;\n          &lt;s:enumeration value="Simple"/&gt;\n     &lt;/s:restriction&gt;\n&lt;/s:simpleType&gt;\n</code></pre>\n\n<p>Its up to the WSDL -> Proxy generator tool to parse that into a enum equivalent in the client language.</p>\n'}, {'answer_id': 49583, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 2, 'selected': False, 'text': '<p>There are some fairly good reasons for not using <code>enum</code>s on an interface boundary like that. Consider <a href="http://www.25hoursaday.com/weblog/2005/08/31/WhyYouShouldAvoidUsingEnumeratedTypesInXMLWebServices.aspx" rel="nofollow noreferrer">Dare\'s post</a> on the subject.</p>\n'}]
['https://Stackoverflow.com/questions/1709', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/349/']