NonShareViewModel, SharedViewModel,ViewModel ??

Nov 14, 2010 at 8:41 PM

Hello Marlon,

I have read your blog about MEFedMVVM. Found much code but when I search for the above 3 terms literally I can NOT find anything about it.

Above all your current code seem to be much different to what you have blogged earlier.

 

mlns:meffed="http:\\www.codeplex.com\MEFedMVVM"

meffed:ViewModelLocator.NonSharedViewModel="MyVM" is just an example.

What do all 3 options mean exactly?

Coordinator
Nov 15, 2010 at 9:46 AM

have a look at this post 

http://marlongrech.wordpress.com/2010/08/05/adding-more-goodies-in-mefedmvvm/

Nov 15, 2010 at 7:50 PM

oh this one I must have missed...

Now I have even more questions because your link is not very detailed. I need a convincing/explaining sample. All sounds so theoretical.

 

"...the ViewModel would be exported as a shared item so all imports get the same instance of the ViewModel. Also this implies that the ViewModel would never be garbage collected since MEF will keep its instance alive... "

1.) Who is garbage collecting it then?

2.) I tried  meffed:ViewModelLocator.SharedViewModel="DocumentListVM"  in my DocumentListView.xaml UserControl. judging what you say

my DocumentListViewModel should be instantiated only ONE time, is that right? If yes thats not the case in my project. I use the DocumentListView UserControl at several places for example in a DataGridRow.

Having 10 DataGridRows this DocumentListView.xaml gets instantiated TEN TIMES and the DocumentListViewModel TOO.

Is that ok or should that be different?

3.) What means the 3rd option : "ViewModel" ? Could not find a description.

Coordinator
Nov 16, 2010 at 1:55 PM

1.) Who is garbage collecting it then?

it would not be garbage collected until the COmositionCOntainer for MEF would be alive since it would keep a reference to that object 

having a sharedviewmodel would only create the view model once that is correct...can you send me a project repo of the issue...

3.) What means the 3rd option : "ViewModel" ? Could not find a description.

this would be up to whoever is exporting.... basically it is determined by using starndard MEF i.e [PartCreationPolicy(CreationPolicy.Shared / CreationPolicy.NonShared

Nov 16, 2010 at 8:51 PM
Edited Nov 18, 2010 at 6:41 AM

That is very odd. I can set NonSharedViewModel or SharedViewModel in my UserControls and in the relating ViewModel I do the ExportViewModel, but when I switch between my contentcontrol templates

my LessonPlannerViewModel is always called and the other viewmodels too. Although I use SharedViewModel...

But what would mostly interest me is the scenario with my UserControl containing 6 other UserControls. all have ONE ViewModel as DataContext. Can you comment to that?

Do you have a private email where I can send you my project and hope it is in good hands?

 

EDIT: This is even more odd. I have now replaced ALL "NonSharedViewModel" in my project with "SharedViewModel" and only the View`s are recreated now.

But the visual state of a datagrid with an selecteditem is lost when I switch between my templated contentcontrol and the viewmodels. I have made the selecteditem

with a Mode=Twoway so the state should actually be saved in the ViewModel, thats odd.

oh this is really a mess. Can`t wait for your answer...

 

EDIT 2: So SharedViewModel means my visual state is saved (having Mode=TwoWay on my bindings of course) because the ViewModel is created only once ?

Why would it make sense to use a ViewModel locator where I use NonSharedViewModel ? I always thought the static resources are the best to prevent the recreating of the VM.

I have studied your wpf demo sample and I see a big difference in the way you use MVVM to what (Jeremy Likeness - sorry that name was wrong) Colin Blair ( last post - http://betaforums.silverlight.net/forums/p/157070/351292.aspx ) said. Colin Blair would also use 2 Views:

SelectedUser and UsersScreen BUT with ONE ViewModel holding the CurrentEntity to switch the master/detail.

YOU use 2 ViewModels... woah people use MVVM so differently everytime I read another blog thats so frustrating...

It seems every alpha geek has its own interpretation of MVVM.

 

EDIT 3: I checked your mediator and I like it more than the messenger from mvvm light toolkit. Because I do not have to wrap  value types which show no meaning sending them in an extra custom class.

With your mediator I can use a string just like in CAB. Even the annotation: [MediatorMessageSink(MediatorMessages.SelectedUser, ParameterType=typeof(User))]

is very similar to CAB. Instead of CommandNames.XXX you wrote MediatorMessages.XXX both string constants hehe.

Nov 17, 2010 at 8:14 PM

Hello Marlon,

I know this is not really MEFedMVVM related but as you are a wpf disciple and mvvm lover

maybe you are kind enough answering me that question concerning your wpf sample demo of MEFedMVVM:

 

Assume I want to add a NewUserDetailView.xaml where I can enter data like firstname/last name of a new USER.

Now you have a NewUserViewModel to the above UserControl.

Would you send a message via mediator from the NewUserViewModel to YOUR TestViewModel aka UserListViewModel ( I renamed it to give it a proper meaning...)

with the new user object  ?

 

Coordinator
Feb 4, 2011 at 9:33 AM

yes you can use the Mediator for doing that... another approach is to have the ListViewModel have a reference to the NewUserDetailViewModel but the Mediator approacher is much easier

Mar 27, 2011 at 5:29 PM

This is weird, cause I'm facing the completely opposite problem:
Although I'm using ViewModelLocator.NonSharedViewModel, every SettingsWindow I create uses the same one,

the viewmodel's constructor is just called when I open the window for the first time.

A workaround for this is to use the CreationPolicy-Attribute on the viewmodel, so this seems to be a bug of mef itself.

Mar 29, 2011 at 3:16 PM

Ok, I investigated further and as far as I can say, the problem lies in the MEFedMvvmExportProvider.cs in line 38.

You create a new Export-object foreach export and it seems that it doesn't matter wether the original export is a CatalogExport or a NonSharedCatalogExport, the resulting

export is always a CatalogExport (with the IsSharedPart-Property set to true).

If I bypass this and just return the exports without recreating them, it works fine.

May 11, 2011 at 8:00 PM
Edited May 11, 2011 at 8:06 PM
tobi1449 wrote:

Ok, I investigated further and as far as I can say, the problem lies in the MEFedMvvmExportProvider.cs in line 38.

You create a new Export-object foreach export and it seems that it doesn't matter wether the original export is a CatalogExport or a NonSharedCatalogExport, the resulting

export is always a CatalogExport (with the IsSharedPart-Property set to true).

If I bypass this and just return the exports without recreating them, it works fine.

I can confirm this bug.

Update: @tobi Do you have a sample on how to the Creation Policy attribute?

Update 2: Never mind, found it.

[PartCreationPolicy(CreationPolicy.NonShared)]
Coordinator
May 14, 2011 at 8:10 PM
I will be fixing this very soon in my next release (which will be when I get a night of coding time :) )
Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Wed, May 11, 2011 at 8:00 PM, duluca <notifications@codeplex.com> wrote:

From: duluca

tobi1449 wrote:

Ok, I investigated further and as far as I can say, the problem lies in the MEFedMvvmExportProvider.cs in line 38.

You create a new Export-object foreach export and it seems that it doesn't matter wether the original export is a CatalogExport or a NonSharedCatalogExport, the resulting

export is always a CatalogExport (with the IsSharedPart-Property set to true).

If I bypass this and just return the exports without recreating them, it works fine.

I can confirm this bug.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEFedMVVM@discussions.codeplex.com)

To start a new discussion for this project, email MEFedMVVM@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com