When using MEF and Prism v4, what is the recommend way to do [Import]
s and what is the difference (if anyway) between these two calls?
Option 1:
public class TestClass
{
[Import]
private IRegionManager _RegionManager;
public TestClass()
{
// stuff here
}
}
Option 2:
public class TestClass
{
private IRegionManager _RegionManager;
[ImportingConstructor]
public TestClass(IRegionManger regionManager)
{
this._RegionManager = regionManager;
// stuff here
}
}
What are the differences between these? Which is recommended?
This is a matter of style but I'm a much bigger fan of ImportingConstructor
than Import
on fields. The ImportingConstructor
allows you to define an object which is both usable with and without MEF support. The contract is clear and well understood by programmers.
Using Import
on the other hand makes your class much more usable from MEF. Developers are used to providing dependencies of an object to the constructor, not looking at all properties of an object and determining which ones it needs to set after construction.