views:

741

answers:

1

I'm building a generic Repository<T> class that supports Linq to SQL, and I'd like to use a factory pattern for the DataContext, because currently I have to pass the correct context to the constructor.

Does anybody know how to determine the correct DataContext type for T, where T is a Linq to Sql Table?

+2  A: 

You do not really need to.

Just take in a DataContext and you can access the tables using GetTable<T>().

Denis Troller
So what is the purpose of the FooDataContext class that gets generated? Is it just so you don't have to specify the type in a generic method?
Adam Lassek
No, the FooDataContext that gets generated also has properties that represent the table(s) in the database. That allows you to do something like myFooDataContext.MyTable....
BFree
Its purpose is exactly that. It allows you to use a meaningful name when using it and provides intellisense. If you look into the generated code, all those properties do is call GetTable<T>().
Denis Troller