views:

107

answers:

2

Hi All

I might be vaguing out here but I'm looking for a nice place to put set based helper operations in linq so I can do things like;

db.Selections.ClearTemporary()

which does something like

db.DeleteAllOnSubmit(db.Selections.Where(s => s.Temporary))

Since I can figure out how to extend Table<Selection> the best I can do is create a static method in partial class of Selection (similar to Ruby) but I have to pass in the datacontext like;

Selection.ClearTemporary(MyDataContext)

This kind of sucks because I have two conventions for doing set based operations and I have to pass the data context to the static class.

I've seen other people recommending piling helper methods into a partial of the datacontext like;

myDataContext.ClearTemporarySelections();

But I feel this makes the dc a dumping ground for in-cohesive operations.

Surely I'm missing something. I hope so. What's the convention? TIA

+2  A: 
public static class LinqExtensions
{
  public static void Clear<T>(this Table<T> t, Expression<Func<T,bool>> pred)
  {
    t.DeleteAllOnSubmit(t.Where(pred));
  }
}

Usage:

db.Selections.Clear(x => x.Temporary);

If needed, the DataContext can be accessed from a Table<T>.

leppie
Gives me something else to work with but applies to all entities. Since .Selections is actually getter for a generic type Table<T> and not a class I suppose extending these are a dead end. Sorry to reply so late I never received notification by email of this.
Luke Rohde
Adding `SubmitChanges` is a terrible idea here (extremely unintuitive with unpredictable side effects). I'll turn my -1 to a +1 without it.
280Z28
I just realized you answered by question title perfectly - great answer stupid question. I've edited my question title but what's the protocol?
Luke Rohde
Yeah I was sketchy about the submit changes as it could unleash all sorts of mayhem on the unsuspecting user. However I was pretty pleased to discover Table<T> has a context reference to the parent context - I'd been looking for that in other scenarios.
Luke Rohde
@280Z28: I know it is terrible :) It was an example of how to get to the DataContext from a Table so only one parameter needs to be passed.
leppie
But you don't need the data context if you aren't submitting it.
280Z28
@280Z28: Ok, I'll remove it... but Luke did say he had the 'burden' of passing the datacontext. I am simply showing you do not need a datacontext when you have the table already :)
leppie
thanks a lot leppie
Luke Rohde
A: 

I'm thinking of creating nested classes as partials of the datacontext plus a getter that initializes and returns the nested class

public SelectionsHelperClass SelectionsHelper {
  get {
    return new SelectionsHelperClass(Selections)
  }
}

This way I can do db.SelectionsHelper.ClearTemporary() without passing in the context while keeping set based operations (collection operations) specific to Selections logically together. I haven't tested this.

Something I forgot to mentioned is that reason I want these helpers is that they are frequently shared by some but not all controllers in an asp mvc app and I'm looking for a place to refactor them too.

Luke Rohde
I've tested this and it works ok. My first usage was for a lookup table - So now I can do things like db.LookupsHelper.GetDropDownValues("User.Type"); instead of - from l in db.lookups where attribute == "User.Type" select new {l.lookupid, l.displaytext}
Luke Rohde