Migration Issue GetRelationsForField

Posts   
 
    
Posts: 56
Joined: 08-Jun-2010
# Posted on: 21-Sep-2010 18:44:33   

Hello

I have a set of custom templates that generates an additional project which calls the llblgen generated code. It adds extra common features we use a lot. We are using v3 Adapter.

I wrote these templates originally in v2 and in parts they call the method GetRelationsForFields("....") to get back relations when building a query. eg

UserPermissionEntity.GetRelationsForField("Permission")

In version 3 GetRelationsForField changed from public to internal. Up until now I've gotten around this by changing your templates back to public each time I update llblgen but I'm fed up of that now and want to do it right.

I know I should be generating code like this:

UserPermissionEntity.Relations.PermissionEntityUsingPermissionId;

However the naming of these properties is pretty complicated. I've looked into the .template files which generate the properties in the first place but my templates are .lpt and I'm really struggling. I've been looking through your included .lpt's and I've not found any examples.

I've got as far as

"UserPermissionEntity.Relations.PermissionEntityUsing"

Do you have an example of how to do this in .lpt? ideally without having to call out to a helper dll?

Thanks ~Brett

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 21-Sep-2010 21:41:21   

Can you show us what your template looks like at the moment ? And an example of the code that you are trying to generate...?

Thanks

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 22-Sep-2010 11:12:53   

Cast to IEntityCore and use GetRelationsForFieldOfType

we moved a lot of code to explicit implementations of the IEntityCore or IEntity2 interfaces to avoid intellisense showing a lot of methods/properties.

So your code becomes: ((IEntityCore)UserPermissionEntity).GetRelationsForFieldOfType("Permission")

Frans Bouma | Lead developer LLBLGen Pro
Posts: 56
Joined: 08-Jun-2010
# Posted on: 22-Sep-2010 12:59:49   

Hi Franz

Good idea, but I was calling it as a static. I don't have any instances at the point this was called.

Any other suggestions?

~Brett

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 22-Sep-2010 15:21:41   

Good idea, but I was calling it as a static. I don't have any instances at the point this was called.

What instance(s) are you talking about?

Posts: 56
Joined: 08-Jun-2010
# Posted on: 22-Sep-2010 16:51:29   

Hi

In My original code I was generating this line:

UserPermissionEntity.GetRelationsForField("Permission")

where GetRelationsForFeild is a static method on the UserPermissionEntity class.

In Franz's Sugestion:

((IEntityCore)UserPermissionEntity).GetRelationsForFieldOfType("Permission")

UserPermissionEntity is a class (not an object instance). A Class cannot be cast to an Interface and I get a compile error.

So I tried to change my code to cast an instance of UserPermissionEntity to IEntityCore but at the stage I am doing this I do not have any instances yet.

After some further digging I've come up with this instead:

((IEntityCore)UserPermissionEntity.Relations).GetRelationsForFieldOfType("Permission")

which so-far compiles and I'm in the process of testing

Posts: 56
Joined: 08-Jun-2010
# Posted on: 22-Sep-2010 17:07:25   

Testing was pretty short. flushed

UserPermissionEntity.Relations doesn't cast to an IEntityCore

Posts: 56
Joined: 08-Jun-2010
# Posted on: 22-Sep-2010 17:26:46   

The snippet below is from your template entityInclude.template. I used to call it from my generated code but now its changed from public to internal I cannot.

I'm trying to to generate calls to the Entity.Relations...EntityUsing.... properties in a very similiar way to this but in an .lpt template file.

If you could point me at an example of this done in lpt I would be able to pull out the bits I need.


        /// <summary>Gets the relation objects which represent the relation the fieldName specified is mapped on. </summary>
        /// <param name="fieldName">Name of the field mapped onto the relation of which the relation objects have to be obtained.</param>
        /// <returns>RelationCollection with relation object(s) which represent the relation the field is maped on</returns>
        internal static <[If IsSubType]>new <[EndIf]>RelationCollection GetRelationsForField(string fieldName)
        {
            RelationCollection toReturn = new RelationCollection();
            switch(fieldName)
            {
<[Foreach RelatedEntity ManyToOne]><[If Not MappedFieldRelationIsHidden]>               case "<[MappedFieldNameRelation]>":
                    toReturn.Add(Relations.<[RelatedEntityName]>EntityUsing<[If Not RelatedEntityIsOtherEntity]><[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]><[EndIf]><[Foreach RelationField]><[RelationFieldName]><[NextForeach]>);
                    break;
<[EndIf]><[NextForeach]><[Foreach RelatedEntity OneToMany]><[If Not MappedFieldRelationIsHidden]>               case "<[MappedFieldNameRelation]>":
                    toReturn.Add(Relations.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]>);
                    break;
<[EndIf]><[NextForeach]><[Foreach RelatedEntity ManyToMany]><[If Not MappedFieldRelationIsHidden]>              case "<[MappedFieldNameRelation]>":
                    toReturn.Add(Relations.<[IntermediateEntityName]>EntityUsing<[Foreach RelationField OneToMany]><[RelatedEntityRelationFieldName]><[NextForeach]>, "<[CurrentEntityName]>Entity__", "<[IntermediateEntityName]>_", JoinHint.None);
                    toReturn.Add(<[IntermediateEntityName]>Entity.Relations.<[RelatedEntityName]>EntityUsing<[Foreach RelationField ManyToOne]><[RelationFieldName]><[NextForeach]>, "<[IntermediateEntityName]>_", string.Empty, JoinHint.None);
                    break;
<[EndIf]><[NextForeach]><[Foreach RelatedEntity OneToOne]><[If Not MappedFieldRelationIsHidden]>                case "<[MappedFieldNameRelation]>":
                    toReturn.Add(Relations.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[If IsOneToOnePkPk FkSide]><[RelationFieldName]><[EndIf]><[If Not IsOneToOnePkPk FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[If IsOneToOnePkFkUc FkSide]><[If RelatedEntityIsOtherEntity]><[RelationFieldName]><[EndIf]><[If Not RelatedEntityIsOtherEntity]><[RelatedEntityRelationFieldName]><[EndIf]><[EndIf]><[If Not IsOneToOnePkFkUc FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[NextForeach]>);
                    break;
<[EndIf]><[NextForeach]>                default:
<[If IsSubType]>                    toReturn = <[SuperTypeName]>Entity.GetRelationsForField(fieldName);
<[EndIf]>                   break;              
            }
            return toReturn;
        }


Posts: 56
Joined: 08-Jun-2010
# Posted on: 22-Sep-2010 17:30:46   

or maybe I can paste that into my own .template file and "include" it from the .lpt template somehow ??

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 23-Sep-2010 07:12:32   

Otis wrote:

Cast to IEntityCore and use GetRelationsForFieldOfType

we moved a lot of code to explicit implementations of the IEntityCore or IEntity2 interfaces to avoid intellisense showing a lot of methods/properties.

So your code becomes: ((IEntityCore)UserPermissionEntity).GetRelationsForFieldOfType("Permission")

BrettBailey wrote:

I was calling it as a static. I don't have any instances at the point this was called.

Sorry to go back to this point, but as I was re-reading the entire thread, the following question came to my mind: Do you have restrictions on creating a dummy instance for that call?

Posts: 56
Joined: 08-Jun-2010
# Posted on: 23-Sep-2010 11:19:02   

No restrictions as such, I just don't think that its a particularly elegant way of doing it.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 23-Sep-2010 21:22:46   

Sorry, it's still the only way simple_smile

Matt