I am interested in knowing if the following scenario is a good use case for Dependency injection. The example is a little made up and may not be good OO design. Please bear with me and focus on the part that relates to the question:
Let's say I have the following classes:
class BankAccount
{
User user;
Integer accountNo;
BankAccount(User user, Integer accountNo){
....
}
}
class User
{
String firstName, lastName;
User(String firstName, String lastName)
{
....
}
}
Let's say I have to create the objects in the following scenario:
void withoutDependecyInjectionUsingNewOperator()
{
User user = new User("Lance", "Armstrong");
// These values are determined
// based on form input on UI
BankAccount account = new BankAccount(user, 1233);
}
I've come to know of frameworks like Guice or Spring which support DI. If I were using any of these frameworks, should I be changing the above function to use DI?
Based on the examples I have seen so far in tutorials, it seems that it is mostly useful when the constructor arguments can be decided as configuration and not for the cases where the values are ultimately based on User input?
Thanks!