Understanding about the dispose method of di container unitycontainer to release resources

Source: Internet
Author: User

When the iunitycontainer. Dispose () method is called, it releases all sub-containers (that is, containers created using createchildcontainer) and the singleton object registered as containercontrolledlifetimemanager. The following example illustrates the problem:

Iunitycontainer modulecontainer = new unitycontainer (); iunitycontainer viewcontainer = modulecontainer. createchildcontainer (); modulecontainer. registerinstance <iunitycontainer> ("salebus", viewcontainer); int I = viewcontainer. gethashcode (); modulecontainer. dispose (); iunitycontainer test = modulecontainer. resolve <iunitycontainer> ("salebus"); // an error is returned because the parent container is released and all sub-containers are released.

 

If these Singleton objects inherit from the idisposable interface, the dispose method of these objects is called to reclaim objects. If registertype is not declared as containercontrolledlifetimemanager (that is, an object instance is re-created for each resolve operation), The iunitycontainer is called even if the object implements the idisposable interface. the dispose () method does not automatically call the dispose method of the object. The following example illustrates the problem:

Iunitycontainer modulecontainer = new unitycontainer (); modulecontainer. registertype <isalebuscorpview, salebuscorpview> (New containercontrolledlifetimemanager (); // salebuscorpview is a class isalebuscorpview view = modulecontainer. resolve <isalebuscorpview> (); isalebuscorpview view3 = modulecontainer. resolve <isalebuscorpview> (); int v = view. gethashcode (); int V3 = view3.gethashcode (); // The values of V and V3 are equal, because salebus Corpview is registered as containercontrolledlifetimemanager modulecontainer. dispose (); // The created singleton object will be released here. If salebuscorpview implements idisposable, it will automatically call the dispose method of salebuscorpview to execute some other operations. Isalebuscorpview view2 = modulecontainer. resolve <isalebuscorpview> (); isalebuscorpview view4 = modulecontainer. resolve <isalebuscorpview> (); int v2 = view2.gethashcode (); int V4 = view4.gethashcode (); // The values of V2 and V4 are equal because salebuscorpview is registered as containercontrolledlifetimemanager, however, the values of V2, V4, V, and V3 are indeed different. This indicates that the singleton object has been released and is subsequently a re-created singleton object.

 

But here I have a question: Call the dispose method of the container and the instance of the container still exists. We can test this by using the following code.

Iunitycontainer modulecontainer = new unitycontainer (); iunitycontainer viewcontainer = modulecontainer. createchildcontainer (); modulecontainer. registerinstance <iunitycontainer> ("salebus", viewcontainer); int I = viewcontainer. gethashcode (); modulecontainer. dispose (); iunitycontainer test = modulecontainer. resolve <iunitycontainer> ("salebus"); // an error is returned because the parent container is released and all sub-containers are released.

Int J = test. gethashcode (); if (I = J) {messbox. Show ("same instance ");}

This indicates that viewcontainer still exists. Some people may explain that viewcontainer is a singleton object registered in modulecontainer. The lifecycle is the same as that of modulecontainer. I also think this is the reason, however, when I dispose viewcontainer, I use viewcontainer. when createchildcontainer () is used to create a sub-container, an error is returned, indicating that viewcontainer is not instantiated. I cannot figure this out. Why? Why can't I create a sub-container if the container exists... For more information, see... It has been a long struggle.

Another problem is how to uninstall the module in Prism modular development. I think the source code of moduleloader is that after the module is initialized, a record will be inserted into the list of initializedmodules, indicating that the module has been initialized, this attribute is private and not public, but my current requirement is as follows: I create each module as a tabitem, And I customize the tabitem template, implemented the close button. However, when you close tabitem and click the same menu again, the tabitem cannot be generated. After debugging, I found that initializedmodules has marked that this module has been initialized, no more Initialization is allowed (My regions is replaced when the module is initialized). I want to change the moduleloader of the source code and add an unloadmodule.
Method to remove a record from initializedmodules:

public void UnLoadModule(ModuleInfo moduleInfo)        {                this.initializedModules.Remove(moduleInfo.ModuleType);        }

Then, when tabitem is disabled, the unloadmodule is called to remove the module initialization tag. In this way, you can regenerate tabitem. I don't know why the P & P team didn't provide a method to uninstall the module. When the tab module is generated in RI, the tab cannot be closed, so it will not be affected if you do not uninstall the module. When you click confirm & buy, it will automatically switch to the tab.

 

Or who uses prism for modular development and better master program framework design? I hope you will not be enlightened ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.