I am currently optimizing MVC application, there are objects creating in Controller Constructor. Something like this
private readonly IUnitOfWork _unitOfWork;
private readonly GenericRepository<User> _user;
private readonly GenericRepository<UserDevice> _userDevice;
public UsersController()
{
_unitOfWork = new UnitOfWork();
_user = new GenericRepository<User>(_unitOfWork);
_userDevice = new GenericRepository<UserDevice>(_unitOfWork);
}
This is simple example but actually there are a lot more objects creating in Controller Constructor, even there need only one object in function but other objects are creating as well. I want to implement a pattern where objects should create only on when needed.
One thing is in my mind to use Abstract Factory Pattern where all objects should create but I have no idea how to implement. You guys can suggest any other solution for the problem, using pattern is just my thought. Thanks
Edit
On demand means using object in a method, like I need only _user object then why is there _userDevice creating?