samedi 4 mars 2017

preferred design pattern for databse sqlite in Xamarin

i am writing xamarin mobile app using xamarin forms but my questions is little generalize irrespective of platform.

I am trying to create single database instance for sqlite database so that entire application using it.

For that i was thinking to create singleton object as follows ( using the locks as multiple threads could access the the same instance )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileSalesApp.Data
{
    class DB
    {
        // volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed
        private volatile static DB dbInstance;
        private static object syncRoot = new Object();

        private DB() {  }

        public static DB Instance()
        {

            if (dbInstance == null)
                lock(syncRoot){
                    dbInstance = new DB();

                }

            return dbInstance;

        }
    }
}

Now while searching for different styles of creating singleton object in C# i came across to one article where they were suggesting to use dependency injection pattern so i am little confused what would be the appropriate pattern in context of creating database central instance.

http://ift.tt/26SWRV5

Aucun commentaire:

Enregistrer un commentaire