Inheritance, casting and type changing

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Mar-2006 06:16:23   

Using SelfService and Target-Per-Entity

Given a simple relationship like this:

Person (NOT ABSTRACT) ------Employee ------Customer

if you have that setup using Target-Per-Entity how do you do the following:

1) Determine what kind of entity it is. If you GetMulti into a collection, to determine what kind of object it is are we just supposed to use the 'is' operator? Like: if (myEntity is Employee) { (myEntity as Employee).Salary=1000; }

2) If you have an entity of a given type, what is the best way to get it to the other type? For example, suppose you have a PersonEntity that was neither an Employee or a Customer, just a Person. They then went to work for you, so now they are an employee. What is the best way to make that Person an Employee?

Perhaps something like this? (I am just thinking there is a more elegant way)

PersonEntity person = new PersonEntity(12);
//now turn them into an employee. 
EmployeeEntity employee= new EmployeeEntity();
employee.PersonPKey = person.PersonPKey;

At the database level, we just add a record to Employee table with the PKey of Person. However, unsure how to best do that with your objects.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Mar-2006 08:06:02   

1) Determine what kind of entity it is. If you GetMulti into a collection, to determine what kind of object it is are we just supposed to use the 'is' operator? Like: if (myEntity is Employee) { (myEntity as Employee).Salary=1000; }

I think the "is" operator will work. You may find the following thread relative to this question: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5053

Also please check the LLBLGen Pro documentation and read the section Filtering on entity type Under "Using the generated code - Adapter/Selfservicing - Advanced filter usage"

2) If you have an entity of a given type, what is the best way to get it to the other type? For example, suppose you have a PersonEntity that was neither an Employee or a Customer, just a Person. They then went to work for you, so now they are an employee. What is the best way to make that Person an Employee?

The answer is here: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=4778