Working with derived entities

Posts   
 
    
philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 06-Sep-2007 13:51:01   

Hi there

I have a few entity classes which I would like to extend in order to use them in a rather specific scenario (which is why I want to extend the entity class rather than the entity itself).

public partial class MyEntity : CommonEntityBase, ISerializable
{
  // generated code
}



public class MySpecificEntity : MyEntity
{
  //additional properties and methods
  //none of them are stored in the database
}

Is it possible to instruct Adapter to instantiate objects of type MySpecificEntity when fetching data (also for 1:n scenarios where I would need a collection of _MySpecificEntity _instances), or does the designer provide functionality that covers this scenario? This would be **very **convenient.

Thanks for your advice Philipp

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Sep-2007 15:05:30   

When fetching an entity, the adapter accepts an object of IEntity2. So you can just pass an instance of MySpecificEntity:

MySpecificEntity myEntity = new MySpecificEntity(PK_VALUE);
adapter.FetchEntity(myEntity);

Also try the following for fetching an EntityCollection:

EntityCollection<MySpecificEntity> mySpecificEntityCollection = new EntityCollection<MySpecificEntity>();
adpater.FetchEntityCollection(null);
philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 06-Sep-2007 17:47:54   

Walaa,

Thanks for the fast reply! One question remains regarding collections: Lets say I have an 1:n collection between _ParentEntity _and ChildEntity, and my additional class extends _ChildEntity _(let's say it's called SpecificChild.

To get my regular entities, I'll just fetch them like this:


//get the child group collection
EntityCollection<ChildEntity> childs= parentGroup.Childs;
adapter.FetchEntityCollection(childs, parentGroup.GetRelationInfoChilds());

Is there a way to populate parentGroup.Childs with _SpecificChild _ instances directly or do I have to create a collection of _SpecificChild _instances with one call (as in your sample) and adding them to the Childs-collection manually?

Edit: Regarding your sample, the docs say that the empty construtor of EntityCollection should not be used - submitting an IEntityFactory is recommended. However, could this be a solution (injecting a SpecificChild factory into the parent)?

Thanks again Philipp

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Sep-2007 10:14:39   

Try the following please:

EntityCollection<SpecificChildEntity> childs = new EntityCollection<SpecificChildEntity>();
adapter.FetchEntityCollection(childs, parentGroup.GetRelationInfoChilds());
philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 10-Sep-2007 10:09:00   

Hi Walaa

This is the code I used before. However, this leaves me with an independent collection - in order to populate the collection of the parent entity, I would have to manually copy the retrieved instances from my retrieved collection to the Childs-collection of the parent, which causes quite an overhead. However, I can live with it. Thanks for the support! simple_smile

Walaa wrote:

Try the following please:

EntityCollection<SpecificChildEntity> childs = new EntityCollection<SpecificChildEntity>();
adapter.FetchEntityCollection(childs, parentGroup.GetRelationInfoChilds());