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.