C# – Quick way to return a list of custom objects from a page method w/o a separate BLL

.net-2.0cjsonsubsonic

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…

Best Answer

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.