Unlike other answers, offer Ninject not mainly about making your code more robust. This is a dependent injection that makes your code more testable! Dependency injection can be used without an IoC container, creating everything in the trading posts. But I am sure that it is easy to replace some parts for integration tests (do not use Ninject in unit tests) - a good side effect.
IoC containers, such as Ninject, are mainly about combining your classes into working software. In small projects, this can easily be done with the help of some plants. But as your application grows, factories become more complex. Imagine an application that has various services, some of which are reused, others are created for each use. Some of the services are also used by several components.
If you use an IoC container, you must determine exactly once how you get the service instance and what its life cycle is. On the other hand, in the factory, you need to specify how to get an instance for each class that needs an instance (for example, the new MyServiceFactory (). CreateInstance ()). In addition, you must manage the life cycle manually.
This means that as the project grows, the IoC container configuration grows linearly with the size of the project. But on the other hand, the factory is growing more pronounced, as, for example, there are services that are used throughout the application (for example, user auth).
BTW: Your example is not very good, because Rebinding is not a common action. Typically, the configuration is performed only once at application startup.
Remo gloor
source share