tags:

views:

86

answers:

2

I have a repository base class that gets called from my controllers in my mvc application

Each controller gets it own repository

like this

public class ServiceRepository : Bluegrass.Mvc.RepositoryBase<Models.Service>
{
    public ServiceRepository()
    {
        this._db = new iSppms.Models.iSppmsDataContext();
    }
}

And gets used like

internal DataAccess.ServiceRepository repository =
        new iSppms.DataAccess.ServiceRepository();
    public ActionResult Index()
    {
        var viewData = new ViewData.ServiceIndexViewData(
            repository.GetItems<Models.Service>());
        return View(viewData);
    }

Where I am wanting to do is not have to pass the model through as the RepositoryBase is already of type Models.Service

I am looking for a better way to structure this.

public class RepositoryBase<T> where T : class
{
    public DataContext _db;

    public List<T> GetItems<T>() where T : class, Interfaces.IModel
    {
        var table = _db.GetTable<T>();
        var data = table.Where(t => !t.Deleted);

        return data.ToList();
    }
+1  A: 

Can you add the IModel bit to RepositoryBase<T>, and make GetItems<T> non-generic? Note: it is already a bad idea to re-use the generic token T (which is different in GetItems):

public class RepositoryBase<T> where T : class, Interfaces.IModel
{
    public DataContext _db;

    public List<T> GetItems()
    {
        var table = _db.GetTable<T>();
        var data = table.Where(t => !t.Deleted);

        return data.ToList();
    }
}
Marc Gravell
A: 

Perhaps this Repository Base Class article could help?

Adrian Grigore