I have a PersonEditViewModel
which needs two other objects, the personId
and the PersonRepository
. PersonRepository
is obtained from a service locator:
private final int personId;
public PersonEditViewModel(int id) {
this.personId = id;
this.personRepository = ServiceLocator.get(PersonRepository.class);
}
But I would like to use dependency injection (DI, IoC) to obtain PersonRepository
, preferably via constructor:
public PersonEditViewModel(PersonRepository pr) {
this.personRepository = pr;
}
My problem is, how can I pass the personId
to the constructor in DI pattern? Do I have to make a setter for id - then DI changes the logic of the PersonEditViewModel
(the personId
field cannot be final)? Is it insufficiency of DI pattern that I cannot pass data objects to the constructor, or do I have to switch my mind and design my ViewModels other way, probably use other patterns to pass data objects to them?
Edit: Changed my question using personId
and PersonRepository
to be closer to common situations.