C# 多个DataContext/EntitiesObject
我在我的应用程序中实现了一个存储库模式,在我的一些控制器中,我使用了各种不同的存储库。(没有实施国际奥委会) 为了避免将来出现臃肿的控制器构造函数的问题,我创建了一个包装类,其中包含作为类对象的所有存储库,并在控制器构造函数中调用该类的单个实例C# 多个DataContext/EntitiesObject,c#,asp.net,repository,C#,Asp.net,Repository,我在我的应用程序中实现了一个存储库模式,在我的一些控制器中,我使用了各种不同的存储库。(没有实施国际奥委会) 为了避免将来出现臃肿的控制器构造函数的问题,我创建了一个包装类,其中包含作为类对象的所有存储库,并在控制器构造函数中调用该类的单个实例 public class Repositories { UsersRepository Users; OtherRepository Other; Other1Repository Other1; public Repo
public class Repositories
{
UsersRepository Users;
OtherRepository Other;
Other1Repository Other1;
public Repositores()
{
this.Users = new UsersRepository();
this.Other = new OtherRepository();
this.Other1 = new Other1Repository();
}
}
在控制器中:
Repositories Reps;
public HomeController()
{
this.Reps= new Repositories();
}
这将影响我的应用程序现在或将来的性能,因为应用程序预计会增长
每个存储库创建自己的DataContext/实体,因此对于10个存储库,即10个不同的DataContext/实体
DataContext/Entitie是否是一个需要大量创建的昂贵对象?您最好在使用存储库时只创建存储库,而不是在构造函数中创建存储库
private UsersRepository _usersRepository;
private UsersRepository UsersRepository
{
get
{
if(_usersRepository == null)
{
_usersRepository = new UsersRepository();
}
return _usersRepository;
}
}
然后使用属性而不是字段进行访问。在使用存储库时,最好只创建存储库,而不是在构造函数中创建存储库
private UsersRepository _usersRepository;
private UsersRepository UsersRepository
{
get
{
if(_usersRepository == null)
{
_usersRepository = new UsersRepository();
}
return _usersRepository;
}
}
然后使用属性而不是字段进行访问。两者都可以。在您的示例中,您的字段是私有的,因此我将属性设置为私有。但如果你需要的话,它可以公开。两者都可以。在您的示例中,您的字段是私有的,因此我将属性设置为私有。但如果你需要的话,可以公开。