views:

116

answers:

1

Hi

I have two tables which looks something like this

Table Queue
 int ID;
 string Name;
 int MessageID;
 string To
 string From
 string Topic
 string Email

Table Message
 int ID
 int MessageType

This is a rather simplified version, but what we did in the classes was to create 1 class named

 class Queue
 int ID
 string Name
 Message message

And then we have an abstract message with either types of message.

 class abstract Message()

 class SMSMessage : Message
 string ToMobile
 string FromMobile
 string Message

 class EmailMessage
 string ToEmail
 string FromEmail
 string Topic
 string Email

However now my problem is figuring out how to map this with fluent Nhibernate. How would i do this ?

+3  A: 

I could rewrite the article, but I think I'll allow it to stand on its own: http://marekblotny.blogspot.com/2009/03/fluent-nhibernate-and-inheritance.html

EDIT:

Running with the spirit of the article, I'd try the following (and I really have no idea if this will work):

Write a Message map:

public class MessageMap : ClassMap<Message> {
  public MessageMap() {
    Id(x=>x.Id).GeneratedBy.Native();
    // only the common stuff

    DiscriminateSubClassesOnColumn<int>("MessageType")
      .SubClass<SMSMessage>(1, m=>m.HasOne( x=>{
                                                 Map(x=>x.ToMobile);
                                                 Map(x=>x.FromMobile);
                                                 // etc
                                               })
                            .Cascade.SaveUpdate()
                            .FetchType.Join())
      .SubClass<EmailMessage>(2, m=>m.HasOne(x=>{
                                                 Map(x=>x.ToEmail);
                                                 Map(x=>x.FromEmail);
                                                 // etc
                                               })
                            .Cascade.SaveUpdate()
                            .FetchType.Join())
  }
}

We'll do SMS only for now ...

public class SMSMessage : Message {
  public virtual string ToMobile { get; set; }
  public virtual string FromMobile { get; set; }
  ...
}

Here's the mapping class...

public class SMSMessageMap : ClassMap<SMSMessage> {
  public SMSMessageMap() {
    WithTable("Queue");
    Id(x=>x.Id, "QueueID").GeneratedBy.Foreign("Message");
    Map(x=>x.ToMobile, "To");
    Map(x=>x.FromMobile, "From");
    WithTable("Message", m=>
    {
      m.Map(x=>x.MessageBody);
    });
  }
}
ddc0660
Hi i just figured out that the type is actually within another table meaning we have something like this:Table Queue int ID; string Name; int MessageID; string To; string From; string Topic; string EmailTable Message int ID int MessageTypeIs there still anyway to map this ?
Morten Schmidt
This wouldnt work, first you are refering to a Details property which doesnt exist, secondly you newer refer to the QueueID which is necessary to get the To and From.
Morten Schmidt
The SMSMessageMap uses the Queue table and the Id(x=>x.Id) would refer to the QueueID. I didn't realize the column was named QueueID as you use "Id" in the question. .. Still not sure it's going to work. :)
ddc0660
I just updated the answer to fix the Details issue as well.
ddc0660