3

suppose i have an interface in some language with two simple methods

interface IConverter{
   String toString(Object o)
   Object fromString(String s)
}

this interface is implemented by many classes and there is always just the implementation of those 2 methods nothing more.

my question is, does it make sense to use dependency injection and register the classes that implement this interface in an IoC container? or should i create a new instance in each class that requires this dependency?

note that i already use an IoC container for other things in this app

  • 1
    Neither? IoC containers are the nuclear option. – Telastyn Jan 20 '15 at 15:41
  • 1
    @Telastyn can you please suggest something? – user1590636 Jan 20 '15 at 15:43
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Jan 20 '15 at 15:44
4

It depends on the current situation and the size of the project. Registering the classes that implement this interface in the IoC container and then injecting specific implementations in the objects that depend on the interface might be the way to go in some cases but might be an overkill in others.

In small projects, I prefer the benefits of seeing dependencies and passing dependent objects as parameters and not using IoC container. In bigger projects using IoC container you can delegate the responsibility for managing objects life cycle to the container and avoid the need to write objects initialization code manually in all places where a specific dependent object is needed.

The more complex your dependency graph the more benefit you will get from a IoC/DI container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.