I have already searched some tutorials and even looked pluralsite Introduction to PRISM. However, most examples based on using unity containers and the some lack of information on how to implement this feature with Mef container. My simple helloworld module is based on web tutorial. My code is the same except I’m stuck only on HelloModule and using Mef, not Unity as tutorial shows:
The main my problem how to initialize my view with my view model. The only working way I have found via experimenting is to initialize view-model in View constructor:
HelloView.xaml.cs
namespace Hello.View
{
[Export]
public partial class HelloView : UserControl, IHelloView
{
public HelloView()
{
InitializeComponent();
Model = new HelloViewModel(this);
}
public IHelloViewModel Model
{
//get { return DataContext as IHelloViewModel; }
get { return (IHelloViewModel)DataContext; }
set { DataContext = value; }
}
}
}
And standard module initialization code:
[ModuleExport(typeof(HelloModule), InitializationMode=InitializationMode.WhenAvailable)]
public class HelloModule : IModule
{
IRegionManager _regionManager;
[ImportingConstructor]
public HelloModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
_regionManager.Regions[RegionNames.ContentRegion].Add(ServiceLocator.Current.GetInstance<HelloView>());
}
}
However, can someone tell the correct way how to this things, I this it must be done in Module initialization section.
MatthiasG shows the way to define modules in MEF. Note that the view itself does not implement IModule. However, the interesting part of using MEF with PRISM is how to import the modules into your UI at startup.
I can only explain the system in principle here, but it might point you in the right direction. There are always numerous approaches to everything, but this is what I understood to be best practice and what I have made very good experiences with:
Bootstrapping
As with Prism and Unity, it all starts with the Bootstrapper, which is derived from
MefBootstrapper
inMicrosoft.Practices.Prism.MefExtensions
. The bootstrapper sets up the MEF container and thus imports all types, including services, views, ViewModels and models.Exporting Views (modules)
This is the part MatthiasG is referring to. My practice is the following structure for the GUI modules:
The model exports itself as its concrete type (can be an interface too, see MatthiasG), using
[Export(typeof(MyModel)]
attribute. Mark with[PartCreationPolicy(CreationPolicy.Shared)]
to indicate, that only one instance is created (singleton behavior).The ViewModel exports itself as its concrete type just like the model and imports the Model via constructor injection:
[ImportingConstructor] public class MyViewModel(MyModel model) { _model = model; }
The View imports the ViewModel via constructor injection, the same way the ViewModel imports the Model
And now, this is important: The View exports itself with a specific attribute, which is derived from the 'standard'
[Export]
attribute. Here is an example:The [ViewExport] attribute
The
[ViewExport]
attribute does two things: Because it derives from[Export]
attribute, it tells the MEF container to import the View. As what? This is hidden in it's defintion: The constructor signature looks like this:By calling the constructor of
[Export]
with type ofUserControl
, every view gets registered asUserControl
in the MEF container.Secondly, it defines a property
RegionName
which will later be used to decide in which Region of your Shell UI the view should be plugged. The RegionName property is the only member of the interfaceIViewRegionRegistration
. The attribute class:Importing the Views
Now, the last crucial part of the system is a behavior, which you attach to the regions of your shell:
AutoPopulateExportedViews
behavior. This imports all of your module from the MEF container with this line:This imports all types registered as
UserControl
from the container, if they have a metadata attribute, which implementsIViewRegionRegistration
. Because your[ViewExport]
attribute does, this means that you import every type marked with[ViewExport(...)]
.The last step is to plug the Views into the regions, which the bahvior does in it's
OnAttach()
property:Notice
.Where(v => v.Metadata.RegionName == Region.Name)
. This uses the RegionName property of the attribute to get only those Views that are exported for the specific region, you are attaching the behavior to.The behavior gets attached to the regions of your shell in the bootstrapper:
We've come full circle, I hope, this gets you an idea of how the things fall into place with MEF and PRISM.
And, if you're still not bored: This is perfect:
Mike Taulty's screencast
The way you implemented
HelloView
means that theView
has to know the exact implementation ofIHelloViewModel
which is in some scenarios fine, but means that you wouldn't need thisinterface
.For the examples I provide I'm using
property injection
, butconstructor injection
would also be fine.If you want to use the
interface
you can implement it like this:Otherwise it would look like this:
One more thing: If you don't want to change your
Views
or provide several implementations of them, you don't need aninterface
for them.