views:

107

answers:

6

Which pattern could be used in web projects?

I am developing a wiki project with ASP.NET. In my structure every wiki entry could have comments.

Which way is the best :

wikiEntry.AddComments(newComment);

wikiEntry.Comments.Add(newComment);

entryManager.AddComment(wikiEntry, newComment);
+1  A: 

Encapsulation suggests 1. I'd go with that. (But it's not how I'd actually do it).

Noon Silk
A: 

If Comments is really a list of comments where you can perform various operations, like listing, deleting, adding, renaming, changing, I'd go with

wikiEntry.AddComments(newComment);

If wikiEntry somehow manages the comment internally and the only thing you can do is add new ones, I'd go with:

wikiEntry.Comments.Add(newComment);

I would never use:

entryManager.AddComment(wikiEntry, newComment);
J. Pablo Fernández
Then you use wikiEntry.Save() for save process?
oneMinute
A: 

All other thigns being equal, I'd go for:

wikiEntry.AddComments(newComment);

It seems the simplest and clearest way to represent what you're doing. I find the simplest way is usually the best.

macleojw
+3  A: 
wikiEntry.AddComments(newComment);

right, the client code is simple, no worries about how comments are represented internally.

wikiEntry.Comments.Add(newComment);

wrong, the client should worrying about Comments if null? there is no information hiding

entryManager.AddComment(wikiEntry, newComment);

wrong, responsabilities are wrong resulting in a procedural code

dfa
+1  A: 

In addition to previous answers I will write only about the second way:

wikiEntry.Comments.Add(newComment);

This way is not so bad as other says

  1. All work relates to Comments done through Comments property, which is easy and intuitive.

  2. There no need to add a new method for every new functionality to owner object (like AddComment, RemoveComment, FindComment, etc). This way the owner object doesn't contain methods for all inner objects, it's smaller and cleaner. There are clean code separation.

  3. If you want to query Comments, you just do this, and there no need to add specific FindBySomething methods to owner.

You can make Comments never be null - that make unnecessary tests for null on every call. In general I don't see a problem with information hiding, but this may depend on specific scenario or application.

Microsoft widely use this pattern in their controls. See DataGrid with properties Items and Attributes.

Kamarey
A: 

I would definitely go with the wikiEntry.Comments.Add(newComment) method. I've listed my reasons below.

It feels intuitive because it is the same way Microsoft's existing controls work, so your code naturally feels like a part of the codebase.

It separates functionality of one thing - Comments - into its own area of responsibilities. Within the Comments collection you can add, edit, remove, order, etc. That way you don't cloud the main namespace if, say, you wanted to also be able to add, edit, remove, order, etc a list of References, Editors, Moderators, historical snapshots, whatever (not saying you would want a collection for each of these, many/most you probably wouldn't)

You can reuse the Comments collection in other classes and already have functionality built in for handling modifications to the list.

Think: wikiEntry.Comments[parentComment].Comments.Add(newComment);

gives you an easy way to represent nested comments.

Aaron