I am trying to create a messaging system between users and organizations - that is, a user can send and recieve messages from users or organizations.
So, to support this system, I started with an interface:
public interface IPassableObject<F, T, O>
where F : DomainObject
where T : DomainObject
{
Guid PassedItemId { get; set; }
O Item { get; set; }
F From { get; set; }
T To { get; set; }
DateTime Created { get; set; }
}
The idea is later I'd like to be able to pass more than just messages - like serialized objects. So, we have a class with 3 Generic types. The To, the From, and the object
Then I created my Message class
public class Message<T, F> : DomainObject, IPassableObject<T, F, string>
where F : DomainObject
where T : DomainObject
{
public Message()
{
Created = DateTime.Now;
}
#region IPassableObject<T,F> Members
public virtual Guid PassedItemId { get; set; }
public virtual string Item { get; set; }
public virtual T From { get; set; }
public virtual F To { get; set; }
public virtual DateTime Created { get; set; }
#endregion
}
Then, I made my table to support my passed items:
CREATE TABLE dbo.PassedItems
(
PassedItemId uniqueidentifier NOT NULL,
FromId uniqueidentifier NOT NULL,
ToId uniqueidentifier NOT NULL,
SerializedObject varchar(max) NOT NULL,
Created datetime DEFAULT(GETEDATE()) NOT NULL,
CONSTRAINT PK_PassedItems PRIMARY KEY CLUSTERED (PassedItemId)
)
The ID's for both my users and organizations are guids. Now, I have my fluent mapping for The User - User messages. My thought is that I will only have to create 4 mappings and not maintain 4 seperate classes for user-user, user-org, org-user, and org-org. I was hoping that Nhibernate would take the guid in the table and automatically try and pull in the object based on the type specified.
public class UserToUserMessageMap : ClassMap<Message<UserProfile, UserProfile>>
{
public UserToUserMessageMap()
{
WithTable("PassedItems");
Id(x => x.PassedItemId, "PassedItemId").GeneratedBy.Guid();
Map(x => x.Item, "SerializedObject");
Map(x => x.Created);
References(x => x.From, "FromId");
References(x => x.To, "ToId");
}
}
The issue I am seeing is that when I run my application, I get a runtime error:
NHibernate.MappingException: An association from the table PassedItems does not specify the referenced entity
So, will this approach even work? What am I doing wrong? Thanks!