简体   繁体   English

如何在存储库模式(MVC)中具有多个类

[英]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? 这里的问题是,在我的控制器中,我只能使用模型中的一个类,如您所看到的(UserRoles),如果我想向该控制器中添加另一个类,我该怎么做?我想让多个类加入它们,但是在我的控制器的构造函数中,我只能使用一个类,问题出在哪里?

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 https://github.com/AJEETX/RepositoryPattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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