Take the 2-minute tour ×

I am using Subsonic ORM by Rob Connery with Backbone.Js to build javascript single page demonstration application. in one of the service end point there is a contract that send all the records existing in the data source like below

[ScriptMethod(UseHttpGet = true)]
public TaskCollection GetAllTasks()
    TaskCollection coll = new TaskCollection();
    return coll;

but seems that each Task in the collection is polluted with loads of properties that are required only on server side. This is the JSON returned on request

    "__type": "DAL.Task",
    "Taskid": 1,
    "Taskname": "welcome to india",
    "Createdon": "\/Date(1334591056903)\/",
    "Modifiedon": "\/Date(1334591056903)\/",
    "ValidateWhenSaving": true,
    "DirtyColumns": [],
    "IsLoaded": true,
    "IsNew": false,
    "IsDirty": false,
    "TableName": "task",
    "ProviderName": null,
    "NullExceptionMessage": "{0} requires a value",
    "InvalidTypeExceptionMessage": "{0} is not a valid {1}",
    "LengthExceptionMessage": "{0} exceeds the maximum length of {1}",
    "Errors": []

all i require is CreatedOn,ModifiedOn and TaskName, TaskId . How do i make sure only these are sent down the wire using SubSonic ORM

share|improve this question
Most ORMs call this a "projection", basically making a query requesting a subset of columns to be returned. You may try googling for "subsonic projection", the top 4 results are all links to stack overflow. –  Michael Maddox Apr 17 '12 at 11:15
@MichaelMaddox don't understand a thing either I am dumb or have no time to view SubSonic in details speaking of which the domain name subsonic project has also expired :( –  Deeptechtons Apr 17 '12 at 11:42
add comment

2 Answers

up vote 1 down vote accepted

Here's a couple of ideas...

Use the viewmodel to autoselect the properties:

public class TaskView
    public int TaskID { get; set; }
    public string TaskDescription { get; set; }
var results = new Select().From(Tables.Task).ExecuteTypedList<TaskView>();

Use an anonymous type

var qry = new Select(new string[] { Task.Columns.TaskID, Task.Columns.TaskDescription }).From(Tables.Task);
var resultList = new List<object>();
using (IDataReader rdr = qry.ExecuteReader())
    while (rdr.Read())
            TaskID = rdr[0].ToString(),
            TaskDescription = rdr[1].ToString(),
share|improve this answer
View model might fit the bill ( but still this is overwork I am abstracting more and more) anonymous typed?? i use .net 2.0 so i don't get to use goodness c# 3.0 provides :( –  Deeptechtons Apr 17 '12 at 11:44
add comment

I don't use SubSonic, but I have to admit this does seem like a good example of where you might want to use a ViewModel, that is, a model that is populated from your Model specifically for the view. Now as far as binding the ViewModel to the Model, and/or possibly generating properties for many ViewModels from the model for the view (because generating ViewModels can definitely be tedious and error prone for a bunch of models), well, I've heard of several general solutions. I'm actually trying to find a solution I'm happy with myself; in the meantime I've been forced to hand write them until I find a better solution. I believe if you're wanting strongly typed ViewModels, you can use a tool like AutoMapper, though I've never used it myself. I've also seen solutions which use or inherit from a C# dynamic and then modify the accessors (though I think that might be slightly problematic to generate JSON from).

The primary reason I've used ViewModels is that I can easily control the format of the Dates. But that might be done better by using a different JSON serializer. Of course, the use of ViewModels can also allow you flexibility to change your data layer as needed. But I have to admit, it's been tedious. I think my implementation can be handled better with a bit of automation, but I don't know how to handle that so far.

I realize this is only a partial answer. I'm curious what other answers might come up.

share|improve this answer
add comment

Your Answer


