Now let’s try to create a Class library projects named MSLogLogger and NLogLogger. For the demo purpose will just try to display in the Console Application on what you have chosen either the MS Logger or the NLog logger.
Now let’s add another Class library called SampleInterface.Business project which will contain the business logic of our sample application like the service, common and the interface.
Now that we have added all libraries that will be using, let’s start coding on our Business layer first. Will add the following folders: Interface, Common, Helper and Service as shown in Figure 4.
Now let’s create our Interface called ILogger by right clicking on the Interface folder then select the add new item then select the Interface and name it ILogger.
For the contents of ILogger will just make it’s access modifier Public then create one method signature called WriteLogs with a string parameter.
Then under the Common folder will create a new public class called Setting in which it will contain two public properties called AssemblyName and ClassName which we will inject later in the constructor of our service. By the way we need AssemblyName and ClassName because we’re going to use reflection later in our Factory helper in selecting what assembly we are going to use whether it maybe MS Logger or NLog logger or so on and so forth.
Now let’s go to our Helper Class which is actually the one doing the Magic in reading the assembly that we want and also in charge of creating the object. This approach is using a Factory pattern but instead of it’s traditional approach where in you select from a given class we are using instead reflections through the provided AssemblyName and ClassName in our Setting class. For the contents of LoggerHelper we just created a static GetLogger method of type ILogger which is responsible for returning the instance of the selected Logger that we are going to use.
For the last step in our Business project lets create our Service which will be the Class that our Console Application will be using. Our LoggerService contains one public method only Called WriteMessage with a string parameter that will used to write in the logs but in this demo will just mock it and instead display it in our screens.
Now that we have finished our Business project let’s go to our Mocked Logger projects MS and Nlog. Basically our goal for the Logger projects is to Implement the Interface ILogger which has a WriteMessage with a string parameter. We also need to add project reference for both Logger projects to our Business Project because it contains our Interface as shown in Figure 10.
This should be the reference part of both projects as shown in Figure 11.
Now let’s code both of our logger projects MSLogLogger and NLogLogger by mocking it with just displaying the parameter string which was passed by our console application and also indicate which logger was used in the process as shown both in Figure 12 and Figure 13.
Now for the last step which will focus on the SampleInterface project which is our Main Project. For the contents of our Program Class, will just be calling the LoggerService with the parameter of Setting type which in actual operation the Setting should be mapped with the application configuration file of the Program.
The only Caveat here is the AssemblyName and the ClassName because we are using the Factory pattern with reflection we need to add the DLL library in the folder of our main program as shown in Figure 15 and supply the Assembly Name and it’s corresponding Class to be instantiated. I think there are more ways to improve this kind of usage but will just leave it as is for the moment.
Now for the moment of truth, let’s try to build and run our application….. So as you can see in Figure 16 we have the expected output that we called the NLog library in displaying our message which was “Hello World”.
Let’s try to verify with the MSLogger Project by just changing our Setting properties to point to our MSLogLogger assembly and it’s respective class. Run the program…. and we also have the expected output with respect to the MSLogger library as shown in Figure 17.
I’ve added a link to my GitHub repository for this Project: https://github.com/KirkPatrickJunsay/SampleInterface
Just a quick recap of what we have learned about implementing interface in designing our software. First we learned that when we use Interface it’s like just plugging in our new library that we are going to use without modifying our business logic code as well as in our U.I. code or incase in this sample our Main Program. Another thing is that we have the luxury of plugging any of the libraries using reflections through there interface. Lastly we have also seen a more decoupled system between the Main Program and Logger Libraries.
Happy Coding 🙂