Personally, I think it would be easier to provide a SingleOrDefault<T> method that takes a Func<int,T> selector argument. You can then provide any desired selector, including one that selects based on this table id.
public abstract class Repository<T> where T : class { public abstract T GetById( int id ); public T SingleOrDefault( Func<int,T> selector ) { return _dataContext.GetTable<T>().SingleOrDefault( selector ); } }
Using:
var myObj = repos.SingleOrDefault<MyClass>( c => c.MyClassID == id );
A strongly typed repository can then use this method to implement GetById ()
public class MyClassRepository : Repository<MyClass> { public override MyClass GetById( int id ) { return this.SingleOrDefault( c => c.MyClassID == id ); } }
tvanfosson
source share