Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在存储库模式(MVC)中拥有多个类_C#_Model View Controller_Repository Pattern - Fatal编程技术网

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

C# 如何在存储库模式(MVC)中拥有多个类,c#,model-view-controller,repository-pattern,C#,Model View Controller,Repository Pattern,我对存储库模式有点困惑,我在这里编写代码,然后解释: public interface IRepositoryTest<T> where T:class { IEnumerable<T> SelectAll(Expression<Func<T, bool>> predicate); } 公共接口IRepositoryTest,其中T:class { IEnumerable SelectAll(表达式谓词); } 以下是上述签名的实现:

我对存储库模式有点困惑,我在这里编写代码,然后解释:

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

}
公共接口IRepositoryTest,其中T:class
{
IEnumerable SelectAll(表达式谓词);
}
以下是上述签名的实现:

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

    }

}
公共类RepositoryTest:IRepositoryTest其中T:class
{
私有中心性db=null;
私有DbSet表=null;
公共存储测试(){
this.db=新的中心性();
table=db.Set();
}
公共存储测试(中央数据库)
{
这个.db=db;
table=db.Set();
}
公共IEnumerable SelectAll(表达式谓词)
{
返回表.Where(谓词).ToList();
}
}
现在,在我的控制器中,如果我想使用此存储库,我必须执行以下操作:

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

    }
IRepositoryTest\u repository=null;
公共DashbrdController(IRepositoryTest\u存储库)
{
此.\u repository=\u repository;
}
公共DashbrdController()
{
此._repository=new RepositoryTest();
}
公共行动结果DashBrd()
{
var rslt=\u repository.SelectAll(s=>s.user\u id==“myName”);
返回视图();
}

这里的问题是,在我的控制器中,我只能使用模型中的一个类,正如您所看到的(UserRoles),如果我想向这个控制器添加另一个类,我应该如何做?我想有多个类来加入它们,但在我的控制器的构造函数中,我只能使用一个类,问题在哪里?

更新:

添加这个wapper类

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();
    }
}
类事务管理器
{
中心性;
哈希表存储库;
公共事务经理(CentralEntities ctx)
{
_ctx=ctx;
}
public RepositoryTest CreateRepository(),其中T:class
{
如果(_==null)
_repositories=newhashtable();
var type=typeof(T).名称;
if(!\u repositories.Contains(type))
{
var repositoryType=类型(RepositoryTest);
var repositoryInstance=Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)),_ctx);
_Add(类型,repositoryInstance);
}
返回(RepositoryTest)_存储库[类型];
}
公共整数保存()
{
返回_ctx.Save();
}
}
现在在控制器中

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();
}
公共类DashbrdController:控制器
{ 
交易经理;
公共DashbrdController(TransactionManager tMgr)
{
这个。_tMgr=tMgr;
}
公共DashbrdController()
{
这是。_tMgr=new TransactionManager();
}
公共行动结果DashBrd()
{
var rslt=\u tMgr.CreateRepository().SelectAll(s=>s.user\u id==“myName”);
返回视图();
}
公共行动结果另一个dashbrd()
{

var AnothersLT=\u tMgr.CreateRepository

但这种方法很好?假设类的数量达到20个,那么控制器就会一团糟,解决方案是什么?请查看github链接,其中ITransactionManager包装了所有存储库。然后您只需要在controller@mortezasol,如果你d答案有用,请勾选正确。许多人感谢你的答案是有用的,但我认为设计应该在控制器中使用不超过5个类constructor@mortezasol,是的,您可以看到控制器在构造函数中只使用一个类,即TransactionManager。