1

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();
    }

2 Answers 2

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps this Repository Base Class article could help?

Comments

Your Answer

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