mardi 9 août 2016

Database communication class as a singleton

Im considering, whether class which gonna communicate with database should be a singleton.

Instead of injecting context everywhere it's needed, I've created one inside Database class, which is the singleton class.

Is it good practice?

Code:

    class DatabaseService
{
    public FreelanceOrganizerDataContext DatabaseContext { get; private set; } = new FreelanceOrganizerDataContext();
    static DatabaseService _singleton = new DatabaseService();
    private DatabaseService() { }

    public static DatabaseService GetDatabaseService()
    {
        return _singleton;
    }

    //Save Section
    public void AddProject()
    {

    }

    public void AddCustomer()
    {

    }

    //etc

}

Aucun commentaire:

Enregistrer un commentaire