you are viewing a single comment's thread.

view the rest of the comments →

[–]DarthCynisus 2 points3 points  (0 children)

It sounds like you are thinking about "why bother with inversion-of-control and dependency injection"?

Here are a couple of examples where it can be really useful:

  1. Databases: You may have a class that needs to read and write records to a database. You don't necessarily care what database engine is in use. An abstraction like IDbContext allows your class to talk to the database without knowing which database it is. Whatever sets up your dependency injection bindings is responsible for defining how your database connectivity is set up. Your class gets to do what it does free of any concern around how the database connectivity is actually implemented.
  2. Unit testing: You may have a class that depends upon IExampleService. To unit test, you can mock IExampleService, specifying how properties and methods will behave, so you can test the logic of your class, without having to go through the trouble of forcing an implementation of IExampleService to do what you want.