SubSonic ORM with backbone.js
-
31-05-2021 - |
Pergunta
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
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public TaskCollection GetAllTasks()
{
TaskCollection coll = new TaskCollection();
coll.Load();
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
Solução
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())
resultList.Add(new
{
TaskID = rdr[0].ToString(),
TaskDescription = rdr[1].ToString(),
});
}
Outras dicas
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.