使C#逻辑层调用通用

时间:2015-07-28 00:25:26

标签: c# generics

我正在寻找一种方法来使我提供的示例更加通用。任何人都可以提供一些最佳方法的指导吗?

除了数据类型和使用的静态管理器调用之外,我有10个以上看起来相同的类。对管理器的一些静态方法调用可能有不同的实现。在示例中,我使用Item1和Item2作为示例。

// 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;
        }
    }
}

项目2几乎完全相同,除非经理可以打电话

// 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;
        }
    }
}

我认为使用一些仿制药或基于工厂的模式会有所帮助,但我可以使用一些指导。由于逻辑层类的结构看起来相同,除了管理器调用之外,似乎可以简化这种逻辑的重复,如

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);
    }
}

1 个答案:

答案 0 :(得分:1)

假设所有项类继承自某些基类名称,例如包含 .IsDirty .IsActive 属性的 BaseItem ,那么类似于这应该有效:

// This is a fill for a class I assume that you already have
public class BaseItem
{
    public bool IsDirty;
    public bool IsActive;
}

// This is a fill for a class I assume that you already have
public class Item1 : BaseItem {}

// This is a fill for a class I assume that you already have
public class Item2 : BaseItem {}

// This is a fill for a class I assume that you already have
public class ItemSaveException : ApplicationException {}

// This is a fill for a class I assume that you already have
public class ItemRetrieveException : ApplicationException {}

// This is a fill for a class I assume that you already have
public static class Item1Manager
{
    internal static void SaveItem(Item1 item) {}

    internal static Item1 GetItem(long id, long callerId) { return new Item1(); }

    internal static List<Item1> GetItemsByCallerId(long callerId) { return new List<Item1>(); }

    internal static void DeactivateItem(long id, long callerId) {}
}

// This is a fill for a class I assume that you already have
public static class Item2Manager
{
    internal static void SaveItem(Item2 item) {}

    internal static Item2 GetItem(long id, long callerId) { return new Item2(); }

    internal static List<Item2> GetItemsByCallerId(long callerId) { return new List<Item2>(); }

    internal static void DeactivateItem(long id, long callerId) {}
}

public abstract class ItemManagerAdapter<TItemManagerAdapter, TItem>
    where TItemManagerAdapter : ItemManagerAdapter<TItemManagerAdapter, TItem>, new() 
    where TItem : BaseItem
{

    private static TItemManagerAdapter instance = new TItemManagerAdapter();

    protected abstract void SaveItem(TItem item);
    protected abstract TItem GetItem(long id, long callerId);
    protected abstract List<TItem> GetItemsByCallerId(long callerId);
    protected abstract void DeactivateItem(long id, long callerId);

    public static void Save(TItem item)
    {
        try
        {
            if (!item.IsDirty) return;

            instance.SaveItem(item);
        }
        catch (Exception ex)
        {
            throw new ItemSaveException();
        }
    }

    public static TItem Get(long id, long callerId)
    {
        TItem item;
        try
        {
            item = instance.GetItem(id, callerId);
        }
        catch (Exception ex)
        {
            throw new ItemRetrieveException();
        }

        return item;
    }

    public static List<TItem> List(long callerId)
    {       
        return instance.GetItemsByCallerId(callerId).Where(x => x.IsActive).ToList();
    }

    public static bool Delete(long id, long callerId)
    {
        try
        {
            instance.DeactivateItem(id, callerId);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

}

public class Item1ManagerAdapter : ItemManagerAdapter<Item1ManagerAdapter, Item1>
{
    protected override void SaveItem(Item1 item) { Item1Manager.SaveItem(item); }
    protected override Item1 GetItem(long id, long callerId) { return Item1Manager.GetItem(id, callerId); }
    protected override List<Item1> GetItemsByCallerId(long callerId) { return Item1Manager.GetItemsByCallerId(callerId); }
    protected override void DeactivateItem(long id, long callerId) { Item1Manager.DeactivateItem(id, callerId); }
}

public class Item2ManagerAdapter : ItemManagerAdapter<Item2ManagerAdapter, Item2>
{
    protected override void SaveItem(Item2 item) { Item2Manager.SaveItem(item); }
    protected override Item2 GetItem(long id, long callerId) { return Item2Manager.GetItem(id, callerId); }
    protected override List<Item2> GetItemsByCallerId(long callerId) { return Item2Manager.GetItemsByCallerId(callerId); }
    protected override void DeactivateItem(long id, long callerId) { Item2Manager.DeactivateItem(id, callerId); }
}

像这样使用它:

public class Program
{

    void Main()
    {
        var item1 = Item1ManagerAdapter.Get(1, 1);
        Item1ManagerAdapter.Save(item1);
        var item1Deleted = Item1ManagerAdapter.Delete(1, 1);
        var item1s = Item1ManagerAdapter.List(1);

        var item2 = Item2ManagerAdapter.Get(2, 2);
        Item2ManagerAdapter.Save(item2);
        var item2Deleted = Item2ManagerAdapter.Delete(2, 2);
        var item2s = Item2ManagerAdapter.List(2);
    }

}