TypeSwitch Pattern for generic Entity Framework Calls.


You can find the TypeSwitch code here :
http://blogs.msdn.com/b/jaredpar/archive/2008/05/16/switching-on-types.aspx

I found this useful rather than having to use wrapper classes or additional properties to switch on.
This was very well done and it seems to lend itself to base frameworks, navigation, replacement of IOC containers etc.

public static Container Entities = new Container(new Uri("SomeOdataIP"));

public static void Add<T>((T item)
{
TypeSwitch.Do(
typeof(T),
TypeSwitch.Case<Class1>(() => { Entities.AddToClass1s(item as Class1); }),
TypeSwitch.Case<Car>((() => { Entities.AddToCars(item as car); }),
TypeSwitch.Case<Tire>((() => { Entities.AddToTires(item as Tire); }),
TypeSwitch.Default(() =>
{
//Do Nothing
}));

Entities.BeginSaveChanges(null, null);

}

Share on Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *