0

I have an application that I am working on. The call to the data manager to set up looks like this:

public partial class App : Application
{

    public App()
    {
        InitializeComponent();
        MainPage = new Japanese.MainPage();
    }

    public static DataManager DB
    {
        get
        {
            if (AS.dm == null)
            {
                AS.dm = new DataManager();
            }
            return AS.dm;
        }
    }

    protected override void OnStart()
    {
        AS.GetSettings();
        AS.selectedPhraseCount = AS.dm.GetTotalPhrasesCountForSelectedCategories();
    }

In other words the datamanager is set up when it's first needed.

Can someone tell me if there is any advantage to doing this. It would seem to me to be simpler just to do a call to AS.dm = new DataManager() in the onStart event.

public partial class DataManager
{
    protected static object locker = new object();
    protected SQLiteConnection db1;
    protected SQLiteConnection db2;

    public DataManager()
    {
        db1 = DependencyService.Get<ISQLiteDB1>().GetConnection();
        db2 = DependencyService.Get<ISQLiteDB2>().GetConnection();
Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

1

You'd need to post a bit more code (for example where are dm and AS declared?) to be absolutely sure, but this method of having a static declaration with a private constructor is called the Singleton pattern and is designed to ensure that only one instance of the object (in your case the DataManager) can ever exist. See this existing question

However, your code looks slightly odd in the OnStart because it looks like you are referencing the datamanager using the dm backing variable rather than the DM property.

Tintow
  • 248
  • 1
  • 2
  • 8
  • Would it not be the case that if I put the set up of the DM after initializeComponent that it would only happen the one time? – Alan2 Jul 15 '17 at 09:25
  • Thanks for pointing out about the access using AS.dm. That needs to change. – Alan2 Jul 15 '17 at 09:27