Resource to return from API

4 views
Skip to first unread message

David Lawton

unread,
Oct 26, 2009, 10:10:36 PM10/26/09
to open...@googlegroups.com
Hi Guys,

Still plugging away on OR building this API and its coming along nicely! 

I have a best practices question just now, in the form of the resource the API will return.

I am looking to have a standardised response returned that can include errors and system messages, so i'm wondering if its best practice to but a base class on the resources with errors collection, or to do the following.

    [XmlRoot(ElementName = "ApiResult")]
    public class ApiResult<T> where T : class 
    {
        public ApiResult() {}
        public ApiResult(T resource)
        {
            Resource = resource;
        }

        public T Resource { get; set; }
        public string[] Errors { get; set; }
    }

I am not 100% sold on this being the best way to achieve what I am looking for, and considering OR has pretty much got everything else i'm wondering if I am approaching this scenario from the wrong direction.

David

--
"I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." -- Charles Babbage

Barry Dahlberg

unread,
Oct 26, 2009, 10:18:06 PM10/26/09
to open...@googlegroups.com
Assuming you're building something RESTish, you probably want to return a resource OR an errors collection...  if there were errors you shouldnt' have modified the resource anyway.  What sort of operations are you trying to do this for?

Unless it's for debugging in which case there are better ways to go about it.

Barry

2009/10/27 David Lawton <da...@davetheninja.net>

David Lawton

unread,
Oct 26, 2009, 10:25:24 PM10/26/09
to open...@googlegroups.com
Hi Barry,

Its for things like saving, updating etc.

So say /accounts/user POST and there were domain validation errors, I would like to return them to the client UI.

David

2009/10/27 Barry Dahlberg <barry.d...@gmail.com>

Barry Dahlberg

unread,
Oct 26, 2009, 10:41:54 PM10/26/09
to open...@googlegroups.com
Sure.  The user sent you the resource though so they know what it looks like, I would send back just the errors by themselves.  There's an example at the bottom of this page which shows 1 handler returning different types of results for errors etc:


This avoids complication your resource models with (somewhat) unrelated error handling info. Is that the kind of thing you are after?

David Lawton

unread,
Oct 26, 2009, 10:46:24 PM10/26/09
to open...@googlegroups.com
Thats exactly what im looking for Barry, cheers!
Reply all
Reply to author
Forward
0 new messages