Reputation: 1825
I’m working on a project which uses an Assembler pattern to assemble LinqToEntity entities into Data Transfer Objects at a service level, which are then passed down to the client layer for use. The approach has been to translate the entitie objects into simplified, flat objects that provide information specific to the service call.
Eg.
// Original Entity looks something like this
public class PersonEntity
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int MotherId { get; set; }
public PersonEntity Mother { get; set; }
// lots of other properties
}
// Flattened DTO
public class PersonSummaryInfo
{
public int Id { get; set; }
public string FullName { get; set; }
public string MothersFullName { get; set; }
}
In this example, the assembler would create a PersonSummaryInfo, constructing the FullNames part of the process.
I now face an issue with some 3rd party control (Telerik ASP.NET MVC GridControl), where the control is set up to filter (using IQueryable) based on it’s Model’s properties. There idea seems to be that you have a single tier design and pump your database entities directly into the view, which I can’t stand.
Trying to incorporate it into my logic, the GridControl binds to my DTO instead of the Entity which is all good until it tries to sort anything. I pushed all of the IQueryable stuff into my service in a very Generic fasion to make it reponsible for this. Sorting attempts to sort by, say MothersFullName on the DTO (its behaviour is to pass “MothersFullName” as a string to your sorting logic), this gets pushed up to my service which through reflection attempts to sort the Entities, making use of IQueryable lazy loading, but of course when the query is executed, an Exception is thrown as “MothersFullName” is not a property of the original Entity.
Is there a good strategy that handles this sort of implementation? Is it good practice to effectively “disassemble” a DTO back to its ORM entity once its back in the Service layer of an application? Or is it better to pass down richer objects that have more knowledge of what they are (such as how to sort a full name using FirstName and LastName)?
What is key to my requirements is:
Upvotes: 3
Views: 837
Reputation: 1825
Went with this solution.
Create sql Views containing the Grid specific columns. Then made data transfer objects use properties that exactly match those of the View. Not the cleanest or strongest way to achieve this, but at least it gets my Data references out of my projects that don't need it.
Did not use dynamic linq, instead kept my generic approach and used reflection to get column names for matching. Did not go for Telerik's Open Access just because we already have a whole service layer implemented, but it sounds like a nice solution.
It's all still IQueryable, so efficiently it works very very well (just relies on developers to ensure the GridViews match the GridViewModels, property for property).
Upvotes: 0
Reputation: 1108
You have a couple of options ahead of yourself. First of all it is true that the is able to be bound to a IQueryable, because that is the fastest (and most common) way to do it, in terms of development time of course.
In your case (a full blown Service layer on top of the ORM) the situation is a bit different. I would personally suggest you dig a little bit and provide custom bindings to your grid. You get an GridCommand object that you can query for sorting and filtering and use that to ask your service layer for data. This is a good place to mention an easy way to solve the problem you are facing (this being that your expressions are based on the DTO properties). You can try using Dynamic Linq. Just construct your string queries from the expressions and pass them down to your DAL.
As a matter of fact, this is a suggested best practice by another of Telerik's products the OpenAccess ORM. The OpenAccess SDK contains a couple of examples (especially the WCF Plain Services one) that use a similar architecture to yours. The product also provides a code generation tool that delivers a whole service layer.
Upvotes: 4
Reputation: 12192
Are you only using the RadGrid for its out of the box sorting function? I ran across this same problem and ended up just using RadListView and plugging a pager/sort into it. Using the template, you can tell it exactly what to sort on through the SortExpession property. Then it just about firing the right event. Here's my event handler, you can set whatever you can get your hands on to fire it. I'm not sure if this is a solution but hopefully it can help you find one:
protected void SortSearchTickets(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var selectedValue = e.Value;
lsvSearchResults.SortExpressions.Clear();
var sortExp = new RadListViewSortExpression();
switch (selectedValue)
{
case "ID":
sortExp.FieldName = "TicketID";
break;
case "TicketType":
sortExp.FieldName = "TypeDescription";
break;
case "Subject":
sortExp.FieldName = "Subject";
break;
case "Status":
sortExp.FieldName = "Status.Key";
break;
case "DueDateDesc":
sortExp.FieldName = "DueDate";
sortExp.SortOrder = RadListViewSortOrder.Descending;
break;
case "DueDateAsc":
sortExp.FieldName = "DueDate";
sortExp.SortOrder = RadListViewSortOrder.Ascending;
break;
case "Assigned To":
sortExp.FieldName = "AssignedTo.Key";
break;
case "Assigned By":
sortExp.FieldName = "AssignedBy.Key";
break;
default:
break;
}
lsvSearchResults.SortExpressions.AddSortExpression(sortExp);
lsvSearchResults.Rebind();
}
Upvotes: 0