开发者

Circular compositional pattern with RSS Feed and FeedItem classes

I'm designing a small system to parse RSS feeds, and I have two classes: Feed and FeedItem.

public class Feed
{
    public string Title{ get; set; }
    public string Link{ get; set; }
    public string Description { get; set; }
    public bool IsTwitterFeed { get; set; }
    public List<FeedItem> Items { get; set; }
}

public class FeedItem
{
    public string Title { get; set; }
    public string Link{ get; set; }
    public string Description { get; set; }
    public DateTime Date { get; set; }
}

Feeds have FeedItems, and FeedItems have parent Feeds. Would it be a bad pattern to give the FeedItem class a parent Feed member:

public Feed ParentFeed { get; set; }

so I could do this:

// get the latest item from the latest feed, then print its parent feed name
FeedItem item = Feeds.GetLatest().Item[0];
Response.Write(i开发者_StackOverflow中文版tem.ParentFeed.Name + ": " + item.Title);

or should I only ever get a FeedItem through its parent Feed, to avoid this circular reference between these two classes?


If you really need the parent then you could store it as an object. It would be necessary if you can get a FeedItem through some other route than from the Feed and need to get to the Feed. However, it's not particularly elegant.

This is usually done in cases where there can be different types of parent object.


Why doesn't your caller just store the reference to the Feed?

Feed latest = Feeds.GetLatest();
FeedItem item = latest.Item[0];
Response.Write(latest.Name + ": " + item.Title);

Edit: Personally I'd avoid the circular reference if possible but I guess that's just a matter of taste!?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜