Thursday, November 5, 2009

Calling Custom Entity Method From View Object Implementation Class

Few days ago I got a question from one of my colleagues about how to call custom method from EO implementation class. Colleague wanted to call this method from VO implementation class. I would not recommend you to follow this approach, because instead calling method in EO, you can invoke same code in VO row implementation class. But yes, exist different use cases and sometimes you may need to call EO from VO. In this post I will describe how you can do this.

Download sample application - CallingEntityMethodFromView.zip, this sample implements Java classes for EO and VO.

In EmployeesImpl.java, implementation class for EO, I have created simple custom method - calculateEmployeeRate(). This method multiplies salary with commission value per row:


Then you need to create VO and VO row implementation classes:


Create method in VO implementation class. This method should accept a row, based on this row it will retrieve EO implementation class and invoke custom method available there:


In order to make it work from ViewController, expose method you just created in VO implementation class through Client Interface:


Any exposed method can be declared in Page Definition. Pay attention for parameter value, I specified to accept current row. This means method in VO implementation always will get current row and based on it will invoke my custom method in EO:


On runtime, press Calculate Rate button that invokes custom logic in Model, and you will get result:

2 comments:

  1. Hi Andrew,

    Nice Post. Seems like tweaking :-) . But i really am very intersted in trying to know a use case when i should use this. It seen every thing that i can do in the EOImpl , i can do in the viewRowImpl. Am i correct. If possible please let me know a use case for this.

    Thanks
    -Pradip

    ReplyDelete
  2. Hi Pradip,

    You should think simple about it. Use EO Impl for those cases, when method can be shared across multiple VO's.

    I'm not talking about those cases when you implement validation, override doDML(), etc.

    Regards,
    Andrejus

    ReplyDelete