Search code examples
c#oopinheritanceextendwindows-console

Inheritance of classes that aren't related in the traditional sense of OOP


I am wondering if I am approaching my project the right way in terms of OOP. My project consists of a windows console application in c#. The point of the application is to take user input, convert it to an sql query and query the connecting database and then spit out the information in a reader-friendly format that the user requested. I have the following 3 classes right now:

Class: commandLineInterpreter extends sqlQueries

this class prints out the commands that the user can utilize. it also takes the user input.

Class: sqlQueries extends dbConnect

this class holds the sql queries that will query the database depending on what the user is inputing.

Class: dbConnect this class initlizes the database connection and prints out a message saying if it suceeded or not.

as you can see i have the commandlineInterpreter class that extends the sql query class that then extends the db connect class.

When I initlize the commandline class in the main() function, it automatically intitlizes the other extend classes as well. I did this because without connecting to the DB then the commandLine interpreter is useless as it cant provide any answers.

My question is, even though these classes aren't related in an inheritance sense of OOP, does it still make sense to do class inheritance this way? or is there a better way to organize my code?


Solution

  • Inheritance usually expresses a relation of the kind "A is a B". Example: An apple is a fruit. But here you have completely unrelated classes. Keep them distinct and create separate objects for the distinct tasks.

    The dbConnect class mixes two very distinct aspects: Connecting to the DB and displaying a message. The commandLineInterpreter class should be the only one interacting with the user and displaying messages. The dbConnect class seems superfluous. The connection functionality should go to the sqlQueries class. Possibly create a method bool Connect() there that you can call from the command line interpreter. The Boolean result can be used to display an appropriate message.

    class SqlQueries
    {
        private SqlConnection connection;
    
        public bool Connect()
        {
            Try {
                connection = ...
                connection.Open();
                return true;
            } catch {
                return false;
            }
        }
    
        ....
    }
    
    class CommandLineInterpreter 
    {
        public void Run()
        {
            var sqlQueries = new SqlQueries();
            if (sqlQueries.Connect()) {
                Console.WriteLine("connected");
    
                // run your interpreter here
                ...
            } else {
                Console.WriteLine("Connection error! Not connected.");
            }
        }
    }
    

    In C# types (like classes), properties, methods and events have PascalCase. Fields, method parameters and local variables have camelCase.