views:

28

answers:

1

Hello,

I need a little help to map the following structure using Fluent NHibernate. The logic behind my classes is to have a question that contains a list of choices and one answer. Each answer contains a sub-list of the choices contained by the question. I hope the code below is more clear

I've minimized the code for each class to contain only the data relevant to this question

class Question {
    public virtual int Id { get; set; }
    public virtual IList<Choice> Choices { get; set; }
    public virtual Answer Answer { get; set; }
}

class Choice {
    public virtual int Id { get; set; }
    public virtual Question Question { get; set; }
}

class Answer {
    public virtual int Id { get; set; }
    public virtual IList<Choice> Choices { get; set; }
}

The issue here is how to map the list of choices for the Answer class, so that a choice in the answer will reference the same choice in the question

I'm opened also to suggestions to even change the class structure to achieve the same goal, if you guys have a better idea

Thanks in advance

A: 

I believe your classes will Automap exactly as you've designed them.

Then, you can just populate your Choices lists, with the same Choice in both of them where desired, and everything should just work.

Edit: Go to this link for Automapping documentation.

Tom Bushell
I understand how to save. The problem is more at loading. For example, let's say I have a Choice that I've added to both the Question and the Answer lists. I persist them. Later, I load the Question and the Answer. Here, I would expect the Choice in both lists to be the same object. This is the scenario I'm looking for (if it is possible, of course). Can you please write how the map class for Answer looks like?
Gicanu
I don't think you need any map classes at all. If you use the Automapping functionality that's part of FNH (I've added a link to my answer), it will map everything purely based on your classes. It will also load them back into memory exactly as they were saved.
Tom Bushell
You are right, I've just tried it and it works.I thought it should be something more complicated, I never thought it's that easy.Thanks.
Gicanu
Glad it worked for you. I have a dream - to never write another line of ADO.Net or SQL code ever again. I'm sure my dream will not be realized, but with FNH I can come close!
Tom Bushell