Load list of root objects and show related child objects

Posts   
 
    
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 25-Apr-2007 10:59:43   

LLBLGen v.2.0.0.0 / .NET 2 / Adapter

Hi,

I have the following class model:

Person --> Employee Person --> Customer

So Employee and Customer extend the Person class with additional fields.

Now I want to show a list of all Person objects including their type (Employee or Customer). It should also be possible to filter by Employees or Customers.

How can I do that? disappointed

From my point of view, the GetDependingRelatedEntities() method should actually return a list of child objects but the collection is always empty. That's what I've tried so far:


EntityCollection<PersonEntity> persons = new EntityCollection<PersonEntity>(new PersonEntityFactory());
mAdapter.FetchEntityCollection(persons, null);

foreach (PersonEntity person in persons) {
      List<IEntity2> list = person.GetDependingRelatedEntities();
      if (list.Count > 0) 
            Response.Write(person.FirstName); // --> This code line is never reached!!!!
}


I guess I have to specify a prefetch path to load the depending entities. How can I do that?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Apr-2007 11:08:51   

Are these refrential relations or inheritance relations? If they are refrential relations, then PrefetchPaths would do the trick.

KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 25-Apr-2007 11:12:38   

Inheritance relations.. Person is the super class, Employee and Customer are sub classes.

Yeah it would be easy with references.. wink

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Apr-2007 11:27:12   

If it's an inheritance relation as in LLBLGen Pro inheritance, then fetching Persons will fetch all entities that is of type Persons including other subType entities, and you can filter by type. Please refer to the title Filtering on entity type in the manual: "Using the generated code -> Adapter/SelfServicing -> Filtering and sorting -> Advanced filter usage"

KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 25-Apr-2007 11:41:40   

Ok. Got it! smile That's easier than I thought!

With this code I can get the correct type and do a cast.


            foreach (PersonEntity person in persons) {
                string entityType = person.GetInheritanceInfo().OwnerEntityName;

                if (entityType == "EmployeeEntity") {
                    EmployeeEntity test = (EmployeeEntity) person;
                    Response.Write("Employee: " + test.FullName);
                }
            }

Thanks for your help!

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 25-Apr-2007 12:22:41   

KIT wrote:

Ok. Got it! smile That's easier than I thought!

With this code I can get the correct type and do a cast.


            foreach (PersonEntity person in persons) {
                string entityType = person.GetInheritanceInfo().OwnerEntityName;

                if (entityType == "EmployeeEntity") {
                    EmployeeEntity test = (EmployeeEntity) person;
                    Response.Write("Employee: " + test.FullName);
                }
            }

Thanks for your help!

Not tested but would this simplify things even further?


            foreach (PersonEntity person in persons) {
                EmployeeEntity employee = person as EmployeeEntity

                if (employee != null) {
                    Response.Write("Employee: " + employee.FullName);
                }
            }

KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 25-Apr-2007 13:05:36   

Yes that's even better code! I didn't know the "as" operator before.. flushed

Thanks! simple_smile