Our system generates emails with user-chosen lists of To / CC / BCC contacts. I wanted to store them as follows in our SQL Server database, here's the 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 looks like a 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) such that I could enable the following code to work :
testEmail.ToContacts.Add(contact1)
testEmail.CCContacts.Add(contact2)
testEmail.BCCContacts.Add(contact3)
Can this be done without adding an additional domain object (EmailContact)? Do I need to use two many-to-one relationships with an additional domain object like Billy McCafferty mentions here?
Also, how would I represent this my NHibernate mapping file?