views:

36

answers:

3

Lets say I have a table called Projects in my SQL server. My dbml file has the Project object, and I use a list of Projects in multiple views of my MVC application.

The thing is: Ordering.

By default then you use linq the Projects are ordered by ID.

lets say I want them ordered by Name and then by Code.

_db.Projects.Ordery(q=>q.Name).ThenBy(q=>q.Code)

easy.

Now, I use this list of projects in several views, with different statements in the Where clause.

everywhere i need to add the same .Ordery(q=>q.Name).ThenBy(q=>q.Code)

this is not DRY.

If I were to change the order to make it first by code then by name i need to change that in several places.

What is my question: How do I DRY up the ordering. How would I have 1 place, where I define how the ordering of a list of Projects should be done.

surely I cant be the first with this kind of question. Tips will be very welcome.

+3  A: 

You could easily write your own extension method (or even normal method) to do this:

public static IQueryable<Project> InStandardOrder
    (this IQueryable<Project> source)
{
    return source.OrderBy(q => q.Name).ThenBy(q => q.Code);
}

then just use:

_db.Projects.InStandardOrder()
Jon Skeet
That's a good idea but I would have to do this for a lot of different objects which each would have a different order. like `projects`, `modules`, `sessions`. how would I do that? edit: ok i got it, this would only be called for a Project. so this would work just fine.
Stefanvds
if u have it doing different things alot of the time you will struggle to DRY this up. Only thing you can do is either have one method for each time you DO do the same thing. Or maybe a method which takes a variable!
Steve
i think for every object i would make such a method. making that more dry sounds like overkill :) in what namespace would I put this? and in which class. I'd like to have like 1 CS file with all my 'ordering preferences'
Stefanvds
+1, this is how i do it. Kind of similar to the Specification pattern (but for ordering, not filtering).
RPM1984
@Stefanvds: Do all of your types have the same properties and want to be sorted the same way? If so, they could implement an interface and let you have one extension method. If not, you need one extension method per type.
Jon Skeet
no, they do not have the same properties. thats why indeed i will need one extension per type.
Stefanvds
+2  A: 

This might not be as DRY as you like but an extension class could be created to apply to IEnumerable.

The method would order by name then code.

public static IEnumerable<Project> DefaultOrder(this IEnumerable<Project> products){
    return projects.OrderBy(p => p.Name).ThenBy(p => p.Code);
}

Then simply call the extension on the _db.Projects

Edit: Just noticed Jon Skeet beat me to the mark!

Steve
Jon Skeet always does that :D I start to think he's hired by stackoverflow :D
Stefanvds
+2  A: 

Use repository pattern. Create a repository method for each query for the given entity.

public class OrderRepository
{
    public IEnumerable<Project> GetProjects()
     {
        return projects.OrderBy(p=> p.Name).ThenBy(p=>p.Code);
     }

    public IEnumerable<Project> GetProjectsByID(int id)
    {
         return GetProjects().Where(p=>p.Id=id);
    }
}

This way you won't be repeating yourself.

Hasan Khan