我有一个名为“Entity”的基类和名为“Project”、“Company”、“Contact”的子类,它们继承了基类“Entity”并在我的 WCF REST 应用程序上使用 Fluent NHibernate 进行了映射。
以前,我的实体类不需要这种多态关联,但最近我需要将这些类与多对多关系相关联,所以我决定这样做。这是我的基类的映射:
public crmEntityMap()
{
Table("crmEntity");
LazyLoad();
Id(x => x.ID).GeneratedBy.Identity().Column("ID");
Map(x => x.instanceID).Not.Nullable().Column("InstanceID");
Map(x => x.comment).Column("Comment").Length(500);
HasManyToMany(x => x.RelatedEntities)
.AsList(i => i.Column("`Index`"))
.ParentKeyColumn("ParentID")
.ChildKeyColumn("ChildID")
.BatchSize(100)
.Not
.LazyLoad()
.Fetch.Join()
.Cascade.None();
DiscriminateSubClassesOnColumn("Type");
}
项目映射:
public class crmProjectMap : SubclassMap<crmProject>
{
public crmProjectMap() {
Table("crmProjects");
LazyLoad();
Map(x => x.name).Column("Name").Length(50);
Map(x => x.initialDate).Column("InitialDate");
Map(x => x.deadline).Column("Deadline");
Map(x => x.isClosed).Column("IsClosed");
References(x => x.assignedToUser).Column("AssignedToUserID").NotFound.Ignore();
}
}
我的 WCF REST 服务上的序列化转换代码:
public static DTO.Project GetProject(int projectId, int instanceId)
{
crmUser user = null;
return Provider.GetSession().QueryOver<crmProject>()
.Fetch(x => x.assignedToUser).Eager()
.JoinAlias(x => x.assignedToUser, () => user, JoinType.LeftOuterJoin)
.Where(c => c.ID == projectId)
.And(c => c.instanceID == instanceId)
.Select(Projections.ProjectionList()
.Add(Projections.Property("ID"), "ID")
.Add(Projections.Property("instanceID"), "instanceID")
.Add(Projections.Property("name"), "name")
.Add(Projections.Property("comment"), "comment")
.Add(Projections.Property("isClosed"), "isClosed")
.Add(Projections.Property("initialDate"), "initialDate")
.Add(Projections.Property("deadline"), "deadline")
.Add(Projections.Property(() => user.userID), "assignedToUserID")
//Add Related Entities?
).TransformUsing(Transformers.AliasToBean<DTO.Project>())
.SingleOrDefault<DTO.Project>();
}
但正如您所看到的,我需要在此处添加相关实体,但我不知道该怎么做,因为相关实体可以是继承实体类的“公司”、“联系人”或“项目”。我需要在 DTO.Project 类上定义它并将数据转换为它。