Reputation: 642
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 ?
Upvotes: 0
Views: 180
Reputation: 4052
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);
});
}
}
Upvotes: 4