I am very new to using dependency injection, and I am trying it out with Ninject in my WPF application. I was wondering how should I be passing parameters across classes.
For e.g.
public class ViewPersonViewModel : ViewModelBase
{
private IDataAccessService _dataService;
private IPerson _person;
private string _remarks;
// binded to textbox
public string Remarks
{
get { return _remarks; }
set {
if (_remarks != value) {
remarks = value;
OnPropertyChanged("Remarks");
}
}
}
public ViewPersonViewModel(
IDataAccessService dataService, IPerson person)
{
_dataService= dataService;
_person = person;
}
// binded to Button
public void RetrieveStatus()
{
Remarks = _dataService.RetrieveRemarks(_person);
}
}
In here, dataService is a fixed class, where I presumably can do
class Module : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<IDataAccessService>().To<DefaultDataAccessService>();
}
}
But I will like to know how should I handle the person parameter, where it is set by the calling class.
I saw from Creating an instance using Ninject with additional parameters in the constructor that it is possible to pass additional parameters in the constructor.
However, I have a few concerns:
Is using
kernel.Get<MyClass>( With.Parameters.ConstructorArgument( "parameterName", parameterValue) );
an ideal way? Wouldn't it cause a lot of problems in debugging such as when you have typed yourparameterName
by mistake?Ruben also mentioned about using a more complicated way where Provider is involved. Is it applicable here? If it is, how can I use it?