views:

35

answers:

2

When I'm asking this question on stackoverflow, I can add tags to it. So, in DDD, I would have a class Question that somehow has tags. One way to model it would be with a List of tags, since a tag is not really an entity (or is it?).

public class Question
{
    // ...

    public List<string> Tags;
}

In the database, however, I would probably have these three tables:

Question: QuestionID, Title, Content
Tag: TagID, Title
QuestionTag: QuestionID, TagID

Would this be possible from an NHibernate perspective? Would it be recommended? How would you do it?

A: 

While what you describe is possible you may want to consider Tag as an entity. Consider the situation where you want to display a list of questions for a particular Tag. It will therefore result in a many to many relationship between the Tag entity and the Question entity. The database structure will pretty much end up the way you envision.

Vincent Ramdhanie
+1  A: 

It depends on:

  • how you are creating / changing / deleting tags
  • how you query for tags and questions by tags
  • how many items you have and if you desperately need performance tricks

You could:

  • map the tags as an entity, with an id, which is reused. There will be a separate class (Tag) and two tables, a Tag table and a QuestionToTag table (and additional XxxToTag table for each Tag owner if there are others).
  • map the tags as a list of strings, which are not reused. You need no special class and thee is one additional Table for each Tag owner (QuestionTags).
  • map the tags as a single string with separated by some character, eg. a comma or a space. There is no class and no table for this.

You can't have tags in a list of strings which are reused for several questions. If they should be reused, they need to be entities.

Every solution I proposed has its advantages and disadvantages. It depends on what is most important for you to say which solution is the best.

Stefan Steinegger