依赖注入:应该在哪里注入什么?

发布于 2024-11-28 02:08:13 字数 1073 浏览 0 评论 0原文

我有许多类包含写入 CSV 或 XML 文件的功能。 为了实现此目的,我将 StreamWriter 或 XmlWriter 的实例传递给 ToCsv 或 ToXml 方法。

public class HandFeed : JobItem
{
    // Snip

    public override void ToCsv(StreamWriter writer)
    {
        if (writer != null)
        {
            writer.WriteLine(JobItemsStrings.Job_HandFeed);
            writer.WriteLine(JobItemsStrings.Job_Total + SEPARATOR_CHAR + this.Total.ToString());
        }
    }

    public override void ToXml(XmlWriter writer)
    {
        if (writer != null)
        {
            writer.WriteStartElement("handfeed");
            writer.WriteElementString("total", this.Total.ToString());
            writer.WriteEndElement();
        }
    }
}

现在我也希望为 JSON 提供支持,如果我以同样的方式继续,这将意味着沿着“ToJson”或类似的方式向我的所有类添加另一个方法。

嗯,这感觉好像有点不对劲。我正在考虑也许以相反的方式做事,并将类的实例传递给不同的文件编写器对象,但不确定这是否是正确的做法,甚至不确定如何最好地完成它。

也许我可以传入一个接口并调用 Write()?这感觉会更正确,但同样,如何将其付诸实践。

所以,简而言之,我的问题是:

  • 这些课程是否应该“注入”作家?
  • 一些作家应该被“注入”课程吗?
  • 坚持当前的设置,但创建某种形式的界面?

欢迎任何建议。

I have a number of classes that contain the ability to be written to either a CSV or XML file.
To accomplish this, I pass in an instance of a StreamWriter or XmlWriter to ToCsv or ToXml methods.

public class HandFeed : JobItem
{
    // Snip

    public override void ToCsv(StreamWriter writer)
    {
        if (writer != null)
        {
            writer.WriteLine(JobItemsStrings.Job_HandFeed);
            writer.WriteLine(JobItemsStrings.Job_Total + SEPARATOR_CHAR + this.Total.ToString());
        }
    }

    public override void ToXml(XmlWriter writer)
    {
        if (writer != null)
        {
            writer.WriteStartElement("handfeed");
            writer.WriteElementString("total", this.Total.ToString());
            writer.WriteEndElement();
        }
    }
}

Now I am looking to provide suport for JSON too, and if I carry on in the same way, this would mean adding another method to all my classes along the lines of 'ToJson' or similar.

Well, this feels like it is wrong somehow. I am thinking of maybe doing things the other way around and passing instances of the classes to different file writer objects instead, but not really sure if this would be the right thing to do either, or even how best to accomplish it.

Perhaps I can pass in an interface and call Write()? This feels like it would be more correct, but again, how to put this into practice.

So, my questions in a nutshell are:

  • Should the classes be 'injected' with writers?
  • Should some writers be 'injected' with the classes?
  • Stick with the current setup, but create some form of interface instead?

Any advice welcome.

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

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

发布评论

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

评论(2

断爱 2024-12-05 02:08:13

您可以编写符合接口的“Writers”并注入 writers。然而,这意味着给定对象只能在任何单一状态下写入单一格式。这对您来说可能不是问题,如果不是,这就是一种实用的方法。

另一种选择是创建符合接口的编写器,但将对象排除在编写自身的业务之外。相反,某些调用者可以设置编写器并将对象传递给它。这与我刚刚回答的一个问题有关:

应该类能够通过 XML 保存和加载自身吗?

在您的情况下,希望写出的调用者可以通过某个能够解析要使用的正确编写器的工厂获取“编写器”。

我不会坚持您当前的设置,因为正如您提到的,每次需要新格式时,您都需要修改现有代码并添加该格式的方法。另外,你告诉你的对象他们应该知道如何做太多的事情。

You could write "Writers" that conform to an interface and inject the writers. However this would mean that a given object can only be written to a single format in any single state. This might not be a problem for you, and if it isn't this is a practical approach.

The other option would be to create writers that conform to the interface but leave the objects out of the business of writing themselves out. Instead some caller can setup the writer and pass the object to it. This relates to a question I just answered:

Should classes be able to save and load themselves via XML?

In your case the caller that wishes to write out then can get the "writer" via some factory that is able to resolve the correct writer to use.

I wouldn't stick to your current setup because as you mention, each time a new format is needed, you need to modify your existing code and add methods for that format. Also, you are telling your objects that they should know how to do too much.

假装爱人 2024-12-05 02:08:13

我认为您需要应用仆人设计模式,您可以在此链接上阅读相关内容

I think you need to apply the servant design pattern, you can read about it on this link

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