views:

91

answers:

1

I Have two classes, Survey and Poll classes. Also I have Question and Question Choice classes. How do I map these so I come out with particular table formats. here is the classes involved.

public class Survey
{
     public IList<Question> Questions { get; private set; }   
}

public class Poll
{
    public Question Question { get; set; }
}

public class Question
{
    public string Text { get; set; }
    public IList<QuestionChocie> Choices { get; private set; }
}

public class QuestionChoice
{
    public string Text { get; set; }
}

The resulting tables that I'm shooting for include the following

Surveys- a table of survey information.
Polls - a table of polls information.
SurveyQuestions -a table of survey questions.
PollQuestions - a table of poll questions.
SurveyChoices - a table of the question choices for the surveys.
PollChoices - a table of the question choices for the survey.

preferably i really want to know for fluent nhibernate, or just mapping xml is fine too.

A: 

You haven't defined relationships between the tables so I'm going to assume one-to-many.

The general mapping would be:

public class SurveyMap : ClassMap<Survey>
{
    public SurveyMap()
    {
        HasMany<SurveyQuestion>(x => x.Questions).Inverse();
        // Rest of mapping
    }
}

public class SurveyQuestionMap : ClassMap<Question>
{
    public QuestionMap()
    {
        References<Survey>(x => x.Survey);
        HasMany<SurveyChoice>(x => x.Choices).Inverse();
        // Rest of mapping
    }
}

public class SurveyChoiceMap : ClassMap<SurveyChoice>
{
    public SurveyChoiceMap()
    {
        References<SurveyQuestion>(x => x.Question);
        // Rest of mapping
    }
}
Stuart Childs
Stuart, I think you can drop the type names between <...>. They should be inferred unless I'm missing something.
TheDeeno
You're right, you can leave the types off. It's just my preference to leave it in so that it's clear what I'm mapping. I like to read it as "Has many X through Y" or "References X through Z."
Stuart Childs