Prerequisite Imports: ImportingConstructor?... RRS feed

  • Question

  • Hi,

    Just need a little clearing up about the ImportConstuctor on how its passing the parameter.  In reading "Attributed Programming Model Overview"  section "Prerequisite Imports" is says:

    To fill the parameters of a constructor marked with the ImportingConstructor attribute, all of those parameters are automatically declared as imports. This is a convenient way to declare imports that are used during part initialization.

    Example: public MainViewModel(IBookstoreRepository bookstoreRepository)


    [Export("MainViewModel")] public class MainViewModel : ViewModelBase { private Book _currentBook; private ObservableCollection<Book> _books = new ObservableCollection<Book>(); private IBookstoreRepository _bookstoreRepository = new BookstoreRepository(); private ICommand _removeBookCommand; [ImportingConstructor] public MainViewModel(IBookstoreRepository bookstoreRepository) { if (bookstoreRepository == null) throw new ArgumentNullException("bookstoreRepository"); _bookstoreRepository = bookstoreRepository; LoadBooks(); } public ObservableCollection<Book> Books { get { return _books; } } public Book CurrentBook { get { return _currentBook; } set { _currentBook = value; OnPropertyChanged("CurrentBook"); } } public ICommand RemoveBookCommand { get { if (_removeBookCommand == null) { _removeBookCommand = new DelegateCommand<Book>(RemoveBook, CanRemoveBook); } return _removeBookCommand; } } private void LoadBooks() { foreach (Book book in _bookstoreRepository.GetBooks()) { _books.Add(book); } } private bool CanRemoveBook(Book book) { return Books.Count > 0 && Books.Contains(book); } private void RemoveBook(Book book) { Books.Remove(book); CurrentBook = null; } }

    ...does this mean the "private IBookstoreRepository _bookstoreRepository = new BookstoreRepository();" will be used during the initialization by MEF to pass a value to the construction automatically?  If not, how does this parameter get initialized by MEF during the composition of the Import part?  I can't seem to get this clear in my thinking.


    public partial class App : Application { private CompositionContainer _container; private object _mainViewModel; [Import("MainViewModel")] internal object MainViewModel { get { return _mainViewModel; } set { _mainViewModel = value; } } private void OnStartup(object sender, StartupEventArgs e) { if (Compose()) { Views.MainView view = new Views.MainView(); view.DataContext = MainViewModel; view.Show(); } } private bool Compose() { // An aggregate catalog can contain one or more types of catalog var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); _container = new CompositionContainer(catalog); try { // Perform the composition _container.ComposeParts(this); } catch (CompositionException compositionException) { MessageBox.Show(compositionException.ToString()); return false; } return true; } }

    If you study this part being imported there is no passing of the parameter, but when I when the program in debug the parameter bookstoreRepository is initialized.


    Code is like a box of chocolates!...

    • Edited by VcDeveloper Sunday, September 23, 2012 2:59 PM
    • Moved by Mike Dos Zhang Tuesday, September 25, 2012 3:04 AM not a Windows Desktop dev question (From:General Windows Desktop Development Issues)
    Saturday, September 22, 2012 3:05 PM