Best way to load related properties

Posts   
 
    
Orion
User
Posts: 4
Joined: 18-Dec-2009
# Posted on: 12-Jan-2010 06:15:40   

Let's say I have a HospitalEntity and it has a 1:m relationship with LocationEntity. Obviously, a HospitalEntity has many other properties that are inherent to that entity (HospitalName, etc).

I looked in the manual and found information about "Prefetch Paths" but I'm not necessarily trying to prefetch them. I have an ASP.NET MVC app and I am using a service layer to actually run the LINQ queries but I am passing regular LLBLGen Entities around back to my MVC controllers (in other words, currently I don't use any kind of DTO, just LLBLGen Entities).

I thought by running this code:

                
   LinqMetaData metaData = new LinqMetaData(adapter);

     HospitalEntity hosp = 
          (from h in metaData.Hospital
          where h.HospitalId = 42
          orderby h.Name
          select c).SingleOrDefault()


I could then run some code like this:


     foreach(RoomEntity r in hosp.Rooms)
     { ... }

I thought this would cause the hosp.Rooms collection to be populated but it's not.

I use the WithPath extensions and was able to populate this collection.

However, is this the best way? I need to have something kind of flexible. My issue is that, as I said, I have a service layer, so I have a class, say ServiceLayer that has methods that perform the LINQ queries as well as create/update/delete operations and are called by the Controllers.

So I have a function in the ServiceLayer class like:

public HospitalEntity GetHospital(long id)

It would be nice if the controller could simply call properties on the returned object to get access to the related entities. I definitely don't want my LINQ queries to occur anywhere other than in my ServiceLayer.

What I've done for now is modify my GetHospital() type methods to go ahead and use the WithPath for whatever relation I need.

This is not optimal as not every call to GetHospital() will require these relations. However, I don't want to have a bunch of methods like GetHospital(), GetHospitalWithRooms(), GetHospitalWithDoctors(), etc.

What's the best way to do this? Can my GetHospital() style methods accept some kind of field list and my LINQ queries be given WithPaths dynamically or something similar?

The only thing is I have to use LINQ. I can't really use the old style query API (which I realize can accept these kinds of lists one way or another).

Anyway, thanks for any help.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Jan-2010 07:03:22   

When using WithPath and PathEdges you can pass LLBLGen IPrefetchPath elements to the ctor of the PatchEdge. See this snippet: http://www.llblgening.com/archive/2009/10/prefetchpaths-in-depth/#casea . Look at the Code (LINQ2LLBL – PathEdges variant) snippet. This way you can write a method overload that accepts and¿ IPrefetchPath(2) and pass it to your PathEdge.

David Elizondo | LLBLGen Support Team
Orion
User
Posts: 4
Joined: 18-Dec-2009
# Posted on: 12-Jan-2010 20:28:30   

daelmo wrote:

When using WithPath and PathEdges you can pass LLBLGen IPrefetchPath elements to the ctor of the PatchEdge. See this snippet: http://www.llblgening.com/archive/2009/10/prefetchpaths-in-depth/#casea . Look at the Code (LINQ2LLBL – PathEdges variant) snippet. This way you can write a method overload that accepts and¿ IPrefetchPath(2) and pass it to your PathEdge.

That's interesting. I checked out the link. Could you, perhaps, provide another example of how to use this syntactically, say with the example I had?

Are you saying I'd create an IPrefetchPath2 object, add all the properties I want to "fill," and then in my ServiceLayer I could do something like add a .WithPath(myIPrefetchPath2Variable) or something similar? Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 13-Jan-2010 10:36:42   

IMHO, you should pass an array of PathEdges, as follows (code not tested, but should show the concept).

        public void GetHospitals(List<IPathEdge> pathsToFetch)
        {
            var meta = new Linq.LinqMetaData(new DataAccessAdapter());
            var q = (from h in meta.Hospital
                     select h).WithPath(pathsToFetch.ToArray());            
        }