2

I have general questions

1) Why we need IoC in Asp.NET application ?

2) When should use it

3) if I dont use Structuremap in my MVC Application What will I miss?

Unfortunately I have problems with the DI so Please explain in simple

tereško
  • 58,060
  • 25
  • 98
  • 150
HamedFathi
  • 3,667
  • 5
  • 32
  • 72

2 Answers2

2
  1. You don't need it. Using it provides advantages but it's not required.
  2. When creating instances of types from a lower layer, e.g. a controller creating a service or a service creating a repository.
  3. Mainly IoC helps to decouple the layers. For instance, we are creating an application where the service layer is a DLL in the same folder as the web site. There's a chance that it will later be broken out into a web service. Because the web site uses IoC to create the service objects, that change will not require any adjustment to the code of the web site. We can simply change the IoC configuration and it will just work.
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0
  1. Bacause it is a good practice. Code is much clearer and easier to maintenance.
  2. For example i'm using it in controllers constructors (i pass repositories and services as arguments) and in repositories (i pass dbcontext as argument) - but it depends on your project's architecture.
  3. StructureMap is one of the Ioc containers (i personally prefer NInject or Funq) so without it you miss all advantages of using IoC.
Kamil Będkowski
  • 1,092
  • 4
  • 16
  • 36