In this section, we will delve further with Interfaces and see why Interfaces are important for any Scalable, Extend-able and Testable. And, one of the key reasons which I believe basically for using Interfaces is to keep the code future proof and maintainable. Now, let us consider a below case. Here, I have a plain simple class with few properties in it. Now, this will serve as a model for me populating the values.
namespace ExtendingInterface
{
public class Movies
{
public string MovieName { get; set; }
public string DirectorName { get; set; }
public string ReleaseYear { get; set; }
}
}
In order to populate the same I am just using one repository as shown below:
namespace ExtendingInterface
{
public class MovieRepository
{
public Movies[] GetMovies()
{
return new[]
{
new Movies
{
DirectorName = "Gareth Edwards",
MovieName = "Godzilla",
ReleaseYear = "2014"
},
new Movies
{
DirectorName = "James Cameron",
MovieName = "Avatar",
ReleaseYear = "2009"
},
new Movies
{
DirectorName = "James Cameron",
MovieName = "Titanic",
ReleaseYear = "1997"
},
new Movies
{
DirectorName = "Lee Tamahori",
MovieName = "Die Another Day",
ReleaseYear = "2002"
},
new Movies
{
DirectorName = "Colin Trevorrow",
MovieName = "Jurassic World",
ReleaseYear = "2015"
}
};
}
}
}
Now, currently, the above method is returning all movies. Here, I am using Array as collection. Now, let us look at the main program.
using System;
using System.Collections;
using System.Collections.Generic;
using ExtendingInterface;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
MovieRepository movieRepository = new MovieRepository();
Movies[] movies = movieRepository.GetMovies();
Console.WriteLine("---------Writing Simple Way------------");
foreach (var items in movies)
{
Console.WriteLine("Movie Name:- "+items.MovieName+
", Director Name:- "+items.DirectorName+
", Release Year:- "+items.ReleaseYear);
}
IEnumerable<movies> moviesEnumerable = movieRepository.GetMovies();
Console.WriteLine("---------Writing Interface Way------------");
foreach (var elems in moviesEnumerable)
{
Console.WriteLine("Movie Name:- " + elems.MovieName +
", Director Name:- " + elems.DirectorName +
", Release Year:- " + elems.ReleaseYear);
}
Console.ReadLine();
}
}
}</movies>
Here, it is printing same values one using simple Array and the other one using IEnumerable
. In both the cases, it is working fine as IEnumerable
is basically implemented by almost all the collections.
Now, let us consider for some reason, the developer wants to change the way it is returning data. Then, would like to use more feature rich collections like List
.
using System.Collections.Generic;
namespace ExtendingInterface
{
public class MovieRepository
{
public List<movies> GetMovies()
{
return new List<movies>
{
new Movies
{
DirectorName = "Gareth Edwards",
MovieName = "Godzilla",
ReleaseYear = "2014"
},
new Movies
{
DirectorName = "James Cameron",
MovieName = "Avatar",
ReleaseYear = "2009"
},
new Movies
{
DirectorName = "James Cameron",
MovieName = "Titanic",
ReleaseYear = "1997"
},
new Movies
{
DirectorName = "Lee Tamahori",
MovieName = "Die Another Day",
ReleaseYear = "2002"
},
new Movies
{
DirectorName = "Colin Trevorrow",
MovieName = "Jurassic World",
ReleaseYear = "2015"
}
};
}
}
}</movies></movies>
At this instant, when I build the solution, then it will fail. It will print the following message:
Basically, it’s saying that you cannot convert List to Arrays. Now, in order to fix the same, either we need to cast the same or we can just change the variable from Array to List as shown below:
using System;
using System.Collections;
using System.Collections.Generic;
using ExtendingInterface;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
MovieRepository movieRepository = new MovieRepository();
List<movies> movies = movieRepository.GetMovies();
Console.WriteLine("---------Writing Simple Way------------");
foreach (var items in movies)
{
Console.WriteLine("Movie Name:- "+items.MovieName+
", Director Name:- "+items.DirectorName+
", Release Year:- "+items.ReleaseYear);
}
IEnumerable<movies> moviesEnumerable = movieRepository.GetMovies();
Console.WriteLine("---------Writing Interface Way------------");
foreach (var elems in moviesEnumerable)
{
Console.WriteLine("Movie Name:- " + elems.MovieName +
", Director Name:- " + elems.DirectorName +
", Release Year:- " + elems.ReleaseYear);
}
Console.ReadLine();
}
}
}</movies></movies>
With the above change in place, it will work fine, it will again produce the same result.
But, the conclusion is we didn’t have to change our Interface code because both Array and List implements IEnumerable.Therefore, whenever we are coding on the abstraction, we don’t care about the specific class coming back, only thing which we care about is the class which fulfills the contract. I hope you would have liked today’s discussion on Interfaces. In the coming section, we delve further inside Interfaces. Thanks for joining me.
Download Code: https://github.com/rahulsahay19/Extending-Interfaces
Thanks,
Rahul Sahay
Happy coding!
CodeProject
Hey there, it's Rahul Sahay! I'm thrilled to be a platform specialist at Publicis Sapient, where I get to work on some exciting projects. I've been honing my skills in various aspects of the software development life cycle for more than 15 years, with a primary focus on web stack development. I've been fortunate to have contributed to numerous software development initiatives, ranging from client applications to web services and websites. Additionally, I enjoy crafting application architecture from scratch, and I've spent most of my time writing platform agnostic and cloud agnostic code. As a self-proclaimed code junkie, software development is more than just a job to me; it's a passion! And I consider myself lucky to have worked with an array of cutting-edge technologies, from .NetCore to SpringBoot 3, from Angular to React, and from Azure to AWS and many more cousin technologies...
- 🔭 I’m currently working @ below tech stacks
- Microservices,
- Distributed Systems,
- Spring Boot
- Spring Cloud
- System Design,
- Docker,
- Kubernetes,
- Message Queues,
- ELK Stack
- DotNetCore,
- Angular,
- Azure
- 💬 Ask me anything about my articles [My View](https://myview.rahulnivi.net/)
- 📫 How to reach me: [@rahulsahay19](https://twitter.com/rahulsahay19)
- 📫 Github: [@rahulsahay19](https://github.com/rahulsahay19)