views:

19

answers:

1

I'm developing a question/answer based application

I have a Post table in the db along the lines of:

  • ID
  • Title
  • Body
  • DateCreated
  • AuthorID
  • AuthorName (in case user not registered)
  • AutherEmail (as above)
  • PostType (enum - 1 if question, 2 if reply)
  • Show (bit field)

Then, there is "PostBase" - which is an abstract class (this has properties common to both Question and Answer - listed below) I then have an internal "Post" class, which descends from PostBase, and therefore has all the properties listed above.

There is then have a Question class, and an Answer class.

Both use Post as a base class.

Here are the classes:

public abstract class PostBase
{
    {
        get { return postCreatorEmail; }
        //todo: add email address validation here?
        set { postCreatorEmail = value; }
    }        private IList<Attachment> attachments = new List<Attachment>();

    public PostBase()
    {
       //init logic here
    }

    public virtual DateTime DateCreated { get; set; }

    public virtual string ID { get; set; }

    public virtual string Body { get; set; }

    public virtual DateTime DateLastModified { get; set; }

    public virtual string PostCreatorName { get; set; }

    public virtual string PostCreatorEmail { get; set; }

    public virtual int PostCreatorID { get; set; }

    public virtual PostType PostType { get; set; }

    public virtual PostSource PostSource { get; set; }

    public virtual bool Show { get; set; }

    public IEnumerable<Attachment> Attachments { get { return attachments; } }
}

Post

internal class Post : PostBase
{
    public virtual List<string> Tags { get; set; }

    public virtual string Title { get; set; }

    public virtual string ParentPostID { get; set; }
}

Question:

public class Question : PostBase
{
    public Question()
    {
        tags = new List<string>();
    }

    public string Title { get; set; }
    public List<string> Tags { get { return tags; } }

    public int NumberOfReplies { get; set; }
}

Answer:

public class Answer : PostBase
{
    public string QuestionID { get; set; }
}

I use automapper to map to / from Answer and Post or Question and Post

What I'm trying to do, is create the following class:

public class QuestionWithAnswers
{
    public Question Question { get; set; }
    public IEnumerable<Answer> Answers { get; set; }
}

Which basically has a "Question" - remember this is just a Post in the db, with a ParentPostID of 0, with a list of Answers (where ParentPostID is equal to Question.ID ... or along those lines)

My question is - how do I map this in fluent nHibernate?

+1  A: 

You could use Automapper for QuestionWithAnswers, but Answers has to have an IList and not an IEnumerable:

public class QuestionWithAnswers
{
    public virtual Question Question { get; set; }
    public virtual IList<Answer> Answers { get; set; }
}

What I don't understand is the existence of QuestionWithAnswers, when the following (I think) should do:

public class Question : PostBase
{
    public virtual string Title { get; set; }
    public virtual List<string> Tags { get { return tags; } }
    public virtual int NumberOfReplies { get; set; }
    public virtual IList<Answer> Answers { get; set; }
}

If the question has answers, you'll get them, and if it does not, you don't.

Rafael Belliard