1

我正在尝试使我正在设计的应用程序更通用,并在其中实现命令模式以使用管理器类来调用接口公开的方法。

我有几个类,其中有GetItem()GetList()方法,有些是重载的。当我尝试使用依赖注入时,它们接受不同的参数,并且它们返回不同的类型。这里有几个例子:

 class DatastoreHelper
    {
        public Datastore GetItem(string DatastoreName)
        {
            // return new Datastore(); from somewhere
        }
        public Datastore GetItem(int DatastoreID)
        {
            // return new Datastore(); from somewhere
        }
        public List<Datastore> GetList()
        {
           // return List<Datastore>(); from somewhere
        }
        public List<Datastore> GetList(HostSystem myHostSystem)
        {
           // return List<Datastore>(); from somewhere
        }

    }
    class HostSystemHelper
    {
        public HostSystem GetItem(int HostSystemID)
        {
          //  return new HostSystem(); from somewhere
        }
        public List<HostSystem> GetList(string ClusterName)
        {
            //return new List<HostSystem>(); from somewhere
        }
    }

我试图弄清楚我是否可以为这两种方法使用通用接口,以及一个可以有效地作为控制器的管理器类。这样做会增加我的经理类的重用能力。

  interface IGetObjects
    {
        public object GetItem();
        public object GetList();
    }

class GetObjectsManager
{
    private IGetObjects mGetObject;
    public GetObjectsManager(IGetObjects GetObject)
    {
        this.mGetObject = GetObject;
    }
    public object GetItem()
    {
        return this.mGetObject.GetItem();
    }
    public object GetList()
    {
        return this.GetList();
    }
}

我知道我必须放弃将参数传递给方法本身并使用类属性,但我会失去依赖注入。我知道我必须将调用代码中的返回对象转换为它们应该是的。所以我的助手类看起来像这样:

class DatastoreHelper
{
    public string DatastoreName { get; set; }
    public string DatastoreID { get; set; }
    public object GetItem()
    {
        // return new Datastore(); from somewhere
    }
    public List<object> GetList()
    {
       // return List<Datastore>(); from somewhere
    }

}
class HostSystemHelper
{
    public int HostSystemID { get; set; }
    public string ClusterName {get; set;}
    public object GetItem()
    {
      //  return new HostSystem(); from somewhere
    }
    public List<object> GetList()
    {
        //return new List<HostSystem>(); from somewhere
    }
}

但以上是一个好主意,还是我试图在不属于它的地方适应模式?

编辑:我添加了一些更多的重载方法来说明我的类很复杂并且包含许多方法,有些方法根据不同的输入参数重载了很多次。

4

3 回答 3

2

如果我正确理解了这个概念,那么这样的设计是一个非常糟糕的主意:

class DatastoreHelper
{
    public string DatastoreName { get; set; }
    public string DatastoreID { get; set; }
    public object GetItem()
    {
        // return new Datastore(); from somewhere
    }
    public List<object> GetList()
    {
       // return List<Datastore>(); from somewhere
    }
}

原因是现在获取结果将是一个两步过程:首先设置属性,然后调用方法。这带来了一系列问题:

  • 不直观(每个人都习惯于在方法调用中提供参数)
  • 将参数绑定从调用站点移开(当然,这可能意味着“将它们移动到上一个 LOC”,但仍然如此)
  • 哪个方法使用哪个属性值不再明显
  • 取这个对象的一个​​实例,然后添加几个线程来立即获得乐趣

建议:

  1. 使两者都IGetObjects通用GetObjectsManager,这样您就不会失去类型安全性。这使您失去了以多态方式对待不同经理的能力,但这有什么意义呢?每个管理器最终都将专门用于特定类型的对象,除非您知道该类型是什么,否则您无法真正使用 getter 方法的返回值。那么,通过将管理者视为“未知的管理者”,你能获得什么?
  2. 考虑重写您的GetX方法以接受Expression<Func<T, bool>>而不是裸值。通过这种方式,您可以使用 lambda 谓词,这将使您的代码更加灵活而不会真正丢失任何内容。例如:

    helper.GetItem(i => i.DataStoreID == 42);
    helper.GetList(i => i.DataStoreName.Contains("Foo"));
    
于 2012-04-06T11:29:49.703 回答
1

It is not a good idea. Based on these examples you would be better off with a generic interface for the varying return type and parameters of GetItem/GetList. Though honestly the prevalence of Managers, the use of something cas vague as GetItem in multiple places and trying to fit your solution into design patterns (rather than defining the solution in terms of the patterns) are huge code smells to me for the wider solution.

于 2012-04-06T11:37:37.413 回答
1

第一个代码示例看起来与存储库模式非常相似。我认为这就是您要申请的内容。最后一个样本不好,Jon 告诉你原因。但是,不要重新发明轮子,而是阅读一些关于存储库的内容(关于它的很多问题),因为如果我理解正确,这就是你真正想要的。

关于重用,可重用的东西并不多,尤其是持久化接口。有一个通用存储库模式(我认为它是一种反模式)试图实现这一点,但实际上,所有应用程序都需要相同的持久性接口吗?

作为一般准则,当你设计一个对象时,设计它以满足特定应用程序的需求,如果它碰巧被重用,那是一个奖励,但这不是一个对象的主要目的。

于 2012-04-06T17:28:24.440 回答