EF – manage and display associated objects

This post teaches you to manage and display associated objects.

.

As an example, we’ll use the Document and Comment entities that were defined for your programming assignment’s blogging module.

.

The purpose of “comments”

A browser user will be able to add a comment to one of your blog posts.

In addition, the comment can be a “reply” to an existing comment.

The suggested Comment entity appears below.

  • It is associated (related) with the Document entity, with a to-many association. That is, a Document can have many Comments.
  • A Comment object has a Comments collection. This to-many association enables a comment to have one or more replies.
  • A Comment can be associated with another comment, as a reply.

.

.

Display and create comments

Create a standard web form (or a user control) to enable you to create new comments.

A comment must be associated with a Document object, so creation and editing must be done in the context of working with a Document.

To display your categories, you can use any data display control, including list controls. See below for more.

.

Setting the Document – Comment association

When adding a Comment to a Document (i.e. a blog post), follow this approach:

Get a reference to a Document object. You may fetch one from the store, or create a new one.

Get a reference to a Comment object. Typically, you’re going to create a comment only when you have a reference to a Document object.

Add the Comment to the Document’s “Comments” collection, use the collection’s “Add()” method. For example:

doc.Comments.Add(com);

You do not have to add the Document to the Comment’s “Document” property. Setting the association above will enable you to use it both ways.

If you want to remove the Comment from the Document’s “Comments” collection, use the collection’s “Remove()” method. For example:

doc.Comments.Remove(com);

Then, delete the Comment object.

.

Setting the Comment – Comment association

When adding a Comment as a reply to another Comment, follow this approach:

Get a reference to the “parent” Comment object. Typically, you have fetched it.

Get a reference to a “reply” Comment object. Typically, you’re going to create this comment, only when you have a reference to a Document object, and a reference to the “parent” Comment object.

Add the “reply” Comment to the “parent” Comment’s “Comments” collection, use the collection’s “Add()” method. For example:

parent.Comments.Add(reply);

.

Display comments – flat style

There are several approaches to displaying comments. Two typical ways to display them are “flat” and “threaded”.

Flat: This is the minimum requirement for the programming assignment. Simply obtain a Document’s collection of Comments, and display then using a programming structure that you feel works best.

.

Display comments – threaded (nested) style

Threaded: Implementing threaded comments, where replies appear directly under their associated comment, and indented nicely, will earn you more marks on the programming assignment.

 

.


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: