管理工厂类的基类

发布于 2025-01-06 16:36:55 字数 1259 浏览 4 评论 0原文

我有一个 StaffFactory 用于通过各种方式获取 Staff 对象,但我也有一些设置方法来确定要使用哪个数据源。

class StaffFactory
{
    private const string DefaultDbSourceName = "Production";
    private string dbSourceName;
    #region Factory Management
    private static Dictionary<string, StaffFactory> staffFactories = new Dictionary<string,StaffFactory>();
    public static StaffFactory GetInstance()
    {
        return GetInstance(DefaultDbSourceName);
    }
    public static StaffFactory GetInstance(string dbSourceName)
    {
        if (!staffFactories.ContainsKey(dbSourceName))
        {
            staffFactories.Add(dbSourceName, new StaffFactory(dbSourceName));
        }
        return staffFactories[dbSourceName];
    }
    private StaffFactory(string dbSourceName)
    {
        this.dbSourceName = dbSourceName;
    }
    #endregion Factory Management
    #region Factory Methods
    public Staff ById(int id) { ... }
    public IList<Staff> ByName(string name) { ... }
    ...
    #endregion Factory Methods
}

当我要创建下一个工厂时,我意识到无论工厂是什么类型,所有这些管理逻辑都将保持不变。所以我想我创建一些包含该逻辑的基本 Factory 或 Factory 类,然后使用 class StaffFactory : Factory声明上述内容{ ... } 之类的,但我对如何解决这个问题完全空白。是否使用泛型等来实现它。

任何人都可以指出我正确的方向吗?

I have a StaffFactory for getting Staff objects by various means, but I also have some setup methods to determine which data source to use.

class StaffFactory
{
    private const string DefaultDbSourceName = "Production";
    private string dbSourceName;
    #region Factory Management
    private static Dictionary<string, StaffFactory> staffFactories = new Dictionary<string,StaffFactory>();
    public static StaffFactory GetInstance()
    {
        return GetInstance(DefaultDbSourceName);
    }
    public static StaffFactory GetInstance(string dbSourceName)
    {
        if (!staffFactories.ContainsKey(dbSourceName))
        {
            staffFactories.Add(dbSourceName, new StaffFactory(dbSourceName));
        }
        return staffFactories[dbSourceName];
    }
    private StaffFactory(string dbSourceName)
    {
        this.dbSourceName = dbSourceName;
    }
    #endregion Factory Management
    #region Factory Methods
    public Staff ById(int id) { ... }
    public IList<Staff> ByName(string name) { ... }
    ...
    #endregion Factory Methods
}

As I go to create my next factory, I realise all this management logic is going to remain the same regardless of what type the factory is for. So I'm thinking I create some base Factory or Factory class that houses that logic, and then my declare the above with class StaffFactory : Factory<Staff> { ... } or something, but I'm drawing complete blanks on how I would go about that. Whether or not to implement it using generics, etc.

Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2025-01-13 16:36:55

在引入新的抽象层之前,请确保收益大于成本。在您的情况下,这些将包括:

设计和实现抽象的成本

StaffFactory 比通用 Factory更容易设计、实现和测试。班级。

了解工厂的成本抽象

每次有人阅读代码时,他们可能都需要思考抽象。通用抽象比非通用抽象更难理解。

未来进行更改的成本

假设您有 Factory和 Factory,将来您会发现您需要为两者采用不同的缓存策略。也许工厂<产品>如果缓存大小超过某个阈值,则应丢弃缓存的对象。

您打算概括 Factory<> 吗?类支持不同的模式?这是可以做到的,但它比单独修改 StaffFactory 和 ProductFactory 类要复杂得多。

总结

因此,不要仅仅为了抽象而引入抽象。并且,绝对不要将 StaffFactory 概括为通用 Factory。如果工厂<员工>将是您拥有的唯一通用实例化。

从上面的代码来看,您的 Factory似乎是类基本上是一个 Dictionary。如果是这样的话,那么引入额外的泛型 Factory 类不会给您带来太多好处,只会添​​加不必要的抽象层。

Before you introduce a new abstraction layer, make sure that the benefits will outweigh the costs. In your case, those would include:

The cost to design and implement the abstraction

StaffFactory is much easier to design, implement and test than a generic Factory<T> class.

The cost to understand the Factory<T> abstraction

Every time someone reads the code, they may need to wrap their head around the abstraction. A generic abstraction is harder to wrap your head around than a non-generic one.

The cost to make changes in the future

Let's say that you have Factory<Staff> and Factory<Product>, and in the future you find that you want a different caching policy for the two. Maybe the Factory<Product> should discard the cached object if the cache size exceeds some threshold.

Are you going to generalize the Factory<> class to support different modes? That can be done, but its much more complex than just modifying StaffFactory and ProductFactory classes independently.

Summary

So, don't introduce an abstraction just for the sake of it. And, definitely do not generalize StaffFactory into a generic Factory<T> if Factory<Staff> is going to be the only generic instantiation you'll have.

From your code above, it seems that your Factory<T> class would be basically a Dictionary<string, T>. If that's the case, then introducing the additional generic Factory class does not get you much benefit and only adds an unnecessary abstraction layer.

海未深 2025-01-13 16:36:55

据我了解,您要实现的是存储库模式。请参阅C# 存储库模式教程的答案。

As I understand what you're going to implement is a Repository pattern. See the answers to Repository pattern tutorial in C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文