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.

1 Answers1

0

You could either use projections or map to another entity which would only return the said requirements.

i.e.

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

or via projections

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
kalki
  • 516
  • 2
  • 10
  • Thanks kalki for you answer, but I want to avoid additional classes and projections can not be used from service layer. – Emanuel Dejanu May 08 '12 at 09:09
  • Well your repository has to be extended to permit this in your service layer, i'm assuming repository here is a wrapper for the session – kalki May 08 '12 at 09:39
  • kalki the repository's GetById returns ParentEntity. How can I use projections on an returned entity? I agree that on repository's Search method I can return IQueryble and use projects. – Emanuel Dejanu May 08 '12 at 14:01
  • Well if GetById returns parent entity and if a session is still active & children are mapped lazily, you could still project by manually assigning the entities properties to a dto by a manual conversion i.e. dto.Property = entity.Property, but this would load the entire entity sans the children which although better is still not as efficient as the projection w.r.t a query – kalki May 08 '12 at 14:43
  • you could make individual properties lazy as well, but would result in a remote call specific for the property. Link : http://ayende.com/blog/4377/nhibernate-new-feature-lazy-properties – kalki May 08 '12 at 15:01