如何处理具有不同属性的相似对象?

发布于 2024-11-14 08:03:01 字数 606 浏览 2 评论 0原文

我正在构建一个 RSS 客户端并使用 Argotic 框架。它为不同类型的提要(例如 Atom、RSS 和 OPML)提供不同的类。这些类不继承自任何其他类,并且它们不实现用于访问其属性的公共接口。

有一个 GenericSynminationFeed 类型实现了一个重载方法,您可以在其中传入 AtomFeedRssFeed。如果我想使用“更多”强类型类,我的程序中的任何地方都需要两个代码路径(一个用于 Atom,一个用于 RSS)。显然,我不会这样做。

除了 API 文档之外,作者没有提供任何文档,所以我有点不明白为什么要以这种方式实现而不是充分利用完整的类。困扰我的一件事是,使用 GenericSynminationItem 类型时我无法获取项目的作者。

我在这里能做什么?制作一个包装类?或者继承 RssFeed 和 AtomFeed 类并实现一个接口来公开我认为两者应该相似的属性?

I'm building an RSS client and using the Argotic framework. It provides different classes for different kinds of feeds like Atom, RSS, and OPML. These classes don't inherit from any other class and they don't implement a common interface for accessing their properties.

There is a GenericSyndicationFeed type that implements an overloaded method where you can pass in an AtomFeed or RssFeed. If I want to use the "more" strongly typed classes I would essentially need two code paths (one for Atom and one for RSS) everywhere in my program. Obviously, I'm not going to do this.

There is no documentation from the author other than the API documentation, so I'm kind of at a loss as to why it was implemented this way instead of taking full advantage of the complete classes. One thing that bothers me is that I can't get the authors of an item when using the GenericSyndicationItem type.

What can I do here? Make a wrapper class? Or inherit from the RssFeed and AtomFeed classes and implement an interface to expose the properties I feel should be similar from both?

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

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

发布评论

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

评论(2

亽野灬性zι浪 2024-11-21 08:03:01

当您使用第三方库并且该库不能满足您的架构需求时:适应!但如何呢?

您已经确定了一些选项,还有更多选项:

  1. 使用适配器模式<将现有类包装在新类中/a>
  2. 通过实现通用接口来扩展和统一不同的类重构
  3. 原始代码以使用本地多态

如果现有的类确实根本没有公共基类,那么前两个选项的工作量大致相同。包装的优点是耦合稍微松散,以防您决定切换到不同的框架。扩展避免了许多像 adaptee.AdapteeMethod 这样的代码,因为您可以在不指定实例的情况下调用基本方法。在这种情况下,我会倾向于适配器模式,除非至少有一些公共基类可以通过继承来利用。

最后一个重要的选择是将代码重构为更加面向对象,只有当您打算回馈项目并得到项目作者的支持时,我才推荐这种方法。原因是您可能没有完全理解您的工作代码,乱搞它只会有破坏它的风险。保留工作代码并从外部对其进行调整。

When you are using a third-party library and the library doesn't meet your architectural needs: adapt! But how?

You've already identified some of your options and there are more:

  1. Wrap existing classes in new classes using the Adapter Pattern
  2. Extend and unify disparate classes by implementing a common interface
  3. Refactor the original code to use Polymorphism natively

If the existing classes really have no common base class at all, then the first two options are both about the same amount of work. Wrapping has the advantage of a little looser coupling in case you ever decide to switch to a different framework. Extending avoids a lot of code like adaptee.AdapteeMethod since you can call base methods without specifying an instance. In this case I would lean towards the adapter pattern unless there is at least some common base class you can exploit through inheritance.

The last serious option is refactoring the code to be more object-oriented and I only recommend this approach if you are intending to contribute back to the project and have the blessing of the project's author. The reason is that you have working code that you probably don't full understand and messing around with it just risks breaking it. Leave the working code alone and adapt it from the outside.

半岛未凉 2024-11-21 08:03:01

自从我写 Argotic 以来已经有很长一段时间了(它是在 System.ServiceModel.Syndicate 存在于 .NET 中),但由于作者的概念同时存在于 RSS 2.0Atom,我不太记得为什么通用提要项不包含作者集合。这可能是因为 OPML 文档中的大纲元素没有作者的概念。显然我的设计很糟糕。

底线是我还年轻,还在学习,Argotic 在 3 年前写的时候虽然很有用;不幸的是需要进行重大重构。如果 System.ServiceModel.Synmination 可以满足您的需求,我建议您使用它来解析您的聚合提要。

由于您拥有 Argotic 的完整源代码,但它不能满足您的需求;您可以将 Authors 集合添加到通用联合项目类中,并在使用 RSS 或 Atom 提要时填充它。

无论您是否做出贡献,我都绝对会祝福您按照您认为合适的方式进行重构,我几年前就放弃了项目职责,并且不确定现在的状态如何。

话虽如此,如果您在使用 Feed 之前知道 Feed 的格式,则可以执行以下操作:

RssFeed feed = RssFeed.Create(new Uri("http://www.pwop.com/feed.aspx?show=dotnetrocks&filetype=master"));

AtomFeed feed = AtomFeed.Create(new Uri("http://news.google.com/?output=atom"));

It has been a really long time since I wrote Argotic (it was written before System.ServiceModel.Syndication existed in .NET), but since the concept of author exists in both RSS 2.0 and Atom, I don't really recall why the generic feed item did not include an Authors collection. It may have been because the outline elements in an OPML document do not have the concept of author. Poor design on my part obviously.

The bottom line is I was still young and learning, and Argotic while useful when it was written 3 years ago; is woefully in need of a major refactoring. If System.ServiceModel.Syndication can fulfill your needs, I recommend you use that to parse your syndication feeds.

Since you have the full source code to Argotic, and it is not fulfilling your needs; you could add an Authors collection to the generic syndication item class and populate it when consuming an RSS or Atom feed.

You most definitely have my blessing to refactor as you see fit regardless of whether you contribute back, I passed off project responsibilities years ago and am not sure what state it is in these days.

That said and done, if you know the format of the feed prior to consuming it, you can do the following:

RssFeed feed = RssFeed.Create(new Uri("http://www.pwop.com/feed.aspx?show=dotnetrocks&filetype=master"));

AtomFeed feed = AtomFeed.Create(new Uri("http://news.google.com/?output=atom"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文