3

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.

user3794983
  • 31
  • 1
  • 2

3 Answers3

2

You should be interested in the great impromptu interface library (available via nuget). With its help, you can simply do a magic like:

var indian = new IndianMobileLibrary();
IMobileStore iface = indian.ActLike<IMobileStore>();
var mobile = iface.AddMobile("test");

And you have full intellisense support for ActLike returned object of course. Another magic advantage of this is you do not have to know the type of indian at all.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • @Konrad, Thanks for your support... I have tried to download impromptu interface Binaries from above link. But i am not getting the dll. Could you please share the (step by step) link from where i can get the proper dll for impromptu interface to add in my project. – user3794983 Jul 01 '14 at 21:01
  • i have installed the impromptu interface Binaries, could you please give idea how to do the above stuff without using indian type as i dont know the type beforehand. – user3794983 Jul 01 '14 at 21:19
  • @user3794983, Just add reference to this dll, add `using ImpromptuInterface;` and call `yourObject`.ActLike() to get `IMobileStore` reference. – Konrad Kokosa Jul 02 '14 at 05:30
1

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

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);
         }
     }
ale
  • 10,012
  • 5
  • 40
  • 49
  • @lordkain why do you think this is funny? standard proxy/adaptor pattern in practice. – trailmax Jul 01 '14 at 20:10
  • simple and elegant solution, I was thinking to complex – lordkain Jul 01 '14 at 20:11
  • Actually beforehand i dont know the class name IndianMobileLibrary itself. Some 3-rd party vendor is developing the IndianMobileLibrary functionality side by side. Think i have developed one mock project for development while staging only they are providing the original dll. * Before hand i know/agreed only function signatures* – user3794983 Jul 01 '14 at 20:14
  • @user3794983, then I suggest my solution. – Konrad Kokosa Jul 01 '14 at 20:25
  • This is overkill, in big projects this doesn't work. You would spend more time writing wrappers than actual code. – Daniel Lobo Dec 15 '17 at 17:47
0

The most straightforward way around this is to create yourself a proxy to the IndianMobileLibrary functionality. You can create a proxy class that does implements IMobileInterface but uses the IndianMobileLibrary implementation.

uses IndianMobileLibrary;

namespace MyIndianMobileLibrary 
{
    public class MyIndianMobileLibraryImpl : IMobileStore
    {
        List<string> GetMobileDetails()
        {
             return new IndianMobileLibrary.GetMobileDetails();
        }

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

Now you have a compile-able class implementing your logic and can be injected wherever IMobileStore is invoked.

Ross Bush
  • 14,648
  • 2
  • 32
  • 55