I want to know whether we can write a code explicitly so as to know whether the instance of the class is already available or not and if the instance is available we could dispose it and then the new instance of the class can be created I am doing a project in WPF and Prism with very limited knowledge in it. Any help would be appreciated.
public class BillingSectionModule : IModule
{
private IRegionManager _regionManager;
private IUnityContainer _unityContainer;
public BillingSectionModule(IRegionManager regionManager, IUnityContainer unityContainer)
{
this._regionManager = regionManager;
this._unityContainer = unityContainer;
}
#region IModule Members
public void Initialize()
{
_unityContainer.RegisterType<IBillingSectionViewModel, BillingSectionViewModel>();
_unityContainer.RegisterType<IBillingSectionView, BillingSectionView>();
_unityContainer.RegisterType<IBillingSectionAdvanceBillingViewModel, BillingSectionAdvanceBillingViewModel>();
_unityContainer.RegisterType<IBillingSectionAdvanceBillingView, BillingSectionAdvanceBillingView>();
_regionManager.RegisterViewWithRegion(RegionNames.BillingOptionRegion, typeof(IBillingSectionView));
}
#endregion
}
This is the module class i Ve written... As you can see there is two views associated with a single module. So when ever
public void ChangeToAdvanceRegion(IRegionManager regionManager, IUnityContainer unityContainer)
{
//change Billing Area
var BillingAreaview = ServiceLocator.Current.GetInstance<IBillingSectionAdvanceView>();
if (!regionManager.Regions[RegionNames.BillingOptionRegion].Views.Contains(BillingAreaview))
regionManager.Regions[RegionNames.BillingOptionRegion].Add(BillingAreaview);
regionManager.Regions[RegionNames.BillingOptionRegion].Activate(BillingAreaview);
//reset the values
_eventAggregator.GetEvent<NewItemOrderEvent>().Publish("CHANGEADVANCEITEMFROMREGION");
}
public void ChangeToNewOrderRegion(IRegionManager regionManager, IUnityContainer unityContainer)
{
//change Billing Area
var BillingAreaview = ServiceLocator.Current.GetInstance<IBillingSectionView>();
if (!regionManager.Regions[RegionNames.BillingOptionRegion].Views.Contains(BillingAreaview))
regionManager.Regions[RegionNames.BillingOptionRegion].Add(BillingAreaview);
regionManager.Regions[RegionNames.BillingOptionRegion].Activate(BillingAreaview);
//reset the values
_eventAggregator.GetEvent<NewItemOrderEvent>().Publish("CHANGENEWITEMFROMREGION");
}
So when ever i call above functin first and then the second one.. the previously created instance of the view model is not been disposed.. but a new one is created... so when i call the nth time.. the function inside the view model is executed n tyms. creating load issues in the application....