2

I am creating a MVC application using NINject as my IOC. I am trying to separate my business logic from controller. And so I don't want my controller constructor creates for my any Dependency Object i.e. I dont want to pass my interface in the constructor and than it gets resolved through IOC container. It should be the responsibility of the business logic layer. I am trying to achieve something like below.

public BusinessHolidayController()
{

}
// GET: BusinessHoliday
public ActionResult Index(Product product)
{
    string model = invoke<IProduct>().GetSum(product);
    return View(model);
}
5
  • its a generic method Commented Jun 15, 2016 at 4:33
  • Well that can be the busienss logic class or may be I can write a extension method for the controller to resolve dependencies and than get the required object. Commented Jun 15, 2016 at 4:36
  • 1
    Can you please elaborate a bit more? What do you mean by "Dependency Object"? Most importantly, what's the question? Commented Jun 15, 2016 at 4:37
  • Are you saying you want something like a "Component Factory" that resolves all dependencies and that that "Component Factory" must reside in the "business logic"? Commented Jun 15, 2016 at 4:39
  • Kind of. with the above syntax Commented Jun 15, 2016 at 4:40

1 Answer 1

3

What you are above to do is leaning toward Service Locator Pattern which is anti-pattern and a dangerous pattern. Its disadvantages are greater than its advantages.

There are four basic DI patterns -

  1. Constructor Injection
  2. Property Injection
  3. Method Injecton
  4. Ambient Context

For MVC controller, you want to use Constructor Injection, because it is the best pattern of the above four patterns especially for controller.

It should be the responsibility of the business logic layer.

No. Composite Root should be placed entry point of the application such as Global.asax.

For more information, you want to read Mark Seemann's Dependency Injection in .NET.

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

1 Comment

do you mean I should resolve my dependency in Global.asax or the IOC container initializer.

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.