title: Why does Xamarin documentation suggest using singleton for database connection?
body: <p>I am looking at the official documentation of Xamarin and they seem to encourage using statics/singleton for a Database Connection, which seems weird to me:</p>
  
  <p><strong>HERE</strong> <a href="https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/" rel="noreferrer">https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/</a></p>
  
  <blockquote>
    <p>This approach creates a single database connection that is kept open
    while the application runs, therefore avoiding the expense of opening
    and closing the database file each time a database operation is
    performed.
        static TodoItemDatabase database;</p>
  </blockquote>
  
  <pre><code>public static TodoItemDatabase Database
  {
    get
    {
      if (database == null)
      {
        database = new TodoItemDatabase(DependencyService.Get&lt;IFileHelper&gt;().GetLocalFilePath("TodoSQLite.db3"));
      }
      return database;
    }
  }
  </code></pre>
  
  <p><strong>HERE</strong> <a href="https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/part_2_-_architecture/" rel="noreferrer">https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/part_2_-_architecture/</a></p>
  
  <blockquote>
    <p>Singleton – The Singleton pattern provides for a way in which only a
    single instance of a particular object can ever exist. For example,
    when using SQLite in mobile applications, you only ever want one
    instance of the database. Using the Singleton pattern is a simple way
    to ensure this.</p>
  </blockquote>
  
  <p><strong>AND HERE</strong> <a href="https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/case_study-tasky/" rel="noreferrer">https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/case_study-tasky/</a></p>
  
  <blockquote>
    <p>The TaskItemDatabase is a singleton, ensuring that all access occurs against the same instance. A lock is used to prevent concurrent
    access from multiple threads.</p>
  </blockquote>
  
  <pre><code>public T GetItem&lt;T&gt; (int id) where T : BL.Contracts.IBusinessEntity, new ()
  {
      lock (locker) {
          return Table&lt;T&gt;().FirstOrDefault(x =&gt; x.ID == id);
      }
  }
  </code></pre>
  
  <p>It seems to me though that this is a widely discouraged idea in general, for instance here on SO:
  <a href="https://stackoverflow.com/questions/814206/getting-db-connection-through-singleton-class">getting db connection through singleton class</a>
  <a href="https://stackoverflow.com/questions/15599538/is-singleton-approach-right-for-accessing-maintaining-database-and-internet-conn">Is singleton approach right for accessing/maintaining database and internet connection</a></p>
  
  <p>So, any idea why does Xamarin team promote this approach? Is it different because of some particularity of their framework? <strong>And more importantly, if not that, then what is the proper approach?</strong></p>