title: Factory Method with Generic Interface
body: <p>I have two different types of item: Picture and Video. In order to manage them, I have created an interface and two classes which implement it</p>
  <pre><code>public interface Item{
  }
  
  public class ItemPicture: Item{
  }
  
  public class ItemVideo: Item {
  }
  </code></pre>
  <p>Now I have some classes to manage that items, that inherit from a manager interface</p>
  <pre><code>public interface ItemManager&lt;T&gt;{
     IList&lt;T&gt; FGetByGroup(string idgroup);
  }
  
  public class ItemManagerPictures : IItemManager&lt;ItemPicture&gt;{
     public IList&lt;ItemFoto&gt; FGetByGroup(string idgroup){
        ...
     }
  }
  public class ItemManagerVideos: IItemManager&lt;ItemVideo&gt;{
     public IList&lt;ItemVideo&gt; FGetByGroup(string idgroup){
        ...
     }
  }
  </code></pre>
  <p>In order to have a factory method which creates the appropriate object, I have created this class</p>
  <pre><code>public class ItemManagerCreator
      {
          public static IItemManager&lt;Item&gt; MakeItemManager(string type)
          {
              IItemManager&lt;Item&gt; objMgr = null;
  
              switch (type)
              {
                  
                  case &quot;Picture&quot;:
                      objMgr = (IItemManager&lt;Item&gt;)new ItemManagerPictures();
                      break;
  
                  case &quot;Video&quot;:
                      objMgr = (IItemManager&lt;Item&gt;)new ItemManagerVideos();
                      break;
  
                  default:
                      break;
              }
              return objMgr ;
  
          }
      }
  </code></pre>
  <p>From the controller I want to do this</p>
  <pre><code>var type=&quot;Picture&quot;;
  
  IItemManager&lt;Item&gt; itemMgr = ItemManagerCreator.MakeItemManager(type);
  var itemList = itemMgr.FGetByGroup(string idgroup);
  </code></pre>
  <p>But I get this casting error</p>
  <blockquote>
  <p>Can't convert from type '...ItemManagerPictures' to type  '...IItemManager`1[...Item]'.</p>
  </blockquote>
  <p>which makes me think I'm doing something wrong, but after 1000 turns, I think the problem is with the factory method and generics, which is not well designed.</p>
  <p>I'm new to design patterns, and maybe I'm not applying the right one.</p>
  <p>Thanks in advance</p>