I have three classes:
- MyModel (pure POCO object)
- MyModelWrapper (inherits from BindableBase)
- MyViewModel (it also inherits from Bindable class)
The problem I have is that I need a paremeterless constructor in MyModelWrapper because I bind an object in MyViewModel which is an ObservableCollection<MyModelWrapper>
to a Datagrid Itemssource, and if it has not a parameterless constructor, you are not able to insert new rows in it (it does not appear a blank line at the end of the Datagrid).
So, if I want the user can insert new rows I need a parameterless constructor (and I do want).
And here I have my problem: I need to inject a service in MyModelWrapper.
I can inject the service in the constructor:
IMyModelWrapperService Service;
public MyModelWrapper(IMyModelWrapperService Service)
{
Service = service;
}
But this way the Datagrid does not let the user to insert new rows.
If I could get a reference to the container, I could do container.Resolve<IMyModelWrapper>()
, but I think that I would need to inject the container via constructor too, so it is not a valid solution.
So, I would need something like:
[Inject]
IMyModelWrapperService Service;
But I think property injection does not work in Prism (at least I am not able to use it).
Which is the right approach to inject a service in a class that needs to be parameterless in Prism?
Thank you
CodePudding user response:
If you absolutely have to, you can access the container through Prism.Ioc.ContainerLocator.Container
to get what you need.
Yes, this is evil and ugly, but if you need a dependency in a class whose construction you can't control, then this is your only option.
This is nearly a duplicate of this closed question.