<p>It's 2008, and I'm still torn on this one.  So I'm developing a web method that needs a complex type passed into it and returned from it.  The two options I'm toying with are:</p>

<ol>
<li><p>Pass and return <em>actual</em> business objects with both data and behavior.  When wsdl.exe is run, it will automatically create proxy classes that contain just the data portion, and these will be automatically converted to and from my real business objects on the server side.  On the client side, they will only get to use the dumb proxy type, and they will have to map them into some real business objects as they see fit.  A big drawback here is that if I "own" both the server and client side, and I want to use the same set of real business objects, I can run into certain headaches with name conflicts, etc.  (Since the real objects and the proxies are named the same.)</p></li>
<li><p>Forget trying to pass "real" business objects.  Instead, just create simple DataTransfer objects which I will map back and forth to my real business objects manually.  They still get copied to new proxy objects by wsdl.exe anyway, but at least I'm not tricking myself into thinking that web services can natively handle objects with business logic in them.</p></li>
</ol>

<p>By the way - Does anyone know how to tell wsdl.exe to <em>not</em> make a copy of the object?  Shouldn't we be able to just tell it, "Hey, use this existing type right over here. Don't copy it!"</p>

<p>Anyway, I've kinda settled on #2 for now, but I'm curious what you all think.  I have a feeling there are <em>way</em> better ways to do this in general, and I may not even be totally accurate on all my points, so please let me know what your experiences have been.</p>

<p><strong>Update</strong>: I just found out that VS 2008 has an option to reuse existing types when adding a "Service Reference", rather than creating brand new identical type in the proxy file. Sweet.</p>

[{'answer_id': 12990, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>I\'d do a hybrid. I would use an object like this</p>\n\n<pre><code>public class TransferObject\n{\n    public string Type { get; set; }\n    public byte[] Data { get; set; }\n}\n</code></pre>\n\n<p>then i have a nice little utility that serializes an object then compresses it.</p>\n\n<pre><code>public static class CompressedSerializer\n{\n    /// &lt;summary&gt;\n    /// Decompresses the specified compressed data.\n    /// &lt;/summary&gt;\n    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;\n    /// &lt;param name="compressedData"&gt;The compressed data.&lt;/param&gt;\n    /// &lt;returns&gt;&lt;/returns&gt;\n    public static T Decompress&lt;T&gt;(byte[] compressedData) where T : class\n    {\n        T result = null;\n        using (MemoryStream memory = new MemoryStream())\n        {\n            memory.Write(compressedData, 0, compressedData.Length);\n            memory.Position = 0L;\n\n            using (GZipStream zip= new GZipStream(memory, CompressionMode.Decompress, true))\n            {\n                zip.Flush();\n                var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();\n                result = formatter.Deserialize(zip) as T;\n            }\n        }\n\n        return result;\n    }\n\n    /// &lt;summary&gt;\n    /// Compresses the specified data.\n    /// &lt;/summary&gt;\n    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;\n    /// &lt;param name="data"&gt;The data.&lt;/param&gt;\n    /// &lt;returns&gt;&lt;/returns&gt;\n    public static byte[] Compress&lt;T&gt;(T data)\n    {\n        byte[] result = null;\n        using (MemoryStream memory = new MemoryStream())\n        {\n            using (GZipStream zip= new GZipStream(memory, CompressionMode.Compress, true))\n            {\n                var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();\n                formatter.Serialize(zip, data);\n            }\n\n            result = memory.ToArray();\n        }\n\n        return result;\n    }\n}\n</code></pre>\n\n<p>Then you\'d just pass the transfer object that would have the type name. So you could do something like this</p>\n\n<pre><code>[WebMethod]\npublic void ReceiveData(TransferObject data)\n{\n    Type originType = Type.GetType(data.Type);\n    object item = CompressedSerializer.Decompress&lt;object&gt;(data.Data);\n}\n</code></pre>\n\n<p>right now the compressed serializer uses generics to make it strongly typed, but you could make a method easily to take in a Type object to deserialize using originType above, all depends on your implementation.</p>\n\n<p>hope this gives you some ideas. Oh, and to answer your other question, wsdl.exe doesn\'t support reusing types, WCF does though.</p>\n'}, {'answer_id': 12994, 'author': 'jeremcc', 'author_id': 1436, 'author_profile': 'https://Stackoverflow.com/users/1436', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p><strong>Darren</strong> wrote: I\'d do a hybrid. I would use an object like this...</p>\n</blockquote>\n\n<p>Interesting idea... passing a serialized version of the object instead of the (wsdl-ed) object itself.  In a way, I like its elegance, but in another way, it seems to defeat the purpose of exposing your web service to potential third parties or partners or whatever.  How would they know what to pass?  Would they have to rely purely on documentation?  It also loses some of the "heterogeneous client" aspect, since the serialization is very .Net specific.  I don\'t mean to be critical, I\'m just wondering if what you\'re proposing is also meant for these types of use cases. I don\'t see anything wrong with using it in a closed environment though.</p>\n\n<p>I should look into WCF... I\'ve been avoiding it, but maybe it\'s time.</p>\n'}, {'answer_id': 13002, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 1, 'selected': False, 'text': "<p>oh, for sure, i only do this when i'm the consumer of the webservice or if you have some sort of controller that they request an object from and then you handle the serialization and sending rather than them directly consuming the web service. But really, if they are directly consuming the webservice, then they wouldn't need or necessarily have the assembly that would have the type in it in the first place, and should be using the objects that wsdl generates.</p>\n\n<p>And yes, what i put forth is very .NET specific because i don't like to use anything else. The only other time i consume webservices outside of .net was in javascript, but now i only use json responses instead of xml webservice responses :)</p>\n"}, {'answer_id': 19726, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': True, 'text': '<p>there is also an argument for separating the tiers - have a set of serializable objects that get passed to and from the web service and a translator to map and convert between that set and the business objects (which might have properties not suitable for passing over the wire)</p>\n\n<p>Its the approach favoured by the web service software factory <a href="http://www.codeplex.com/servicefactory" rel="nofollow noreferrer">service factory</a> and means that you can change your business objects without breaking the web service interface/contract</p>\n'}]
['https://Stackoverflow.com/questions/12982', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1436/']