I've got a situation where I have two tables that basically hold similar entities (but not identical). For business reasons they need to be separate. I want to map them to the same entity if possible although right now I have a wrapper class that just has two sets, one for each entity type.
I can modify the OtherSite table as I wish, but the CTRSite table is outside of my control and in another database.
I'm finding this relationship I've got hard to describe so here is some code:
Code:
public class User : PersistentObjectWithTypedId<Guid>
{
// Most of the stuff removed for simplicity sake
public virtual string Name {get; set;}
public virtual string UserName {get; set;}
[b]public virtual IList<WorksiteHistory> WorksiteHistories { get; set;} [/b]
}
Code:
public class WorksiteHistory : PersistentObjectWithTypedId<Guid>
{
public virtual Boolan Active {get; set;}
public virtual DateTime DateCreated {get; set;}
public virtual User User {get; set;}
// The actual site that may be one of two different entity types (and in different tables)
[b]public virtual ISite Site {get; set;}[/b]
}
Table:
Code:
CREATE TABLE [dbo].[wsdot_WorksiteHistory](
[WorksiteHistoryId] [uniqueidentifier] NOT NULL,
[CTRSiteId] [int] NULL,
[UserId] [uniqueidentifier] NOT NULL,
[Active] [bit] NOT NULL,
[DateCreated] [datetime] NOT NULL,
[OtherSiteId] [uniqueidentifier] NULL,
CONSTRAINT [PK_wsdot_WorksiteHistory] PRIMARY KEY CLUSTERED
)
Mapping file
This does not work quite like I want!:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="WSDOT.Core" namespace="WSDOT.Core.Domain">
<class name="WorksiteHistory" table="wsdot_WorksiteHistory">
<id name="ID" type="Guid" column="WorksiteHistoryId">
<generator class="guid.comb" />
</id>
<property name="Active" column="Active" type="Boolean" />
<property name="DateCreated" column="DateCreated" type="DateTime" />
<many-to-one name="User" column="UserId" class="User" />
<!--<many-to-one name="Worksite" column="WorksiteId" class="Worksite" />-->
<any name="Site" meta-type="string" id-type="System.Guid" >
<meta-value value="CTR" class="CTRSite"/>
<meta-value value="Other" class="OtherSite"/>
<column name="CTRSiteId"/>
<column name="OtherSiteId"/>
</any>
</class>
</hibernate-mapping>
Code:
public interface ISite
{
string Name { get; set;}
string Address { get; set; }
string City { get; set; }
string State { get; set; }
string Zip { get; set; }
bool Active { get; set; }
object ID { get; }
SiteType Type { get; }
}
I then have two entities (one table each) that implement ISite:
Code:
public class OtherSite : PersistentObjectWithTypedId<Guid>, ISite
{
public virtual string Name { get; set; }
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zip { get; set; }
public virtual bool Active { get; set; }
public virtual string Owner { get; set; }
public object ID
{
get { return base.ID; }
}
public SiteType Type
{
get { return SiteType.Other; }
}
}
Note: CTRSite and OtherSite have different Primary Key types. I can modify them to be the same.
Code:
// Primary key is of Int type
public class CTRSite : PersistentObject, ISite
{
public virtual string Name { get; set; }
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zip { get; set; }
public virtual bool Active { get; set; }
public object ID
{
get { return base.ID; }
}
public SiteType Type
{
get { return SiteType.CTR; }
}
public virtual Organization Organization { get; set; }
public virtual IList<Project> Projects { get; set; }
I'm kind of stuck. I don't even know if I'm using the right approach. Thoughts and/or suggestions?
Thanks
Jack