9

I have a few repositories that all generally look like this

public class DepartmentsRepository
{
    private PersonnelActionFormDataContext db = new PersonnelActionFormDataContext();

    /// <summary>
    /// returns all departments
    /// </summary>
    /// <returns>an iquerable of all departments</returns>
    public IQueryable<Department> GetAll()
    {
        return db.Departments;
    }

    /// <summary>
    /// Get department by id
    /// </summary>
    /// <param name="id">id of the department</param>
    /// <returns>a null department if nothing is found</returns>
    public Department Get(int id)
    {
        return db.Departments.SingleOrDefault(d => d.id == id);
    }

    /// <summary>
    /// Get department by department name
    /// </summary>
    /// <param name="department">name of the department</param>
    /// <returns>a null department if nothing is found</returns>
    public Department Get(string department)
    {
        return db.Departments.SingleOrDefault(d => d.DepartmentName == department);
    }

    /// <summary>
    /// Add a department
    /// </summary>
    /// <param name="department"></param>
    public void Add(Department department)
    {
        db.Departments.InsertOnSubmit(department);
    }

I'd like to have some sort of generic base class that could save me some typing, so I started here

 public class GenericRepository<T>
{
    private PersonnelActionFormDataContext db = new PersonnelActionFormDataContext();

    public IQueryable<T> GetAll()
    {
        return db.
    }
}

how do I access the collection of Ts to return them? Is this possible?

Thanks for the help.

2
  • what is db - is this linq-to-sql, or entity framework? Commented Feb 23, 2011 at 20:22
  • That actually makes it easier - see my updated answer Commented Feb 23, 2011 at 20:30

1 Answer 1

3

For Linq-to-SQL this should work:

public IQueryable<T> GetResultL2S<T>() where T : class {
    MyDataContext db = new MyDataContext();
    return db.GetTable<T>();
}

Or, if you're using Entity Framework, then you'd do something like:

public IQueryable<T> GetResultEF<T>() {
    YourEntities db = new YourEntities();
    return db.CreateQuery<T>(String.Format("[{0}s]", typeof(T).Name));
}

That assumes your entity sets can be pluralized by tacking an s on. If that's not the case, then check out System.Data.Entity.Design.PluralizationServices.PluralizationService

Sign up to request clarification or add additional context in comments.

12 Comments

public T Get<T>(int id) where T : class { return db.GetTable<T>().SingleOrDefault(d => d.id == id); } something like this doesn't work. Any ideas?
How could it? C# doesn't know that whatever T is has an id property on it. You'd have to say where T : class, ISomeInterfaceWithAnIdPropertyOnIt
Or if an interface like that's not available, you'd have to build an expression tree from scratch, which can get obtuse. Here's a simple example (tested only with EF, not L2S) stackoverflow.com/questions/5046917/…
AHHH, good looking out. OK...so, do I have to do something like create partial classes for my dbcontext objects and then and implement a iEntity with a id property and then do something like this, where T : iEntity...then these will work. Is there a better way?
Here is a generic primary key function for L2S that I wrote a while back if you are having trouble converting the EF one. http://csainty.blogspot.com/2008/04/linq-to-sql-generic-primary-key.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.