i'm a newbie to both C# and Monotouch/iOS programming.

I am creating a table view's data source and was setting up the object data for it. Here is what i need to implement: Restaurant Menu --> Menu Sections --> Section Items. One additional requirement that the restaurant has multiple menus. I designed the classes as follows:

public class Menu
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<MenuSection> Sections { get; set; }
    public List<SectionItem> Items { get; set; }
}

public class MenuSection
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class SectionItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
} 

Do i declare the Items (SectionItems) in Menu class or in MenuSection class? What is the correct implementation of such a hierarchy in C# ? Thanks!

UPDATE

@Jason, With the solution you proposed i am running into an implementation issue. How do i relate Items to a Menu? I have to go through the hierarchy Item->Section->Menu. Would i be able to add Items to a specific menu given your proposed structure?

Ex:

List<Menu> menuList = new List<Menu> ();
Menu menu;

menu = new Menu (){ Id = 1, Name = "Lunch Menu" };
menu.Sections.Add(new MenuSection(){ Id = 1, Name = "Specials"});
menuList.Add(menu);

menu = new Menu (){ Id = 2, Name = "Dinner Menu" };
menu.Sections.Add(new MenuSection(){ Id = 1, Name = "Salads"});
menuList.Add(menu);

How do i add items without knowing which menu and which sections i am adding it to? Can you please provide an example of adding an item to one of the menus above? One solution that comes to mind is to denormalize Menu and Section by adding Menu.Id and Menu.Name to Section then i only have to deal with a single hierarchy; but, that would be avoiding the issue. There must be a better way. Thanks for helping.

有帮助吗?

解决方案

Item should be a child of Section.

public class Menu
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<MenuSection> Sections { get; set; }

}

public class Section
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<SectionItem> Items { get; set; }
}

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
} 

UPDATE

To access/add items to a Menu (there are multiple ways)

// adding by index
menuList[0].Sections[0].Items.Add(new Item() { .. init .. });

// keep a reference to a section
Section dinner = new Section() { .. init .. };
dinner.Items.Add(new Item() { .. init ..});
dinner.Items.Add(new Item() { .. init ..});
menu.Sections.Add(dinner);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top