views:

294

answers:

3

I am using jQuery to retrieve a JSON object from a page method. I have a DAL which uses SubSonic and if I return objects created from SubSonic-generated classes I will clog up the pipes. :) You know, all public properties get serialized. I don't want a separate business layer for this application, because it's small and focused on read operations and yet another layer seems like an overkill. To avoid downloading some SubSonic bloated objects (possibly with sensitive information as well) and avoid building a separate layer I tried returning a list of objects, like this:

[WebMethod]
public static List<object> GetFiles()
{
    FileCollection collection = DB
        .Select()
        .From(DataAccess.File.Schema)
        .ExecuteAsCollection<FileCollection>();

    List<object> files = new List<object>(collection.Count);

    foreach (DataAccess.File file in collection)
    {
        files.Add(new {
                          file.FileId,
                          file.ApplicantFirstName,
                          file.ApplicantLastName,
                          file.UploadDate
                      }
        );
    }

    return files;
}

It works and I get a nice JSON object in return (disregard the DateTime value):

[{"FileId":1,"ApplicantFirstName":"Paweł","ApplicantLastName":"Krakowiak","UploadDate":"\/Date(1235656448387
)\/"}]

Is this a good approach? I am concerned about List<object> - is it worse than returning say a List<SomeDomainObject>? Performance? Something else?

This is .NET 2.0, I can't use 3.5 features. At least anonymous types work...

+1  A: 

The biggest recommendation might be to make it a "Collection" rather than a List, but with a simple webservice return, it isn't as big of a deal, as that recommendation is most typically in environments where the object still lives in a .NET assembly.

I think it is easy to read as well.

Mitchel Sellers
+1  A: 

The only downside to using List<object> instead of List<SomeDomainObject> in this scenario would be losing strongly-typed access when calling your GetFiles method directly from .net code.

Ken Browning
Thanks. I don't need to use that method in .NET anywhere, it's sole purpose is to get some data back to the client, hence it looks like List<object> will be fine.
Pawel Krakowiak
A: 

Looks like there's nothing wrong with my approach. All that I want to do is to return a JSON object to the calling client (browser) to update the UI. This application does 99% read operations, so I am fine with it. I actually started adding a Services and Domain (I keep my business entities here) layers, but I'm going to throw them away. I really try to keep it simple for this application and don't add stuff I don't need.

Pawel Krakowiak