locked
Help in Adapter Pattern RRS feed

  • Question

  • I need help in designing adapter pattern that will map data from different sources to predefined C# classes. Let’s say i have one big C# class for each of the US state. I want to populate these State classes from different data sources. I want to create Adapter for each of the State Class. So adapter's concrete implementation will be responsible for mapping. Following is the implementation I’m thinking. But I have questions
    1>What should be the name of the class that takes IMapper interface in the constructor? (See implementation below)
    2>How do I get the actual instance of mapped class in this class.
    3>NOTE: I cannot add any interface to State classes (eg Texas, NewYork etc). I don’t have access to these classes.
    4>We may be using dependency injection to inject proper adapter at runtime
      public class Texas
      {    
      }
    
      public class NewYork
      {    
      }
    
      public class California
      {    
      }
    
      public interface IMapper
      {
        void MapData();
      }
    
      public class TexasAdapter : IMapper
      {
        Texas t = new Texas(); 
        public void MapData()
        {      
        }
      }
    
      public class NewYorkAdpter : IMapper
      {
        NewYork n = new NewYork();
        public void MapData()
        {
        }
      }
    
      public class CaliforniaAdpter : IMapper
      {
        California c = new California();    
        public void MapData()
        {
        }
      }
    
      public class WhatShouldICallThisClass //?? what should i call this call
      {
        IMapper _mapper;
        public WhatShouldICallThisClass(IMapper mapper)
        {
          _mapper = mapper;
        }
        public void CreateXML()
        {
          _mapper.MapData();
          //?? I need instance of Texas,NewYork and California here
          //?? How do get instance of mapped object here?? 
          //I need to serialize the instace of mapped class to create XML
          
          //This class has no knowledge of Texas,NewYork and California classes.
          //So may be we have to use generic here. but how??
        }
      }
    
      public class Client
      {
        public void Call()
        {
          //for texas
          IMapper mapper = new TexasAdapter();
          WhatShouldICallThisClass wht = new WhatShouldICallThisClass(mapper);
          wht.CreateXML();
        }
      }
    




    Wednesday, April 20, 2011 2:13 AM