Question

I always have the same problem and i don't know what is best to do... I have many tables in a database and many classes for each table which are managing every table (orm). I also have a base class controller like "router" which is administrating all of the tables-clases.

So, my question is, what is better? Create an instance of the database connection in every class that represents a table or create the instance in the router class and pass it around?

Option 1: Create new instance inside every class.

public class Table1Controller : Table1Mapp
{
    private Database myDatabaseInstance { get; set; }

    public Table1Controller()
    {
        myDatabaseInstance = new Database("Connection String here");
    }
    ...
}

public class Table2Controller : Table2Mapp
{
    private Database myDatabaseInstance { get; set; }

    public Table2Controller()
    {
        myDatabaseInstance = new Database("Connection String here");
    }
    ...
}

public class DatabaseRouterController
{
    private Table1 _table1;
    public Table1 table1
    {
        get { return this._table1 ?? (this._table1 = new Table1()); } 
        private set { this._table1 = value }; 
    }

    private Table2 _table2;
    public Table2 table2
    {
        get { return this._table2 ?? (this._table2 = new Table2()); } 
        private set { this._table2 = value }; 
    }
}

Opcion 2: Create object only once and pass it around.

public class Table1Controller : Table1Mapp
{
    private Database myDatabaseInstance;

    public Table1Controller(Database myDatabaseInstance)
    {
        this.myDatabaseInstance = myDatabaseInstance;
    }
    ...
}

public class Table2Controller : Table2Mapp
{
    private Database myDatabaseInstance;

    public Table2Controller(Database myDatabaseInstance)
    {
        this.myDatabaseInstance = myDatabaseInstance;
    }
    ...
}

public class DataBaseInstanceClass
{
    protected Database myDatabaseInstance { get; set; }

    public DataBaseInstanceClass()
    {
        myDatabaseInstance = new Database("Connection String here");
    }
}

public class DatabaseRouterController : DataBaseInstanceClass
{
    private Table1 _table1;
    public Table1 table1
    {
        get { return this._table1 ?? (this._table1 = new Table1(base.myDatabaseInstance)); } 
        private set { this._table1 = value }; 
    }

    private Table2 _table2;
    public Table2 table2
    {
        get { return this._table2 ?? (this._table2 = new Table2(base.myDatabaseInstance)); } 
        private set { this._table2 = value }; 
    }
}

Any further suggestions would be appreciated... Thanks in advance!

Was it helpful?

Solution

What i would do is create a DAL (Data Access Layer) which is reachable from any of the mentioned controllers and simply wraps the work being done against the database layer, so you don't have to bother passing around database connections.

Also, it would save you meaningful time refactoring code when you need to access a new table or feature in the database.

I suggest you look into the Repository Pattern

OTHER TIPS

You should read about the Dependency Injection pattern. It would advocate for option 2 because it would make your various classes able to connect to any database that you passed in, rather than being locked to a specific database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top