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
public interface Item{
}
public class ItemPicture: Item{
}
public class ItemVideo: Item {
}
Now I have some classes to manage that items, that inherit from a manager interface
public interface ItemManager<T>{
IList<T> FGetByGroup(string idgroup);
}
public class ItemManagerPictures : IItemManager<ItemPicture>{
public IList<ItemFoto> FGetByGroup(string idgroup){
...
}
}
public class ItemManagerVideos: IItemManager<ItemVideo>{
public IList<ItemVideo> FGetByGroup(string idgroup){
...
}
}
In order to have a factory method which creates the appropriate object, I have created this class
public class ItemManagerCreator
{
public static IItemManager<Item> MakeItemManager(string type)
{
IItemManager<Item> objMgr = null;
switch (type)
{
case "Picture":
objMgr = (IItemManager<Item>)new ItemManagerPictures();
break;
case "Video":
objMgr = (IItemManager<Item>)new ItemManagerVideos();
break;
default:
break;
}
return objMgr ;
}
}
From the controller I want to do this
var type="Picture";
IItemManager<Item> itemMgr = ItemManagerCreator.MakeItemManager(type);
var itemList = itemMgr.FGetByGroup(string idgroup);
But I get this casting error
Can't convert from type '...ItemManagerPictures' to type '...IItemManager`1[...Item]'.
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.
I'm new to design patterns, and maybe I'm not applying the right one.
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire