Multiple DataContext/EntitiesObject
I've implemented a repository pattern in my application and in some of my controllers I use a variety of different开发者_C百科 repositories. (No IoC implemented)
UsersRepository Users;
OtherRepository Other;
Other1Repository Other1;
public HomeController()
{
this.Users = new UsersRepository();
this.Other = new OtherRepository();
this.Other1 = new Other1Repository();
}
To avoid future issues of bloated controller constructors, I created a wrapper class that contains all the repositories as objects of the class and I call a single instance of this class in my controller constructors.
public class Repositories
{
UsersRepository Users;
OtherRepository Other;
Other1Repository Other1;
public Repositores()
{
this.Users = new UsersRepository();
this.Other = new OtherRepository();
this.Other1 = new Other1Repository();
}
}
In Controller:
Repositories Reps;
public HomeController()
{
this.Reps= new Repositories();
}
Will this impact the performance of my application now or in the future when the application is expected to grow.
Each repository creates its own DataContext/Entities so for 10 repositories, thats 10 different DataContexts/Entities.
Is DataContext/Entitie an expensive object to create in such a large number?
You might be better off only creating the repositories when you use them, instead of in the constructor.
private UsersRepository _usersRepository;
private UsersRepository UsersRepository
{
get
{
if(_usersRepository == null)
{
_usersRepository = new UsersRepository();
}
return _usersRepository;
}
}
Then use the property instead of the field for accessing.
精彩评论