0

I have to use these two different class files across my application. How do I inherit those two class files in another class file?

This class file to writes an information log

Public class Log
{    
    Public void createLog()
    {

    }
}

This class file gets connection string

public class DataConnector
{ 
   public void Connection()
   {

   }
}

I want to Inherit from those two classes in this class:

Public class FileOperation
{
    public void FileWiter
    {

    }
}

Are there any different ways to access the class files across my project?

What I have tried:

Public class FileOperation
{
    Log oLog=new Log();
    DataConnector oDataconn=new DataConnector();
    public void FileWiter
    {
        oLog.createLog();
    }
}

Yes, I can use that method, but I'm looking for any other best ways to do this?

Tobbe
  • 1,825
  • 3
  • 21
  • 37
King_Fisher
  • 1,171
  • 8
  • 21
  • 51
  • This will answer your quesion, http://stackoverflow.com/questions/178333/multiple-inheritance-in-c-sharp – Barr J Sep 07 '15 at 06:54

2 Answers2

0

Basically, there is nothing wrong with your approach. There is even an OOP Design Principle called composition over inheritance, which calls for this way of doing it.

Think about DataConnector, does you FileWriter really specialize some kind of DataConnector?

What I would recommend though would be hiding your dependencies behind interfaces and injecting them via the constructor of FileOperation like so:

public class FileOperation
{
    ILog log;
    IDataConnector dataConnector;

    public FileOperation(ILog log, IDataConnector dataConnector)
    {
        this.log = log;
        this.dataConnector = dataConncetor;
    }

    public void FileWiter
    {  
        this.log.createLog();    
    }
}

This way you could easily swap one or both of those dependecies by simply passing another object which implements the right interface. For example, you could create a MongoDbDataConnector which implements IDataConnector and pass this one instead of a MsSqlDataConnector or a PostgreSqlDataConnector.

nozzleman
  • 9,529
  • 4
  • 37
  • 58
0

There is no reason why your FileOperation should inherit from either Log nor DataConnector. Your FileOperation uses the Log and the DataConnector, but it shouldn't change how you Log or how you connect to your Data.

You could however prepare for the future, and create an interface for your Log class and for your DataConnector, so that at any time in the future you could easily swap those classes for another implementation, like so:

public interface ILogger
{
    void Log(string message);
}

public interface IDataConnector<T>
{
    IList<T> ReadList(Predicate<T> matches);
    T ReadItem(Predicate<T> match);
}

and implement your classes for these interface.

In your FileOperation class, you can then set these over interfaces, and potentially later fill them using dependency injection, like so:

public class FileOperation 
{
    public ILogger Log { get; set; }
    public IDataConnector DataConnector { get; set; }

    public FileOperation()
        : this(new Logger(), new DataConnector<string>())
    {
    }

    public FileOperation(ILogger log, IDataConnector dataConnector) {
        Log = log;
        DataConnector = dataConnector;
    }
}
Icepickle
  • 12,689
  • 3
  • 34
  • 48