Separating your Code from External Dependencies using Interface in .NET

Separating your Code from External Dependencies using Interface in .NET:

To fix this problem, we need to implement the same functionality but with a loosely coupled implementation. To​​ do this, we need to separate the code that deals with the database information retrieval from the code that manipulates the information. The first step is to create an interface that will expose which methods will interact with the database. This interface​​ will be used to create a class that implements such methods.​​ 

Below example shows the interface called​​ IUserRepository, which (for now) exposes three methods,​​ 

Add a class file, name it​​ IRepository.cs,​​ and then add the code.

interface​​ IUserRepository

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ void​​ CreateUser();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ void​​ GetUserByID(int​​ x);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ void​​ GetUserByName(string​​ name);  ​​ ​​​​ 

 ​​ ​​ ​​​​ }

Listing 1

Listing 2 a class that implements the​​ IUserRepository​​ interface. This class is called​​ UserRepository. The name represents the​​ implementation of the​​ IUserRepository.Create another class which is inheriting a interface​​ IUserRepository​​ and implementing all members of interface. Here we have created a user list and added in lstUser. (You can use table to fetch the data from the database and bind in to the list).

Here we have used LINQ query to fetch the data from the list. There is a two method which is using a LINQ.

In GetUserByID() method is making query on list then filtering into object using single keyword. In GetUserByName()​​ method is applying a LINQ query , filtering a List based on user name then reading all filtered data based on query using foreach.

using​​ System;

using​​ System.Collections.Generic;

using​​ System.Linq;

using​​ System.Text;

using​​ System.Threading.Tasks;

 

 

namespace​​ ExternalDependencies

{

 ​​ ​​ ​​​​ class​​ UserRepository​​ :​​ IUserRepository

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ List<User> lstUser =​​ new​​ List<User>();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ CreateUser()

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ //Adding User in the list​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ User​​ objUser1 =​​ new​​ User();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser1.UserID = 1;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser1.UserName =​​ "Adam";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser1.UserAddress =​​ "New York";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ lstUser.Add(objUser1);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ User​​ objUser2 =​​ new​​ User();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser2.UserID = 2;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser2.UserName =​​ "Byron";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser2.UserAddress =​​ "New York";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ lstUser.Add(objUser2);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ User​​ objUser3 =​​ new​​ User();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser3.UserID = 3;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser3.UserName =​​ "Harry";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser3.UserAddress =​​ "Houston";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ lstUser.Add(objUser3);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ User​​ objUser4 =​​ new​​ User();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser4.UserID = 4;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser4.UserName =​​ "Alen";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser4.UserAddress =​​ "Houston";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ lstUser.Add(objUser4);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ User​​ objUser5 =​​ new​​ User();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser5.UserID = 5;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser5.UserName =​​ "Harry";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objUser5.UserAddress =​​ "San Antonio";

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ lstUser.Add(objUser5);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ GetUserByID(int​​ UserID)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ //Using LINQ query to fetch the data based on user id

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Console.WriteLine("Result Based on User ID is :");

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ User​​ objU = lstUser.Where(x => x.UserID == UserID).Single();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Console.WriteLine("User Name : {0} and Address = {1}", objU.UserName, objU.UserAddress);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ GetUserByName(string​​ UserName)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ //Using LINQ query to fetch the data based on user id

List<User> lstU = lstUser.Where(x => x.UserName.ToUpper() ==​​ UserName.ToUpper()).ToList();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Console.WriteLine("List of Users are : "+Environment.NewLine);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ foreach​​ (User​​ objUser​​ in​​ lstU)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Console.WriteLine("User Id : {0} , User Name : {1} , User Address : {2} ",​​ objUser.UserID, objUser.UserName, objUser.UserAddress);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​​​ class​​ UserManagement

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ private​​ IUserRepository​​ _IUserRepository;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ UserManagement()

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository​​ =​​ new​​ UserRepository();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ UpdateUserList()

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository.CreateUser();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ SearchUserByID(int​​ UserID)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository.GetUserByID(UserID);

 ​​​​  ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ SearchUserByName(string​​ UserName)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository.GetUserByName(UserName);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

 

Listing 2

 

The great benefit of using the​​ IUserRepository​​ interface is that now we can create any number of​​ different classes that implement the same methods (e.g., an Oracle-based implementation or an XML documents implementation). When the application utilizes these classes, it knows indistinctively that when a method is called, it will get an object regardless of where the information came from. Now, how do we use this interface and class in the application? We need to change the​​ UserManagement​​ class to accept as a parameter a class that implements the​​ IUserRepository​​ interface. The easiest way to do this is to create a private variable of type​​ IUserRepository​​ and then, in the class constructor, set the variable with the class we want to use, as shown in Listing 2. You now see one constructors: one without a parameter, with defaults to the​​ UserRepository​​ class,​​ alternatively we can use a technique called​​ Dependency Injection (DI). With DI you define a container object with the knowledge of which class should be used in the code when you reference a specific interface. Then you just ask the container for the class by supplying the interface.

class​​ UserManagement

​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ private​​ IUserRepository​​ _IUserRepository;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ UserManagement()

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository =​​ new​​ UserRepository();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ UpdateUserList()

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository.CreateUser();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ SearchUserByID(int​​ UserID)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository.GetUserByID(UserID);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ void​​ SearchUserByName(string​​ UserName)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ _IUserRepository.GetUserByName(UserName);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

​​ }​​ 

Listing 3

We can then modify the​​ SearchUserByID​​ () ,​​ SearchUserByName() ​​​​ method to use the private _repository variable, as shown in Listing 3. Note in the code that the method​​ is actually very simple. We could do some more processing with the object we get from the repository, but the idea of the method is to just obtain the object and return it.

Now Finally we are going to call UserManagement class form public static void​​ main() .

Please find the below code to call the UserManagement class.

using​​ System;

using​​ System.Collections.Generic;

using​​ System.Linq;

using​​ System.Text;

using​​ System.Threading.Tasks;

 

namespace​​ ExternalDependencies

{

 ​​ ​​ ​​​​ class​​ Program

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ static​​ void​​ Main(string[] args)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ UserManagement​​ objManagement =​​ new​​ UserManagement();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objManagement.UpdateUserList();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objManagement.SearchUserByID(4);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ objManagement.SearchUserByName("Harry");

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Console.ReadLine();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

 

 

 

 

Output :

 

 

 


Recommended Book for MVC : List of Some Important books for .NET Framework, C#, MVC, WCF


(Visited 174 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *