67
Views
6
Comments
Solved
Polymorphism in OutSystems?

How can I do something like this in outsystems?:



Solution

Hi Jota,

There are several ways to do what you want, but the question is: why do you want it? Ids won't be unique, so there's imho no use to have a "LikableId" that's either a PhotoId or an ImageId, since it isn't unique without the type.

That said, these are some solutions:

  1. The simplest one is to forego any typechecking on the Id, and make the LikeableId a Long Integer (or Integer, depending on the Id types of Photo and Video). In fact, I've used this solution a number of times in case I needed some external Id of different types;
  2. A second solution is to add some GUID to both Photo and Video, and put the GUID in LikeableId. The advantage is that it's unique now;
  3. The third option is the only one that displays some kind of actial polymorphism, and that's to have a meta Entity that contains an Id and Likes (and perhaps also the Type), and have the Ids of both Photo and Image be of the Identifier type of that Entity. In the Like entity you can then reference the Id of the meta Entity, but it's still valid to assign it a Photo or Image entity.

Hi Jorge,

Real polymorphism doesn't exist in OutSystems, but you could create a Static Entity 'LikableType' with two values, 'Photo' and 'Video'. If you add a field LikableTypeId in entity Like, then you can reference one of the two values.

Regards,
Lennart

Hi Jorge, 

what I would build is on photo/video tables include one column with the value (P/V) and the same on the Like table so when fetching the photo/video with the ID and that extra column. 

Other options that I thought is if it only 2 options with think using a boolean column,  if its true then video else photo. 

Hope it helps you. 

Regards. 

To complement the other two responses with some personal experience; 

We have solved this in the past by simply storing the ID either as Text or Long Int. It will make joining this in an aggregate almost impossible but that was of no issue for our use case. You just need another property (that you already have) that indicates what the referenced type was so you can query the correct Entity to retrieve the data. 

I do hope that they will support a gremlin based database in the future. This would make these types of references a breeze to implement :) 

I find your answer interesting; However, I still don't fully understand it. Could you explain a little more please?

Solution

Hi Jota,

There are several ways to do what you want, but the question is: why do you want it? Ids won't be unique, so there's imho no use to have a "LikableId" that's either a PhotoId or an ImageId, since it isn't unique without the type.

That said, these are some solutions:

  1. The simplest one is to forego any typechecking on the Id, and make the LikeableId a Long Integer (or Integer, depending on the Id types of Photo and Video). In fact, I've used this solution a number of times in case I needed some external Id of different types;
  2. A second solution is to add some GUID to both Photo and Video, and put the GUID in LikeableId. The advantage is that it's unique now;
  3. The third option is the only one that displays some kind of actial polymorphism, and that's to have a meta Entity that contains an Id and Likes (and perhaps also the Type), and have the Ids of both Photo and Image be of the Identifier type of that Entity. In the Like entity you can then reference the Id of the meta Entity, but it's still valid to assign it a Photo or Image entity.

Hi jota,

I agree with @Kilian Hekhuis second option. We have implemented same in one of our previous project and it works absolutely fine.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.