Map the interface to Class (dynamically) that does not implement the C # interface

advertisements

This is my first post in Stackoverflow. Thanks to Stackoverflow.

My Question:

I have one interface in class library.

namespace TestClient
{
   public interface IMobileStore
   {
        List<string> GetMobileDetails();

        int AddMobile(string mobile);
   }
}

And i have class named MobileLibrary in another class library and implemented IMobileStore interface (While development)

namespace MobileLibrary
{
    public class MobileLibrary  : IMobileStore
    {
        List<string> GetMobileDetails()
        {
             return new List<string>();
        }

        int AddMobile(string mobile)
        {
             return 1;
        }
    }
 }

And consumed the MobileLibrary library in console application and used.

But now i want to use the IndianMobileLibrary (which is having same structure of MobileLibrary class as below, but it doesn't implement the interface) instead of MobileLibrary

Note: IndianMobileLibrary available in another class library (don't have any control on this library - 3rd party library)

namespace IndianMobileLibrary
{
     public class IndianMobileLibrary  //(doesn't implement the interface)
    {
         List<string> GetMobileDetails()
         {
             return new List<string>();
         }

         int AddMobile(string mobile)
         {
             return 1;
         }
     }
 }

Here how to map the IMobileStore dynamically to IndianMobileLibrary (as implementation), and dynamically create object for IMobileStore that implements IndianMobileLibrary class.

As i heard enterprise library dependency injection will help for this. but i dint use the enterprise library still. could you please share the idea for this.


Simplest solution is to wrap the class which don't implement the Interface, in a simple "proxy" fashion style, just an adapted:

public class MyIndianMobileLibrary:IMobileStore
    {
         public MyIndianMobileLibrary(IndianMobileLibrary indian){
          _indianMobileLibrary = indian;
         }
         IndianMobileLibrary _indianMobileLibrary;

         public List<string> GetMobileDetails()
         {
             return indian.GetMobileDetails();
         }

         public int AddMobile(string mobile)
         {
             return indian.AddMobile(mobile);
         }
     }

    public class IndianMobileLibrary  //(doesn't implement the interface)
        {
             public List<string> GetMobileDetails()
             {
                 return new List<string>();
             }

             public int AddMobile(string mobile)
             {
                 return 1;
             }
         }

EDIT

The solution it's simple but, there is not really necessary, maybe this work:

     public class MyIndianMobileLibrary:IndianMobileLibrary,IMobileStore
    {
         public MyIndianMobileLibrary(){

         }

         public List<string> GetMobileDetails()
         {
             return base.GetMobileDetails();
         }

         public int AddMobile(string mobile)
         {
             return base.AddMobile(mobile);
         }
     }