Implement multiple interfaces with same function name and parameters in a class

Few days ago, I got the consultancy work for one of my friend’s firm. In their project i have had faced one interesting issue where there were two interfaces which contain same function name with same parameters and i needed to implement it in one class.

Rather than go into much theory let’s create this situation and see how we can get rid of this situation.

But first we see that what happen if we implement those interfaces in one class. So let’s create the required interfaces and class. So here we have our first interface named – “IStudent

public interface IStudent
{
	///<summary>
	/// Get list of student who have birthday today
	/// </summary>
	/// <returns></returns>
	DataTable GetTodaysBirthdayList(string date = "");
}

And here we have second interface.

public interface IStaff
{
	///<summary>
	/// Get list of staff member who have birthday today
	/// </summary>
	/// <returns></returns>
	DataTable GetTodaysBirthdayList(string date = "");
}

Now let’s try to implement these two interfaces in class.

public class School : IStudent, IStaff
{
	#region IStudent Members

	public DataTable GetTodaysBirthdayList(string date = "")
	{
		var dtList = new DataTable();
		// Business logic to get list of student who have birthday today
		return dtList;
	}

	#endregion

	#region IStaff Members

	public DataTable GetTodaysBirthdayList(string date = "")
	{
		var dtList = new DataTable();
		// Business logic to get list of staff who have birthday today
		return dtList;
	}

	#endregion
}

Alright let’s rebuild the application and see what happen. Whoaa… we get the error!

InterfaceError

Error: Type “School” already defines a member “GetTodaysBirthdayList”

I think no need to explain the error message. Now let’s remove any of the method from the class and rebuild the application. Error is gone now, But the problem is how can you know that which interface method you wanna call!

So to overcome to this situation we need to explicitly implement those interfaces. How ? Let’s see this practically.

public class School : IStudent, IStaff
{

	#region IStudent Members

	DataTable IStudent.GetTodaysBirthdayList(string date)
	{
		var dtList = new DataTable();
		// Business logic to get list of studnet who have birthday today
		Console.WriteLine("=> From IStudent interface.");
		return dtList;
	}

	#endregion

	#region IStaff Members

	DataTable IStaff.GetTodaysBirthdayList(string date)
	{
		var dtList = new DataTable();
		// Business logic to get list of staff who have birthday today
		Console.WriteLine("=> From IStaff interface.");
		return dtList;
	}

	#endregion

	// You cannot directly access explicitly implemented interface method
	// So we need two public method to call above interface method

	public DataTable GetTodaysBirthdayListOfStudent(string date = "")
	{
		IStudent objStudent = (IStudent)this;
		return objStudent.GetTodaysBirthdayList();
	}

	public DataTable GetTodaysBirthdayListOfStaff(string date = "")
	{
		IStaff objStaff = (IStaff)this;
		return objStaff.GetTodaysBirthdayList();
	}
}

By creating an instance of the above class you cannot directly access explicitly implemented interface method. For that we need two public method to call implemented interface method as shown in above class.

And now its time to call the above class method, like this –

static void Main(string[] args)
{
    var school = new School();
            
    var staffList = school.GetTodaysBirthdayListOfStaff();
    var studnetList = school.GetTodaysBirthdayListOfStudent();
}

And we are done. Well after seeing above code you might think that instead of following the above stuff i can make one common interface with common method and inherit that interface in IStaff and IStudent interface. Well Ofcouse you can do that, But consider a situation where you are using any third party DLL or in your existing application you cannot alter or made any changes due to any architectural restriction.

Alright, Hope you enjoyed this post. Thanks for reading this post.

Advertisements

How to achieve UNION ALL and UNION in LINQ

In one of my application, I came to a situation where I had to use UNION ALL like functionality for two enumerable list by using LINQ. So in today’s post we’ll learn about how we can achieve UNION and UNION ALL like functionality using LINQ. You can do a UNION operation using the Union method in LINQ and UNION ALL operation using the Concat method.

Let’s see this practically –

First we will learn about Union method.

static void Main(string[] args)
{
	var productsFromMicrosoft = new List<string>
	{
		"Visual Studio",
		"MS Office",
		"Xbox",
		"Mail Service"  // <-- This product is in both list
	};

	var productsFromGoogle = new List<string>
	{
		"Google Chrome",
		"Mail Service",  // <-- This product is in both list
		"Blogger"
	};

	var unionList = productsFromMicrosoft.Union(productsFromGoogle).ToList();

	Console.WriteLine(unionList);
}

UnionList

So as expected, UNION method excludes duplicates from the return set.

Now we will see Concat operator.

static void Main(string[] args)
{
	var productsFromMicrosoft = new List<string>
	{
		"Visual Studio",
		"MS Office",
		"Xbox",
		"Mail Service"  // <-- This product is in both list
	};

	var productsFromGoogle = new new List<string>
	{
		"Google Chrome",
		"Mail Service",  // <-- This product is in both list
		"Blogger"
	};

	var unionAllList = productsFromMicrosoft.Concat(productsFromGoogle).ToList();

	foreach (var product in unionAllList)
	{
		Console.WriteLine(product);
	}
}

UnionAllList

The Concat method differs from the Union method because the Concat() method returns all the original elements in the input sequences. The Union method returns only unique elements.

Reference Link:

That’s it folks. Hope you like this post.

NOWAIT hint in SQL Server

Today, I was reading about Table Hint in MSDN for other reason and came across this “NOWAIT” hint. In today’s post we will learn about NOWAIT hint. Well, SQL server uses locks to ensure that the data in your table can be accessed, without risk of corruption or dirty reads. Let say when you are inserting or updating records within transaction then your table are locked and other transaction have to wait to read or change the records.

Sometime we are in situation that when our application request for data and if our request is locked by SQL server then rather than wait for some interval, its better to move out. So in that type of situations “NOWAIT” hint comes into the picture. It means when you apply this hint and requesting for select records from the table, the command fails immediately if current request is blocked and then reporting an error.

Let’s see this practically –

Open two query window in your SQL Server Management Studio. Assume that you have a table named Products and you tried to insert some records within one transaction as shown below in query window 1.

BEGIN TRAN

INSERT INTO Products
     VALUES (1, 'iPhone 5s', 55200)

--ROLLBACK
--COMMIT TRAN

To create the blocking situation, i have not committed transaction in above query

Query_Window_1

Now in second query window if you fire select statement command without any hint then you have to wait for infinite interval of time as our above transaction is not completed. But what happen if you fire select command with “NOWAIT” hint like below –

SELECT * FROM Products WITH (NOWAIT);

Above query results in an error as shown below.

Query_With_NoWaitHint

And you have to now manage the exception in your application and modify the code accordingly. Hope this post may help you in future.

That’s it for now folks. Hope you like this post.