Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some SQLite Databases and i have 3 classes for the main handling.

1. A base SQLiteDatabase Class for the Connection
public class SQLiteDatabase
{
    public SQLiteDatabase(string databasePath);

    public void OpenConnection();
    public void CloseConnection();
    protected DataReader ExecuteReader(string sql);
    protected void ExecuteNonQuery(string sql);
}


2. A Class for handling the Tables like create a table.
public class SQLiteDatabaseTableHandler: SQLiteDatabase
{
    public SQLiteDatabaseTableHandler(string databasePath)
        : base(databasePath)
    {
    }
    public void CreateTable(IDatabaseTable table) { }
    public void UpdateTableCompatibility(IDatabaseTable table) { }
}


3. My application specified Database class for getting some stuff from the own Database.
public class ConcreteSQLiteDatabase : SQLiteDatabase
{
    public ConcreteSQLiteDatabase(string databasePath)
        : base(databasePath)
    {
    }
    public SQLiteDataReader GetSpecifiedStuff(string id)
    {
        ExecuteReader("SELECT * FROM specified_table");
    }
    public SQLiteDataReader GetOtherSpecifiedStuff(string id)
    {
        ExecuteReader("SELECT * FROM other_specified_table");
    }
    // ...
}


In my ConcreteSQLiteDatabase class i need methods from the SQLiteDatabaseTableHandler. There are 2 ways to make it possible:

1. Inheritance

I can use a Inheritance in my ConcreteSQLiteDatabase from the SQLiteDatabaseTableHandler instead of SQLiteDatabase.

Like this
public class ConcreteSQLiteDatabase : SQLiteDatabaseTableHandler

instead of
public class ConcreteSQLiteDatabase : SQLiteDatabase


2. Association

My ConcreteSQLiteDatabase class could have a instance of a SQLiteDatabaseTableHandler, so i can use the methods from the instance.

Like this
public class ConcreteSQLiteDatabase : SQLiteDatabase
{
    public SQLiteDatabaseTableHandler TableHandler{get;set;}
    public ConcreteSQLiteDatabase(string databasePath)
        : base(databasePath)
    {
        TableHandler = new SQLiteDatabaseTableHandler( databasePath );
    }
}


I don't know which way should i use in this case, Association or Inheritance?

Sorry for my bad english...
Posted
Updated 19-Jul-10 5:25am
v3

1 solution

I would like to suggest you third way that is best and also optimized.

For creating ConcreteSQLiteDatabase just use association of SQLiteDatabaseTableHandler class without deriving it from anything just like following.

C#
public class ConcreteSQLiteDatabase
{
    public SQLiteDatabaseTableHandler TableHandler{get;set;}
    public ConcreteSQLiteDatabase(string databasePath)
    {
        TableHandler = new SQLiteDatabaseTableHandler(databasePath);
    }
}



Because if you use association then you do not need to derive anything for your ConcreteSQLiteDatabase class in this scenario.


HTH
 
Share this answer
 
Comments
Bigdeak 19-Jul-10 5:02am    
Thanks for you feedback. I will use a association, because i may have more classes like the TableHandler, for examble a class for methods like "ExistsColumn" i can associate in this class later, too.
With a Inheritance it will be difficult to expand the class.
[no name] 20-Jul-10 4:23am    
My Pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900