CodeForLife
CodeForLife

Reputation: 89

Usage of implementsInterface element on entities in guidewire

I would like to know why do we use implementsInterface element in entities. I know one example where they use it to make it as assignable entity. But I could not understand what other purpose and how/why it is being used in entities.

Example: Injuryincident entity has claimantsupplier and coveragesupplier interface

Upvotes: 1

Views: 1002

Answers (2)

eliastion
eliastion

Reputation: 11

I won't be repeating the other answer describing how it works, but I would like to mention how implementing an interface on an entity is different (and serves different purposes) compared to using enhancements.

On basic level both approaches let you add extra functionality to your entity classes. In most cases what you really want to do is just create/expand an enhancement - they are easier to write, more convenient to modify and just as effective when all you want is to just add a new function or calculated property.

When you implement an interface, you're bringing in some more serious guns. While this approach takes more work and requires creation of several files (not to mention modifying the entity itself), it gives you two important advantages over the enhancement mechanism:

  1. The same interface can be implemented by several entities (typically each having its own implementation class) as well as non-entity classes. Objects of all such classes can then be used interchangeably in contexts expecting the interface (you can create an array of entity instances of several entities and even gosu-only wrappers/temporary objects and present it comfortably in the UI).

  2. You can leverage polymorphism. While enhancement functions can't be overridden, the interface implementations allow you full flexibility of polymorphic OOP. You can, for example, set up a default "do nothing" implementation on high level entity that you intend to use and then add more meaningful implementations for specific subtypes meant to really make use of the new functionality.

It does have some overhead and complicates things, however. As mentioned - Enhancements are typically simpler. In practice you should ask yourself whether the extra effort of creating and implementing the interface is worth it - in many cases even situations seemingly calling for polymorphism can be handled well enough by a simple switch typeof this in the enhancement to provide all the necessary type-based logic.

In personal experience I've used interfaces in quite a few situations, but Enhancements are my first choice in overwhelming majority of cases.

As a final note I'd like to mention a delegate entity. If what you want to add to some unrelated entities is not functionality but Properties with underlying database fields, creating a delegate entity and "implement" it with the desired standalone entities. A delegate entity does work a bit like an interface (you can use entity objects implementing the delegate interchangeably in situations where the delegate is expected) and you can set-up both interface implementation and enhancements on delegate level as well.

Upvotes: 0

robertant
robertant

Reputation: 108

I like to see it from this prespective, simplified and assuming that you have some java background:

As you probably already know it, having an entity means in the end of the day, having a Java class... Well, by using the implementsInterface element in your entity, is similar to implement an interface in you java class.

Here you have a quick example...

Consider the following:

MyEntiti.eti

<?xml version="1.0"?>
<entity
  xmlns="http://guidewire.com/datamodel"
  entity="MyEntity"
  table="myentity"
  type="retireable"/>

AnInterface.gs

package mypkg
interface AnInterface {
  function doSomething()
}

AnInterfaceImpl.gs

package mypkg

class AnInterfaceImpl implements AnInterface {
  override function doSomething() {
    print("Hello!")
  }
}

Image that you need MyEntity to have the ability of "doSomething", you just need to add the implementsInterface:

<?xml version="1.0"?>
<entity
  xmlns="http://guidewire.com/datamodel"
  entity="MyEntity"
  table="myentity"
  type="retireable">
  <implementsInterface
    iface="mypkg.AnInterface"
    impl="mypkg.AnInterfaceImpl"/>
</entity>

By doing that, the following code must work:

var myEntity = new MyEntity()
myEntity.doSomething() //this will call the  method defined in the interface-implementation

And even better, you migth let you implementation to recognize the related object of MyEntity and use it as per your needs:

package mypkg

class AnInterfaceImpl implements AnInterface {
  
  private final var  _relatedEntity : MyEntity

  construct(relatedTo : MyEntity) {
    _relatedEntity = relatedTo 
  }

  override function doSomething() {
    var createUser = _relatedEntity.CreateUser // you can accees to whatever you need
    print("Hello!, this is the related instace of MyEntity: ${_relatedEntity}")
  }
}

Hope it helps, regards!

Upvotes: 2

Related Questions