流利的NHibernate多态性和TransformUsing

时间:2012-04-18 11:12:55

标签: nhibernate fluent-nhibernate polymorphism transformation wcf-rest

我有一个名为“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>();
        }

但是你可以看到我需要在这里添加RelatedEntities,但我不知道怎么做,因为RelatedEntity可以是“Company”,“Contact”或“Project”,它继承了Entity类。我需要在DTO.Project类上定义它并将数据转换为它。

1 个答案:

答案 0 :(得分:0)

这是我对这个问题的解决方法。如果有人能改进它,我将不胜感激。

        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;