How can i Release Exports?

Jul 8, 2010 at 9:27 AM

Hi marlon. i have a viewmodel first approach and i use your [ExportViewModel("M1", true)] for designtime issues. so my exports looks like this:

 [PartCreationPolicy(CreationPolicy.Shared)]
 [ExportViewModel("Kadia.ViewModel.MusterSelectorVM", true)]
 [Export(typeof(MusterSelectorVM))]
 public class MusterSelectorVM : ViewModelBase, ITitle, IViewLoadedAction
 {
	private Lazy<IMuster> last;

        	[ImportMany(typeof(IMuster))]
        	private IEnumerable<Lazy<IMuster, IDictionary<string, object>>> musterfac;

all works fine, but :) the musterfac has many NonShared exports. in my app i open and process one and then i'd like to dispose/release it.

if i try :

if(this.last != null && this.last.IsValueCreated)
   ViewModelRepository.Instance.Resolver.Container.ReleaseExport(this.last);

its just works when your [ExportViewModel("M1", true)] is commented out, but then no more design data :) do you have any suggestions for me?

 

Coordinator
Jul 8, 2010 at 9:36 AM
What do you mean by release? you do not have the UI Element to have that ViewModel as dataCOntext?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



On Thu, Jul 8, 2010 at 9:27 AM, blindmeis <notifications@codeplex.com> wrote:

From: blindmeis

Hi marlon. i have a viewmodel first approach and i use your [ExportViewModel("M1", true)] for designtime issues. so my exports looks like this:

 [PartCreationPolicy(CreationPolicy.Shared)]
 [ExportViewModel("Kadia.ViewModel.MusterSelectorVM", true)]
 [Export(typeof(MusterSelectorVM))]
 public class MusterSelectorVM : ViewModelBase, ITitle, IViewLoadedAction
 {
	private Lazy<IMuster> last;

        	[ImportMany(typeof(IMuster))]
        	private IEnumerable<Lazy<IMuster, IDictionary<string, object>>> musterfac;

all works fine, but :) the musterfac has many NonShared exports. in my app i open and process one and then i'd like to dispose/release it.

if i try :

if(this.last != null && this.last.IsValueCreated)
   ViewModelRepository.Instance.Resolver.Container.ReleaseExport(this.last);

its just works when your [ExportViewModel("M1", true)] is commented out, but then no more design data :) do you have any suggestions for me?

 

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


Jul 8, 2010 at 10:17 AM

second try of explanation^^
the musterselectorvm ViewModel comes to the ui via viewmodelfirst and datatemplate. the view has a listbox filled wih metadata from musterfac and a contentpresenter. when selecting an item in the listbox and i get the export and set the workItem

var batch = musterfac.First(x => x.Metadata["Muster"].ToString() == "030");
this.last = batch;
this.Workitem = batch.Value;

the workitem is the content for the contentpresenter and comes to the ui via datatemplate. as far as you select a new item in the listbox, the last "workitem" should be released/disposed and the new one should be displayed. like is said all works fine, as long as i comment you ExportViewModel out.

>>What do you mean by release?
the batch.Value should be released/disposed when processing a new one.

Jul 8, 2010 at 2:40 PM

hi marlon, after all i created a testapp without mefedmvvm, just to understand mef a little bit better, but now some new questions arrived^^

i did a post at http://stackoverflow.com/questions/3203614/mef-lazy-importmany-with-creationpolicy-nonshared. the strange thing is it behave exactly the opposite to my demoapp with mefedmvvm...

with mefedmvvm: i got everytime a new instance and i can release the export
with pure mef: i got everytime the same instance and i cant release the export

for today its to hot out there to think about it^^ maybe you got some time? :)

thx frank

Jul 14, 2010 at 8:40 AM
Edited Jul 14, 2010 at 11:49 AM

the last days i read into this mef topic.

so if i use Lazy<> Import i ALWAYS should get the same instance. (really dont know why a get a new one with mefedmvvm.)

the opposite to lazy is ExportFactory<> there i always get a new instance no matter what CreationPolicy is set. and thats the anwser to my problem. i use ExportFactory now and i can dispose my object via ExportLifeTimeContext.

it would be nice if you can tell why lazy is creating new instances with mefedmvvm?

Coordinator
Jul 14, 2010 at 8:45 AM
can you send me a project where I can reproduce this?
Jul 14, 2010 at 12:33 PM

samle project is on the way