Common with Fluent and NHibernate
I am trying to create a messaging system between users and organizations, i.e. a user can send and receive messages from users or organizations.
So, to support this system, I started with the 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 that later on I would like to transmit more than just messages - for example, serialized objects. So we have a class with 3 Generic types. To, From and Object
Then I created a 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 a 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 for my users and organizations is sheets. I now have a clear mapping for User-User posts. My thought is that I would only need to create 4 mappings and not maintain 4 separate classes for users, users, organizations, org-users and org-org. I was hoping Nhibernate would take the manual in the table and automatically try to pull 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 problem I see is that when I start the application, I get a runtime error:
NHibernate.MappingException: Association from PassedItems table does not specify a referenced object
So, will this approach work? What am I doing wrong? Thank!
source to share
I found a solution to my problem. To give a rough overview of what I've done:
I had to dump generics.
I have my interface, IPassable.
I have an abstract class that implements IPassable: PassableBase
I have a PassableItem class "dummy" that inherits from PassableBase. This class does nothing outside of the defaults set in PassableBase.
Message is a class that also inherits from PassableBase. Other passed objects must inherit from PassableBase.
My translation of Fluent uses PassableItem.
My repos use PassableBase for props and I create a new PassableItem or get a PassableItem from the store (depending on whether it was created already or not) and then do my save / delete on the PassableItem object.
source to share
So what does the display look like for "From" and "To"?
As I think NH cannot match these entities because you did not provide mapping information.
In general, it seems to me that you are reinventing the wheel, you were looking at technology stacks like web services (SOAP and REST), message queue, remote connection (.Net). And concrete implementations like WCF, ASMX (web services), nServiceBus, MassTransit, MSMQ are just a few ...
Ollie
source to share