views:

22

answers:

2

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(item.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?

A: 

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.

ChrisF
A: 

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!?

andyp