Fluent NHibernate Polymorphism and TransformUsing



我有一个名为"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 上定义这个。投影类并将数据转换为其中。

这是我

对该问题的解决方法。如果有人可以改进它,我将不胜感激。

        DTO.Project proj = new DTO.Project();
        crmUser user = null;
        crmEntity ent =null;
        using (ISession session = Provider.SessionFactory.OpenSession())
        {
            proj = session.QueryOver<crmProject>()
                .Fetch(x => x.assignedToUser).Eager()
                .JoinAlias(x => x.assignedToUser, () => user, JoinType.InnerJoin)
                .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")
  ).TransformUsing(Transformers.AliasToBean<DTO.Project>())
  .SingleOrDefault<DTO.Project>();
            proj.RelatedEntities = session.QueryOver<crmProject>()
                .Fetch(x => x.RelatedEntities).Eager()
                .Where(c => c.ID == projectId).JoinAlias(x=>x.RelatedEntities,()=>ent,JoinType.InnerJoin)
  .Select(Projections.ProjectionList()
        .Add(Projections.Property(()=>ent.ID), "ID")
        .Add(Projections.Property(() => ent.type), "Type")
  ).TransformUsing(Transformers.AliasToBean<DTO.EntityRelation>()).List<DTO.EntityRelation>();
        }
        return proj;

最新更新