0

I have N-Tier architecture and in service layer I need a way to get only the ids for associated entities or the full entities. So on one session I only need the ids and in other session I may need the full entities.

I have two entities:

public class ParentEntity
{
    public virtual long Id { get; set; }
    public virtual IList<ChildEntity> Children { get; set; }
    public virtual string Name { get; set; }
    // ... other fields
}

public class ChildEntity
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
   // ... other fields
}

I some times I need to load the full ChildEntity (in verification layer) but some times I need only to load the ids for it like this in my service layer:

ParentEntity parent = repository.GetById(someId);
SendChildIds(parent.Children.Select(x => x.Id));

But doing this will load the ChildEntity fully I only want to load the ids like this

SELECT parentchild0_.ParentId    as ParentId0_,
     parentchild0_.ChildId as ChildId0_
FROM   ParentChildEntities parentchild0_
WHERE  parentchild0_.ParentId0_= 447 /* @p0 */

but he do something like this

SELECT pce.ParentId, ce.* FROM ChildEntities ce INNER JOIN ParentChildEntities pce on pce.ChildId = ce.Id WHERE pce.ParentId = 447

I use FluentNHibernate to configure the mappings.

4

1 回答 1

0

您可以使用投影或映射到仅返回上述要求的另一个实体。

IE

ParentEntity {..., IList<ChildEntity> Children}
ChildEntity {...}
ParentEntityLite {...} //mapped w.r.t requirements i.e. no mappings for children 

或通过投影

from parent in _session.Query<ParentEntity>()
select new {parent.Id, ...}; //projection --> parent.Id

from parent in _session.Query<ParentEntity>()
select new ParentEntity(){Id = parent.Id}; // projection --> parent.Id
于 2012-05-08T08:43:06.500 回答