c# - Dependency Injection in asp.net 5 custom classes, what is the correct way? -


trying understand di.

what correct way use services/dependency objects in custom classes?

do need create each class service , add dependency objects?

or should using [fromservices] (previously, [active] before beta4 ) attribute.

or there service object should passing access them?

what trying understand, how code own classes use di controllers etc.

[fromservices] mvc concept. not available other parts of asp.net 5 stack.

if want pass dependencies down chain have few options:

  1. pass service provider. quite anti-pattern because objects need depend on di container , not inverting control.
  2. pass interfaces in constructors. "pure" di might end parameter nightmare (objects take 10 arguments in constructor).
  3. similar previous 1 group dependencies in factories. more di aligned , less parameter nightmare can create factory nightmare.
  4. inject top large level objects (for example: repositories, entire systems, etc). approach nice tradeoff between dependency nightmare , coupling. of course, systems injected should independent each other. also, each system have own di container inside.

!! don't confuse di configuration. di makes sense when have dependency on contract. configuration useful when need information specific current implementation.

example: if have irepository shouldn't inject connection string because connection strings specific system connect to. there cases when connection strings don't make sense. example, inmemoryrepository not require connection string , not common dependency implementations.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -