<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using complex types to make calling services less&#8230; complex</title>
	<atom:link href="http://encosia.com/using-complex-types-to-make-calling-services-less-complex/feed/" rel="self" type="application/rss+xml" />
	<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/</link>
	<description>ASP.NET and AJAX code, ideas, and examples.</description>
	<lastBuildDate>Thu, 02 Feb 2012 19:50:46 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: dermot mcevoy</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-58226</link>
		<dc:creator>dermot mcevoy</dc:creator>
		<pubDate>Fri, 30 Dec 2011 15:27:02 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-58226</guid>
		<description>Dave, this is a great post, got me out of a jam. This does helps to do atomic transactions where user is sending header/details from jQuery and .net service can process it all within one call. Also like the code when handling DTO and to expand it if require additional data...

var DTO = { &#039;NewPerson&#039; : NewPerson, &#039;MoreData&#039; : MoreData };
 
$.ajax({
  type: &quot;POST&quot;,
  contentType: &quot;application/json; charset=utf-8&quot;,
  url: &quot;PersonService.asmx/AddPerson&quot;,
  data: JSON.stringify(DTO),
  dataType: &quot;json&quot;
});

Regards
Dermot</description>
		<content:encoded><![CDATA[<p>Dave, this is a great post, got me out of a jam. This does helps to do atomic transactions where user is sending header/details from jQuery and .net service can process it all within one call. Also like the code when handling DTO and to expand it if require additional data&#8230;</p>
<p>var DTO = { &#8216;NewPerson&#8217; : NewPerson, &#8216;MoreData&#8217; : MoreData };</p>
<p>$.ajax({<br />
  type: &#8220;POST&#8221;,<br />
  contentType: &#8220;application/json; charset=utf-8&#8243;,<br />
  url: &#8220;PersonService.asmx/AddPerson&#8221;,<br />
  data: JSON.stringify(DTO),<br />
  dataType: &#8220;json&#8221;<br />
});</p>
<p>Regards<br />
Dermot</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-55118</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Mon, 19 Dec 2011 07:36:31 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-55118</guid>
		<description>There&#039;s no need to add the extra complexity of the DTO for a single value. Do something like this:

&lt;pre lang=&quot;csharp&quot;&gt;[WebMethod]
public List&lt;SearchResult&gt; Search(string SearchTerm) {
  return YourSearchMethod(SearchTerm);
}&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>There&#8217;s no need to add the extra complexity of the DTO for a single value. Do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>WebMethod<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>SearchResult<span style="color: #008000;">&gt;</span> Search<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> SearchTerm<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">return</span> YourSearchMethod<span style="color: #000000;">&#40;</span>SearchTerm<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Oscar</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-55086</link>
		<dc:creator>Oscar</dc:creator>
		<pubDate>Mon, 19 Dec 2011 05:10:14 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-55086</guid>
		<description>Dave, thanks for your quick answer, according to this in the web method should instantiate the object and call the search method, also thought to send a dto with just one property assigned (the id) to implement my application according to dto design pattern, is this a good idea or not?, maybe I&#039;m a little confused trying to follow this pattern.

Greetings.</description>
		<content:encoded><![CDATA[<p>Dave, thanks for your quick answer, according to this in the web method should instantiate the object and call the search method, also thought to send a dto with just one property assigned (the id) to implement my application according to dto design pattern, is this a good idea or not?, maybe I&#8217;m a little confused trying to follow this pattern.</p>
<p>Greetings.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-55079</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Mon, 19 Dec 2011 04:46:29 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-55079</guid>
		<description>I would just send the value.</description>
		<content:encoded><![CDATA[<p>I would just send the value.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oscar</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-55077</link>
		<dc:creator>Oscar</dc:creator>
		<pubDate>Mon, 19 Dec 2011 04:30:57 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-55077</guid>
		<description>Hi Dave, first of all, thanks for your excellent articles, I have been very helpful in developing in asp.net. I have a question regarding the use of DTO&#039;s, Should I use a dto for a query that only uses a property of the object (the id) to search the database? Or is it more advisable to just send a value?, that is my question.

Thank you in advance and congratulations for your excellent work.</description>
		<content:encoded><![CDATA[<p>Hi Dave, first of all, thanks for your excellent articles, I have been very helpful in developing in asp.net. I have a question regarding the use of DTO&#8217;s, Should I use a dto for a query that only uses a property of the object (the id) to search the database? Or is it more advisable to just send a value?, that is my question.</p>
<p>Thank you in advance and congratulations for your excellent work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KS</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54485</link>
		<dc:creator>KS</dc:creator>
		<pubDate>Fri, 16 Dec 2011 20:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54485</guid>
		<description>Ok.. I will try that.. Thx</description>
		<content:encoded><![CDATA[<p>Ok.. I will try that.. Thx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54481</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:49:20 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54481</guid>
		<description>If some properties are missing in the JSON, it will work okay and those properties will be null.

One of the best ways to experiment quickly and figure out how these various cases will work is to use LINQPad. You can try all these simple examples in it without worrying about the complexity of building a whole service, client to consume it, etc.</description>
		<content:encoded><![CDATA[<p>If some properties are missing in the JSON, it will work okay and those properties will be null.</p>
<p>One of the best ways to experiment quickly and figure out how these various cases will work is to use LINQPad. You can try all these simple examples in it without worrying about the complexity of building a whole service, client to consume it, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KS</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54479</link>
		<dc:creator>KS</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:45:07 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54479</guid>
		<description>Sorry.. other question..

Hopefully this will solve my problem.. 

Is it ok if i will create myDTO for all my possible objects, reason for asking on different response JSON string will be different e.g one function return Person and COmpany other function return Person and Address..

So my question if i create general myDTO and pass only require JSON string is it going to be work??

e.g 

public class MyDTO {
  public Person person { get; set; }
  public Company company { get; set; }
  public Address address { get; set; }
}

and now i want to desirialze json string which has only Person and Address object or Person and Address object

{“Person”:{“ID”:”123″,”Name”:”Test”},”Company”:{“ID”:”123″,”Name”:”ABC”}}

Or,....

{“Person”:{“ID”:”123″,”Name”:”Test”},”Address”:{“City”:”Toronto″,”Country”:”Canada”}}


Then, I will use JavaScriptSerializer to deserialize that JSON into an object of type MyDTO, will it work or i have to create myDTO for each and every response??

Thank u very much in advance..</description>
		<content:encoded><![CDATA[<p>Sorry.. other question..</p>
<p>Hopefully this will solve my problem.. </p>
<p>Is it ok if i will create myDTO for all my possible objects, reason for asking on different response JSON string will be different e.g one function return Person and COmpany other function return Person and Address..</p>
<p>So my question if i create general myDTO and pass only require JSON string is it going to be work??</p>
<p>e.g </p>
<p>public class MyDTO {<br />
  public Person person { get; set; }<br />
  public Company company { get; set; }<br />
  public Address address { get; set; }<br />
}</p>
<p>and now i want to desirialze json string which has only Person and Address object or Person and Address object</p>
<p>{“Person”:{“ID”:”123″,”Name”:”Test”},”Company”:{“ID”:”123″,”Name”:”ABC”}}</p>
<p>Or,&#8230;.</p>
<p>{“Person”:{“ID”:”123″,”Name”:”Test”},”Address”:{“City”:”Toronto″,”Country”:”Canada”}}</p>
<p>Then, I will use JavaScriptSerializer to deserialize that JSON into an object of type MyDTO, will it work or i have to create myDTO for each and every response??</p>
<p>Thank u very much in advance..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KS</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54477</link>
		<dc:creator>KS</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54477</guid>
		<description>Wow... It&#039;s seems some hope for me....

I will try that way.. and update you.

I really appreciate your very very quick help... Thank you very much..

Thanks for you time..</description>
		<content:encoded><![CDATA[<p>Wow&#8230; It&#8217;s seems some hope for me&#8230;.</p>
<p>I will try that way.. and update you.</p>
<p>I really appreciate your very very quick help&#8230; Thank you very much..</p>
<p>Thanks for you time..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54475</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:22:59 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54475</guid>
		<description>It works the same backwards. You&#039;d need an object like:

&lt;pre lang=&quot;csharp&quot;&gt;public class MyDTO {
  public Person Person { get; set; }
  public Company Company { get; set; }
}&lt;/pre&gt;

Then, you can use JavaScriptSerializer to deserialize that JSON into an object of type MyDTO, which has properties matching the JSON&#039;s structure.</description>
		<content:encoded><![CDATA[<p>It works the same backwards. You&#8217;d need an object like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyDTO <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> Person Person <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">public</span> Company Company <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Then, you can use JavaScriptSerializer to deserialize that JSON into an object of type MyDTO, which has properties matching the JSON&#8217;s structure.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KS</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54474</link>
		<dc:creator>KS</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:21:02 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54474</guid>
		<description>Thank you very much.. 

Hopefully, it will solve my problem..

Other quick question ... not sure if this is right place or not..

How can i convert back JSON string back to object.. i know how to do it for a simple json string but in my case i got wrapped response from wcf and it&#039;s look like

{&quot;Person&quot;:{&quot;ID&quot;:&quot;123&quot;,&quot;Name&quot;:&quot;Test&quot;},&quot;Company&quot;:{&quot;ID&quot;:&quot;123&quot;,&quot;Name&quot;:&quot;ABC&quot;}}

How would i convert this Json string in to Person and Company object

Please help me..

Thanks for your reply.</description>
		<content:encoded><![CDATA[<p>Thank you very much.. </p>
<p>Hopefully, it will solve my problem..</p>
<p>Other quick question &#8230; not sure if this is right place or not..</p>
<p>How can i convert back JSON string back to object.. i know how to do it for a simple json string but in my case i got wrapped response from wcf and it&#8217;s look like</p>
<p>{&#8220;Person&#8221;:{&#8220;ID&#8221;:&#8221;123&#8243;,&#8221;Name&#8221;:&#8221;Test&#8221;},&#8221;Company&#8221;:{&#8220;ID&#8221;:&#8221;123&#8243;,&#8221;Name&#8221;:&#8221;ABC&#8221;}}</p>
<p>How would i convert this Json string in to Person and Company object</p>
<p>Please help me..</p>
<p>Thanks for your reply.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54470</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Fri, 16 Dec 2011 19:09:11 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54470</guid>
		<description>You can use an anonymous type to wrap it up arbitrarily:

&lt;pre lang=&quot;csharp&quot;&gt;Person p = new Person() {
  Name = &quot;testname&quot;,
  Phone = &quot;0000000000&quot;
};

var result = new {
  person = p
};

// This will result in what you&#039;re after.
string json = new JavaScriptSerializer().Serialize(result);&lt;/pre&gt;

Or, if you&#039;re doing that often, you might consider structuring your object to match the JSON you desire:

&lt;pre lang=&quot;csharp&quot;&gt;public class Person {
  public string Name { get; set; }
  public string Phone { get; set; }
}

public class PersonDTO {
  public Person person { get; set; }
}&lt;/pre&gt;

Serializing an instance of the PersonDTO class would result in the JSON structure you&#039;re looking for.</description>
		<content:encoded><![CDATA[<p>You can use an anonymous type to wrap it up arbitrarily:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Person p <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;testname&quot;</span>,
  Phone <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0000000000&quot;</span>
<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
var result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>
  person <span style="color: #008000;">=</span> p
<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// This will result in what you're after.</span>
<span style="color: #FF0000;">string</span> json <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> JavaScriptSerializer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Serialize</span><span style="color: #000000;">&#40;</span>result<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Or, if you&#8217;re doing that often, you might consider structuring your object to match the JSON you desire:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Phone <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PersonDTO <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> Person person <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Serializing an instance of the PersonDTO class would result in the JSON structure you&#8217;re looking for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KS</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54404</link>
		<dc:creator>KS</dc:creator>
		<pubDate>Fri, 16 Dec 2011 13:10:38 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54404</guid>
		<description>Thanks for reply..

I can use JavaScriptSerializer to build JSON string, but as i mentioned we have WCF service which have bodystyle = &quot;Wrapped&quot; so, what ever json i am sending is need to wrapped..

{&quot;person&quot;:{&quot;Name&quot;:&quot;testname&quot;,&quot;Phone&quot;:&quot;00000000&quot;}

how can i add automatic parameter name before starting of json string.

Thanks</description>
		<content:encoded><![CDATA[<p>Thanks for reply..</p>
<p>I can use JavaScriptSerializer to build JSON string, but as i mentioned we have WCF service which have bodystyle = &#8220;Wrapped&#8221; so, what ever json i am sending is need to wrapped..</p>
<p>{&#8220;person&#8221;:{&#8220;Name&#8221;:&#8221;testname&#8221;,&#8221;Phone&#8221;:&#8221;00000000&#8243;}</p>
<p>how can i add automatic parameter name before starting of json string.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54297</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Fri, 16 Dec 2011 05:15:24 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54297</guid>
		<description>In C#, you can use JavaScriptSerializer to build JSON strings.</description>
		<content:encoded><![CDATA[<p>In C#, you can use JavaScriptSerializer to build JSON strings.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KS</title>
		<link>http://encosia.com/using-complex-types-to-make-calling-services-less-complex/#comment-54274</link>
		<dc:creator>KS</dc:creator>
		<pubDate>Fri, 16 Dec 2011 03:43:14 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=849#comment-54274</guid>
		<description>@Dave.. Thanks for such a informatic session...

I was redirected by you from your previous link..

http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/#comment-54270

I am .net developer and now our company need to make iPhone web app.. we have our wcf REST service and all operation contract&#039;s body style is wrapped.. 

I am sorry to ask this question here.. But I am very new for java and jQuery...

I am developing iPhone web app in MONO Develop C#..

Where can i find JSON.stringify(),</description>
		<content:encoded><![CDATA[<p>@Dave.. Thanks for such a informatic session&#8230;</p>
<p>I was redirected by you from your previous link..</p>
<p><a href="http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/#comment-54270" rel="nofollow">http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/#comment-54270</a></p>
<p>I am .net developer and now our company need to make iPhone web app.. we have our wcf REST service and all operation contract&#8217;s body style is wrapped.. </p>
<p>I am sorry to ask this question here.. But I am very new for java and jQuery&#8230;</p>
<p>I am developing iPhone web app in MONO Develop C#..</p>
<p>Where can i find JSON.stringify(),</p>
]]></content:encoded>
	</item>
</channel>
</rss>

