3

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?

4
  • "objects should create only on when needed" -- where? when? Your question is not clear. "When needed" is an ambiguous phrase to use because it could mean 1.) at a certain dime during execution or 2.) in your code in come place that doesn't get called in every code path but nevertheless is statically defined in the code. Commented Mar 9, 2017 at 15:02
  • If your class requires lots of objects, but only uses them occasionaly, it's likely violating the Single Responsibility Principle. You might want to revisit your design, bearing the SOLID principles in mind. However, to actually answer your question, have you considered using Lazy<T> to create the objects only when they are actually required? Commented Mar 9, 2017 at 15:03
  • Are these objects expensive to create? Are you optimizing because you profiled your code? Commented Mar 9, 2017 at 15:03
  • @RB. Thanks, after your response I have searched about Lazy<T> and find it useful. Commented Mar 9, 2017 at 15:13

1 Answer 1

5

Lazy<T> seems to be exactly what you are looking for.

private readonly Lazy<IUnitOfWork> _lazyUnitOfWork;

public UsersController()
{
    _layzUnitOfWork = new Lazy<IUnitOfWork>(() => new UnitOfWork());
}

// Instantiates the unit of work on first use
private IUnitOfWork _unitOfWork { get { return _lazyUnitOfWork.Value; } }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. One more thing, I'm thinking of remove all initializations from Controller, create an Abstract class and add all objects there with Lazy<T> and create concrete classes on that Abstract class. Is it good idea?
@AliShahbaz: I don't see a problem with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.