1

I am currently building an application using .net 4.6, MVC 5 and unity 4. I have a controller which looks like the below:

public class ExamController: Controller {
 private IEducationService _eduService;    
 public ExamController(IEducationService eduService) {
  _eduService = eduService;
 }
}

And Unity injects the service with:

container.RegisterType<IEducationService, EducationService>();

Recently we are adding other Action methods to this controller which will need other services. for example, if I now have an ExperienceService, I could add to that to Unity and change the controller's constructor to take that as well:

public ExamController(IEducationService eduService, IExperienceService _expService )

problem is I don't want both available all the time, they should be available based on which Action method is called. I can make separate constructors for each, but I'm not sure then how to get Unity to make one instead of both, based on which Action method is getting called.

2 Answers 2

2

Using multiple constructors on injectables is anti-pattern.

Furthermore, constructors should be simple, so the worry of creating extra objects which aren't used is misguided (unless of course you are violating this rule). Creating objects is very cheap when there is nothing in the constructor but assignment statements, so injecting 1 or even 100 more is probably not going to have a huge impact on performance.

You should also limit the number of services a controller has. If you have more than 3 or 4, you are probably violating the Single Responsibility Principle and should consider refactoring to facade services that each have a single responsibility. The result is that you will have an object graph that looks more like a pyramid with a few dependencies at the top and those dependencies will each have a few more until you get to the bottom where there are none.

Related: Rebuttal: Constructor over-injection anti-pattern

Sign up to request clarification or add additional context in comments.

1 Comment

+1. If you're really getting worried about the list of ctor parameters is getting longer than surely problem is somewhere else. Your controller might have more than one duties.
1

Rather than have the dependencies injected and available to the entire class, you could simply resolve the dependency in the action method where you need it using the ServiceLocator:

var expService = ServiceLocator.Current.GetInstance(typeof(IExperienceService));

You would first need to register Unity's ServiceLocator:

ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));

Example

Just a quick note: The Service Locator pattern is considered an anti-pattern.

1 Comment

Why suggesting an anti-pattern? Ctor injection is the way to go as it explicitly states what this class depends on.

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.