C# Interfaces Extensibility 2


This entry is part 5 of 8 in the series C# Interfaces

This post shows us how we built the previous post called C# Interfaces Extensibility.

Below is how we start building our dependency injection. In the constructor of DbMigrator we need to supply a class that implements the ILogger interface. DbMigrator does not know or care what the name of that class is. Any class will do as long as it implements the ILogger interface. In other words, DbMigrator does not care where these messages are sent. They could be to the Console, to the screen of a WPF file, to a page on a website, or to a file or to a database. This is the whole point. All we have to do is write those concrete classes when we want to add a new place to send messages to. DbMigrator does not have to be changed. It still works fine as is. Al we have done is extended functionality.

using System;
namespace InterfacesExtensibility2
{
    public interface ILogger
    {
        void LogError(string message);  // method
        void LogInfo(string message);  // method 
    }
    public class DbMigrator
    {
        // ANY class that implements ILogger can be passed in here.
        // DbMigrator doesn't care which class it actually is.
        private readonly ILogger _logger;
        public DbMigrator(ILogger logger)
        {
            _logger = logger;
        }
        public void Migrate()
        {
            _logger.LogInfo($"Migrating started at {DateTime.Now}");
            // details of migrating the database
            _logger.LogInfo($"Migrating ended at {DateTime.Now}");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

We need to write a new class that implements ILogger, and we will finish with adding code to the Main() method. We have chosen to write a class that simply sends messages to the console. We could write a FileLogger if we chose to. We will save that for the next post, Part 3.

    public class ConsoleLogger : ILogger
    {
        public void LogError(string message)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message);
        }
        public void LogInfo(string message)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(message);
        }
    }

Implements Not Inherits

When we see the following code below we read that as implements. It is not inheritance. We know it is not inheritance because the identifier after the colon start with an I signifying an interface, not a class.

public class ConsoleLogger : ILogger

In the Main() we need to call the method Migrate() in DbMigrator. We need to pass in an instance of ConsoleLogger. We need to pass in a concreate class (instance/object). Notice that the Intelisense shows ILogger _logger.

Here is our code.

    class Program
    {
        static void Main(string[] args)
        {
            var dbMigrator = new DbMigrator(new ConsoleLogger());
            dbMigrator.Migrate();
        }
    }

Series Navigation<< C# Interfaces ExtensibilityC# Interfaces Extensibility 3 >>