C# – Designing Applications using Interface

Interface.jpg

Image credit shakespearsglobe

We always hear the word Interface when we are creating Systems whether it be in Java or C# language. We all know that Interfaces are just binding contracts between two different entity or Classes. Another reason to implement Interfaces is too have a more decoupled system or a more cleaner Separation of Concerns. But sometimes like me, has a hard time grasping the true value of Interfaces in creating a program.

In today’s article I would like to show you a simple example on leveraging the features of Interface in creating a more decoupled system. The scenario where going to tackle is will design an application which is flexible enough to use any logging library where it may be Microsoft’s own logging functionality or may be NLog logging.
First let’s create a Console Application and let’s name it SampleInterface as shown in Figure 1.
CreateProject

Figure 1. Creating of Console Application

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.

 

LoggerLibraries

Figure 2. Logger Libraries

 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.

 

BusinessProject.jpg

Figure 3. SampleInterface.Business

 

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.

 

BusinessProject1.jpg

Figure 4. Added folder structure

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.

 

ILogger.jpg

Figure 5. 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.

 

IloggerContents.JPG

Figure 6. ILogger contents.

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.

 

Settings.JPG

Figure 7. Setting Contents.

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.

 

LoggerHelper.jpg

Figure 8. LoggerHelper Contents.

 

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.

 

LoggerService.JPG

Figure 9. LoggerService Contents.

 

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.

 

ReferenceToBusinessProject.jpg

Figure 10. Add to both logger project the reference to our Business Project.

This should be the reference part of both projects as shown in Figure 11.

 

BothReference.jpg

Figure 11. Reference for both Logger projects

 

 

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.

 

MSLogger

Figure 12. Contents of MSLog.

NLogger

Figure 13. Contents of NLog

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.

 

Main1.JPG

Figure 14. Contents of Main

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.

 

FolderContents.jpg

Figure 15. NLogLogger.dll inside the SampleInterface project.

 

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”.

 

NLog Output.JPG

Figure 16. NLog output

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.

 

MSLogOutput.JPG

Figure 17. MSLog output

Source Code:

I’ve added a link to my GitHub repository for this Project: https://github.com/KirkPatrickJunsay/SampleInterface

Conclusion

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 🙂

 

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s