When dealing with objects that require data known only at runtime, such as a username and password, where should object instantiation happen: by using new, in a factory, or in a DI container?
For example, I could just new
an object once I have the data:
UserCredentials creds =
new UserCredentials(dialog.getUsername(), dialog.getPassword());
Or, I could use a factory:
UserCredentials creds =
CredentialsFactory.create(dialog.getUsername(), dialog.getPassword());
Or, I could use a provider within a DI container (which in this case would essentially be a parameter-driven factory). [Sample code omitted.]
It seems both wrong to use the DI container for something so simple yet it also seems wrong not to use it to its fullest.