0

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....

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Muhammed Nigil
  • 173
  • 1
  • 10
  • 3
    Are you referring to the Singleton Pattern? http://csharpindepth.com/articles/general/singleton.aspx – David Sep 29 '14 at 13:01
  • @David Singleton pattern just keeps the same instance right? – Muhammed Nigil Sep 29 '14 at 13:09
  • I want to have a new instance every time but want to dispose the already created instance – Muhammed Nigil Sep 29 '14 at 13:10
  • Yes. Which means there's only one place in the application where the instance can exist. So there's only one place to *destroy* any existing instance to replace with a new one. – David Sep 29 '14 at 13:10
  • The only difference between singleton and what you want is that you'll delete the previous instance every time instead of returning it. – Borgleader Sep 29 '14 at 13:11
  • @David I didnt get you clearly.. – Muhammed Nigil Sep 29 '14 at 13:11
  • I dnt knw whether this might sound crazier coz am new to this field.... can i run the code in the class constructor to identify if an instance exists and then if it would dispose it ... and after that a new instance is created – Muhammed Nigil Sep 29 '14 at 13:13
  • @Borgleader how to do it.. can u help me – Muhammed Nigil Sep 29 '14 at 13:14
  • 1
    what does wpf and/or prism have to do with instances? Can you add more details regarding what you consider an instance to be? [comparing with this](http://stackoverflow.com/q/2219566/238902) for instance states that every time you call a constructor a new *instance* is created. So there is never an existing instance when you call the constructor. i.e. your definition of an "instance" need more clarification. – default Sep 29 '14 at 13:30
  • @Default the main thing is i am having very limited knowledge in WPF and PRsim ..... When am using both for changing views,.. the view models associated with them are loading the same constructors again and again.... when the same view is taken. I am having problem in understanding the flow.. – Muhammed Nigil Sep 29 '14 at 13:33
  • 1
    Please show some sample code where you are having a problem. The ctor is called when you new. If you dispose and then create a new instance the ctor will be called. Describe the problem - not your perceived solution as I don't think you are on the right track. – paparazzo Sep 29 '14 at 13:40
  • Oh, so this is a typical [xy problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)! You should really explain (with code) what your issue is, not how you are trying to solve it. I can almost guarantee that there is a better solution for your problem. But based on the current context you have shown it is impossible to know where to start helping. – default Sep 29 '14 at 14:24

1 Answers1

2

What you're looking for is called the Singleton Pattern. There is a lot of information on it here. For simplicity, we'll use the first example on that page.

With a singleton there is only one instance of the given object in the application. So, per your requirements, you can simply destroy that instance and replace it with a new one. For example:

public sealed class Singleton
{
    private static Singleton instance=null;

    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            instance = new Singleton();
            return instance;
        }
    }
}

Of course, what's not clear is why you want to do this. Consider what this refactors to:

public sealed class Singleton
{
    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return new Singleton();
        }
    }
}

At this point you're not even tracking a singleton, you're just creating a new one any time it's requested. So why do this in the first place? You can simply this even further to:

new Singleton();

So it's not really clear why you want to do this, but if what you want is to track a single instance of an object within the scope of an application then a Singleton is how you do that.

David
  • 208,112
  • 36
  • 198
  • 279