Mapping a class with multiple collection properties containing the same object type

Our system generates emails with user-selected To / CC / BCC contact lists. I wanted to store them like this in our SQL Server database, here's a simplified database table structure:

CREATE TABLE [Contact] (
    [ContactID] [int] IDENTITY (1, 1) NOT NULL,
    [Name] [varchar] (100) NOT NULL,
    [EmailAddress] [varchar] (100) NOT NULL,
    CONSTRAINT [PK_Contact] PRIMARY KEY CLUSTERED ([ContactID])
)

CREATE TABLE [Email] (
    [EmailID] [int] IDENTITY (1, 1) NOT NULL,
    [Subject] [varchar] (500) NOT NULL,
    [Message] [text] NULL,
    [DateSent] [datetime] NOT NULL,
    CONSTRAINT [PK_Email] PRIMARY KEY CLUSTERED ([EmailID])
)

CREATE TABLE [EmailContact] (
    [EmailID] [int] NOT NULL,
    [ContactID] [int] NOT NULL,
    [Type] [varchar] (4) NOT NULL,
    CONSTRAINT [PK_EmailContactList] PRIMARY KEY CLUSTERED 
    (
        [EmailID],
        [ContactID],
        [Type]
    ),
    CONSTRAINT [FK_EmailContact_Contact] FOREIGN KEY ([ContactID]) REFERENCES [Contact] ([ContactID]),
    CONSTRAINT [FK_EmailContact_Email] FOREIGN KEY ([EmailID]) REFERENCES [Email] ([EmailID])
)

      

This to me is similar to the case of a many-to-many relationship between the Email and Contact objects. However, I would like the email domain object to have 3 separate IList properties for the contacts of each list (To / CC / BCC) so that I can include the following code to work:

testEmail.ToContacts.Add(contact1)
testEmail.CCContacts.Add(contact2)
testEmail.BCCContacts.Add(contact3)

      

Can I do this without adding an additional domain object (EmailContact)? Do I need to use two multivalued relationships with an additional domain object like Billy McCafferty mentioned here ?

Also how can I represent this NHibernate mapping file?

+1


source to share


1 answer


According to the article you are citing Billy McCafferty:

At first glance, this might mean that you will need to create a CustomerAddress object in your domain model to reflect this relationship table in the database; this is not the case with NHibernate. Instead, you would simply add a many-to-many association to Customer.hbm.xml, which indicates that the CustomerAddresses table should be used as a search for managing many-to-many relationships.



Replace the "CustomerAddresses" McCafferty example with your "EmailContacts". According to this article, you need the EmailContacts table to map many-to-many relationships between messages and contacts, but not a domain object EmailContacts

. If you need three separate ratios for the To, CC, and the BCC, create three tables connections: To_EmailAddresses

, CC_EmailAddresses

and BCC_EmailAddresses

.

I'm not completely familiar with NHibernate, so I don't know exactly how to update the mapping file, but the article states that the file related to your email domain (Email.hbm.xml) will be one to update. The NHibernate documentation should tell you how to represent many-to-many relationships in a mapping file.

0


source







All Articles