Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Extending Interfaces

5.00/5 (2 votes)
18 Dec 2015CPOL2 min read 6K  
Extending Interfaces

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.

JavaScript
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:

JavaScript
namespace ExtendingInterface
{
    public class MovieRepository
    {
        //Get all Movies Generic Way
        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.

JavaScript
using System;
using System.Collections;
using System.Collections.Generic;
using ExtendingInterface;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            MovieRepository movieRepository = new MovieRepository();
            
            //Implementing plain way
            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);
            }

            //Implementing Interface way
            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.

25th

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.

JavaScript
using System.Collections.Generic;

namespace ExtendingInterface
{
    public class MovieRepository
    {
        //Get all Movies Generic Way
        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:

26th

27th

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:

JavaScript
using System;
using System.Collections;
using System.Collections.Generic;
using ExtendingInterface;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            MovieRepository movieRepository = new MovieRepository();
            
            //Implementing plain way
            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);
            }

            //Implementing Interface way
            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!

Image 4 Image 5 Image 6 Image 7 Image 8 Image 9 Image 10 Image 11 Image 12 Image 13 Image 14

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)