I try to implement my first application using PRISM and UNITY. So i try to split my application in several modules.
In my modules i have the related view as well as the view model.
Currently i instantiate my viewmodel and set the datacontext of my view in the views code behind:
public partial class View : UserControl
{
public View(IViewModel vm)
{
InitializeComponent();
this.DataContext = vm;
}
}
My model is instantiated using the unity-container in my view-models ctor.
public ViewModel(IEventAggregator eventAggregator, IUnityContainer container)
{
_eventAggregator = eventAggregator;
_model = container.Resolve<Model>();
this._model.PropertyChanged += new PropertyChangedEventHandler(OnModelPropertyChanged);
}
Before i used the unity container i injected the models by dependency injection via the ViewModels constructor.
But this seems not to work. I tried it in the following way:
public ViewModel(IEventAggregator eventAggregator, Model model)
{
_eventAggregator = eventAggregator;
_model = model
this._model.PropertyChanged += new PropertyChangedEventHandler(OnModelPropertyChanged);
}
This implementation gives me an exception and i also couldn't find out how to setup the container so that the model injection works in the way i tried.
What i would like to do is to instantiate my models in the module class. And from there i would like to inject it to my viewmodels.
So my questions are:
- Is the way i do it currently correct?
- Is there a way to instantiate the models in the module class and inject them to the viewmodels, if so how and are there any examples on the web?