简体   繁体   中英

how to have multiple classes in repository pattern (MVC)

im a bit confused about repository pattern,here i write my code then i explain:

 public  interface IRepositoryTest<T> where T:class
{
  IEnumerable<T> SelectAll(Expression<Func<T, bool>> predicate);

}

here is the implementation of the above signature:

 public class RepositoryTest<T>:IRepositoryTest<T> where T:class
{

    private CentralEntities db = null;
    private DbSet<T> table = null;

    public RepositoryTest() {
    this.db = new CentralEntities();
    table = db.Set<T>();
                        }

    public RepositoryTest(CentralEntities db)
    {

        this.db = db;
        table = db.Set<T>();
    }


    public IEnumerable<T> SelectAll(Expression<Func<T, bool>> predicate)
    {

        return table.Where(predicate).ToList();

    }

}

now in my controller if i want to use this repository i have to do something like this:

  IRepositoryTest<UserRoles> _repository = null;

    public DashbrdController(IRepositoryTest<UserRoles> _repository)
    {

        this._repository = _repository;


    }

    public DashbrdController()
    {

        this._repository = new RepositoryTest<UserRoles>();

    }

    public ActionResult DashBrd()
    {

        var rslt = _repository.SelectAll(s=>s.user_id=="myName");         
        return View();

    }

the problem here is ,in my controller i can use only one class from my model,as you see its (UserRoles),if i want to add another class to this controller how should i do it?i want to have multiple class to join them,but in the constructor of my controller i can use only one class,where is the problem?

updated:

Add this wapper class

class TransactionManager
{
    CentralEntities _ctx;
    Hashtable _repositories;
    public TransactionManager(CentralEntities ctx)
    {
        _ctx = ctx;
    }
    public RepositoryTest<T> CreateRepository<T>() where T : class
    {
        if (_repositories == null)
            _repositories = new Hashtable();
        var type = typeof(T).Name;
        if (!_repositories.Contains(type))
        {
            var repositoryType = typeof(RepositoryTest<>);
            var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), _ctx);
            _repositories.Add(type,repositoryInstance);
        }
        return (RepositoryTest<T>)_repositories[type];
    }

    public int Save()
    {
        return _ctx.Save();
    }
}

Now in the controller

public class DashbrdController:Controller
{ 
 TransactionManager _tMgr;
public DashbrdController(TransactionManager tMgr)
{
    this._tMgr=tMgr;
}

public DashbrdController()
{
    this._tMgr=new TransactionManager() ;
}

public ActionResult DashBrd()
{
    var rslt = _tMgr.CreateRepository<UserRoles>().SelectAll(s=>s.user_id=="myName");         
    return View();
}
public ActionResult AnotherDashBrd()
{
    var anotherrslt = _tMgr.CreateRepository<AnotherRoles>().SelectAll(s=>s.Name=="myName");         
    return View();
}

Also, You can check a project with source code regarding the repository pattern

https://github.com/AJEETX/RepositoryPattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM