I am looking for a way to make the examples I provided more generic if possible. Could anyone provide some guidance on the best way to do this?
I have over 10 classes that look identical except for the data type and the static manager calls that are used. Some of the static method calls to the managers may have different implementations. In the example I used Item1 and Item2 as examples.
// Item1
public static class Item1Extensions
{
public static void Save(this Item1 item)
{
try
{
if (!item.IsDirty) return;
Item1Manager.SaveItem(item);
}
catch (Exception ex)
{
throw new ItemSaveException();
}
}
public static Item1 Get(long id, long callerId)
{
Item1 item;
try
{
item = Item1Manager.GetItem(id, callerId);
}
catch (Exception ex)
{
throw new ItemRetrieveException();
}
return item;
}
public static List<Item1> List(long callerId)
{
return Item1Manager.GetItemsByCallerId(callerId).Where(x => x.IsActive).ToList();
}
public static bool Delete(long id, long callerId)
{
try
{
Item1Manager.DeactivateItem(id, callerId);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
Item 2 is almost identical except maybe the manager calls
// Item2
public static class Item2Extensions
{
public static void Save(this Item2 item)
{
try
{
if (!item.IsDirty) return;
Item2Manager.SaveItem(item);
}
catch (Exception ex)
{
throw new ItemSaveException();
}
}
public static Item2 Get(long id, long callerId)
{
Item2 item;
try
{
item = Item2Manager.GetItem(id, callerId);
}
catch (Exception ex)
{
throw new ItemRetrieveException();
}
return item;
}
public static List<Item2> List(long callerId)
{
return Item2Manager.GetItemsByCallerId(callerId).Where(x => x.IsNotActive).ToList();
}
public static bool Delete(long id, long callerId)
{
try
{
Item2Manager.DeactivateItem(id, callerId);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
I thought using some generics or a factory based pattern would be helpful but I could use some guidance. Since the structure of the logic layer classes look identical except for the manager calls it seems that the duplication of this logic can be simplified like
public static class Item2ManagerCalls
{
public static void Save(Item2 item)
{
Item2Manager.SaveItem(item);
}
public static Item2 Get(long id, long callerId)
{
return Item2Manager.GetItem(id, callerId);
}
public static List<Item2> List(long callerId)
{
return Item2Manager.GetItemsByCallerId(callerId).Where(x => x.IsActive).ToList();
}
public static bool Delete(long id, long callerId)
{
return Item2Manager.DeactivateItem(id, callerId);
}
}