我已经在我的应用程序中实现了存储库模式,并且在我的一些控制器中,我使用了各种不同的存储库。(未实现IoC )
UsersRepository Users;
OtherRepository Other;
Other1Repository Other1;
public HomeController()
{
this.Users = new UsersRepository();
this.Other = new OtherRepository();
this.Other1 = new Other1Repository();
}为了避免将来出现臃肿的控制器构造函数的问题,我创建了一个包装器类,其中包含所有存储库作为类的对象,并在控制器构造函数中调用该类的单个实例。
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是一个昂贵的对象吗?
发布于 2009-10-30 04:42:47
您最好在使用存储库时创建它们,而不是在构造函数中创建。
private UsersRepository _usersRepository;
private UsersRepository UsersRepository
{
get
{
if(_usersRepository == null)
{
_usersRepository = new UsersRepository();
}
return _usersRepository;
}
}然后使用属性而不是字段进行访问。
https://stackoverflow.com/questions/1646402
复制相似问题