Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeUsing DNN Platf...Using DNN Platf...Using Modules a...Using Modules a...Service Framework - Best pattern for returning responseService Framework - Best pattern for returning response
Previous
 
Next
New Post
3/9/2014 6:20 PM
 

I've noticed after reading many examples both here in the DNN Community and within several wiki articles and other examples throughout the web that there are basically two ways that a Web API call can "return" data to the consumer.

One example (which is also the way I mostly find in the DNN_Core source is using the "Request.CreateResponse()" method to return from the Web API method.  For example:

[HttpGet]
public HttpResponseMessage TestMethod() {
     var myDTOResponseObject = new myDTO();
     return Request.CreateResponse(HttpStatusCode.OK, myDTOResponseObject);
}

Another way that I've seen is to just return the actual object (or DTO), which looks like it generally serializes the exact same way.  For example:

[HttpGet]
public myDTO TestMethod() {
     var myDTOResponseObject = new myDTO();
     return myDTOResponseObject;
}

I'm curious what the recommended pattern is, and more importantly, why one method over the other?

 
New Post
3/9/2014 10:13 PM
 
The CreateResonse method is more suited to cases where you want to control the response header details
- it creates a properly formed httpresponse structure with all the appropriate metadata and the like as part of the response wrapper.
- and then allows you to manipulate those headers etc before passing the response back - You can change headers or content type etc
- and also to completely control the content negotiation process.

The DTO method on the other hand - does not let you have this level of control - its i guess closer to the bone in terms of what it passes.

For example - if you use the DTO method there is no easy way to report a HttpStatusCode.NotFound or HttpStatusCode.BadRequest
- sure yes you could maybe do something like set the idkey of the dto to -1 or some such
- but thats not really ideal if ajax is the main consumer of your data.

If you use the CreateResponse method - you can easily pass back Request.CreateResponse(HttpStatusCode.NotFound);
which ajax will automatically know how to respond to

So as such - to a large extent it comes down to what is consuming your data - if its ajax then you should be handling the content response appropriately.
If you are maybe doing some other forms of process to process communication then DTO may be suited - as it is somewhat lighter on bandwidth.

Have a look at the response you get from each sort of request using something like fiddler - that would give you a better idea of the bandwidth/content that is travelling around under the hood - beside you actual serialized request data.
 
New Post
3/10/2014 3:09 PM
 

Thanks Wes, that helps tremendously.  That gives me somewhere to start in evaluating each use-case to decide if/when to use CreateResponse.  At first glance, after reading your response, I think it is safe to say that using the CreateResponse pattern is what we need for most of what we are planning.

Again, thank you!

 
Previous
 
Next
HomeHomeUsing DNN Platf...Using DNN Platf...Using Modules a...Using Modules a...Service Framework - Best pattern for returning responseService Framework - Best pattern for returning response


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out