Retrieve Derived SubType from entity EntityCollection

Posts   
 
    
fpw23
User
Posts: 12
Joined: 16-Dec-2005
# Posted on: 03-Jan-2006 17:56:03   

Hi All,

So here is my set up:

BusinessOverdraftEventEntity : OverdraftEventEntity OverDraftEventEntity : AccountEventEntity AccountEventEntity

there is a one to one relationship with the accountevent table and the overdraft event table in the database

I also have a factory class setup like the help file said to handle the creation of my derived BusinessOverdraftEventEntity.

when i run this:


EntityCollection odevents = new EntityCollection(new BusinessOverDraftEventEntityFactory());

IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(PredicateFactory.CompareValue(OverDraftEventFieldIndex.Active, ComparisonOperator.Equal, true));
bucket.PredicateExpression.Add(PredicateFactory.CompareValue(AccountEventFieldIndex.AccountNumber, ComparisonOperator.Equal, this.AccountNumber));
bucket.PredicateExpression.Add(PredicateFactory.CompareValue(AccountEventFieldIndex.SuffixCode, ComparisonOperator.Equal, this.SuffixCode));

da.FetchEntityCollection(odevents, bucket);
                
//if found set local var
if (odevents.Count > 0)
{       
     this._activeOverdraftEvent = (BusinessOverDraftEventEntity)odevents[0]);
     this._activeOverdraftEventRetrieved = true;
}
else //set to null and retrieve true
{
     this._activeOverdraftEvent = null;
     this._activeOverdraftEventRetrieved = true;
}

When I run the line to cast the 0 item to a businessoverdraftevent the i get "cast is invalid". I looked at the odevents[0] type and it shows that the type is overdraftevent. I think the error makes sense since the businessoverdraftevent inheirates from the overdraft event so I can't cast up but how can I convert my overdraft event to a business overdraftevent so I can use my custom methods?

Thanks,

Frank

can1
User
Posts: 77
Joined: 16-Sep-2005
# Posted on: 03-Jan-2006 21:20:42   

I am in a similar situation. We are working through it on this thread:

http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=5053

In any case, if I get it figured out, I will let you know.

Can1

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39620
Joined: 17-Aug-2003
# Posted on: 04-Jan-2006 12:58:41   

fpw23 wrote:

Hi All,

So here is my set up:

BusinessOverdraftEventEntity : OverdraftEventEntity OverDraftEventEntity : AccountEventEntity AccountEventEntity

there is a one to one relationship with the accountevent table and the overdraft event table in the database

I also have a factory class setup like the help file said to handle the creation of my derived BusinessOverdraftEventEntity.

when i run this:


EntityCollection odevents = new EntityCollection(new BusinessOverDraftEventEntityFactory());

IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(PredicateFactory.CompareValue(OverDraftEventFieldIndex.Active, ComparisonOperator.Equal, true));
bucket.PredicateExpression.Add(PredicateFactory.CompareValue(AccountEventFieldIndex.AccountNumber, ComparisonOperator.Equal, this.AccountNumber));
bucket.PredicateExpression.Add(PredicateFactory.CompareValue(AccountEventFieldIndex.SuffixCode, ComparisonOperator.Equal, this.SuffixCode));

da.FetchEntityCollection(odevents, bucket);
                
//if found set local var
if (odevents.Count > 0)
{       
     this._activeOverdraftEvent = (BusinessOverDraftEventEntity)odevents[0]);
     this._activeOverdraftEventRetrieved = true;
}
else //set to null and retrieve true
{
     this._activeOverdraftEvent = null;
     this._activeOverdraftEventRetrieved = true;
}

When I run the line to cast the 0 item to a businessoverdraftevent the i get "cast is invalid". I looked at the odevents[0] type and it shows that the type is overdraftevent. I think the error makes sense since the businessoverdraftevent inheirates from the overdraft event so I can't cast up but how can I convert my overdraft event to a business overdraftevent so I can use my custom methods?

If you save an entity of type OverDraftEventEntity into the database, and later on read it back with the code above, you'll get the data back in an OverDraftEventEntity instance. This is controlled by the InheritanceInfoProvider object which contains inheritance info and can determine based on a row from the database which factory to call to produce the actual instance.

If you go to the InheritanceInfoprovider.cs class in the HelperClasses folder, you'll see an init method and in there also the line for OverDraftEventEntity which states that it uses an OverDraftEventEntityFactory instance.

Now, you can do two things. 1) create a custom template for InheritanceInfoProvider.cs, which produces code which states that it should use BusinessOverDraftEventEntityFactory instead of OverDraftEventEntityFactory. This can be a bit problematic: you probably should store in a custom property which entities will be used as business components and which ones aren't. If you're using solely leaf entities as business components (so if it has subtypes it's not a business component), you can determine with an <[if Not IsSuperType]>...<[EndIf]> if an entity is a leaf and then emit 'Business' there, so the full line becomes:


<[If IsInHierarchyType TargetPerEntity]>            base.AddEntityInfo("<[CurrentEntityName]>Entity", <[If IsSubType]>"<[SuperTypeName]>Entity"<[Else]>string.Empty<[EndIf]>, new <[CurrentEntityName]>Relations(), new <[If Not IsSuperType]>Business<[EndIf]><[CurrentEntityName]>EntityFactory()<[If IsSubType]>, <[Foreach FieldToLink Comma]> (<[SubTypeFieldIndex]>-<[IndexFirstNonInheritedField]>)<[NextForeach]><[EndIf]>);<[Else]><[If IsInHierarchyType TargetPerEntityHierarchy]>           base.AddEntityInfo("<[CurrentEntityName]>Entity", <[If IsSubType]>"<[SuperTypeName]>Entity"<[Else]>string.Empty<[EndIf]>, new <[If Not IsSuperType]>Business<[EndIf]><[CurrentEntityName]>EntityFactory(), (int)<[CurrentEntityName]>FieldIndex.<[DiscriminatorColumnName]>, <[DiscriminatorValue]>);<[EndIf]><[EndIf]><[ EndIf ]><[ NextForeach ]>

(for your custom inheritanceInfoProvider.template )

2) you can also add code to your own BusinessOverDraftEventEntityFactory implementation. In the method GetEntityFactory you now have the line: return (IEntityFactory2)InheritanceInfoProviderSingleton.GetInstance().GetEntityFactory("OverdraftEventEntity", fieldValues, entityFieldStartIndexesPerEntity); (or similar).

Instead of keeping that line, simply check if the returned factory is indeed OverdraftEventEntityFactory, and if so, return 'this' instead of what's returned by the inheritanceinfoprovider.

I think you should start with option 2) simple_smile

@can1: no this is a different problem, not related to yours.

Frans Bouma | Lead developer LLBLGen Pro