0

Good day

This question is more of a meta question than a specific problem based question.

It is always a good idea to separate any and all DB code from view related code and view files, correct?

Thus is my assumption when using JSF correct in that:

  1. The xhtml file forms part of the view in the MVC.
  2. The @Named backing beans also forms part of the view.
  3. To ensure that one can relatively painlessly migrate away from JSF to another type of tech, one would ensure to not have ANY DB code inside the @Named backing beans.
  4. All DB code should reside in a controller / service class.
  5. The controller / service class will contain all the DB access code and business logic. This thus forms the controller of the MVC.
  6. The @Entity classes are used to map the DB to JPA and this thus forms the model of the MVC.

Now, if my understanding of the above is correct, what would best methods and methodologies be when handling the following scenario:

I have an XHTML file displaying JSF components (Primefaces). The lists and component linked members that link the JSF components to the Java code all resides in the @Named backing bean.

Now for argument sake, let's say that the specific form is used to CRUD a supplier's information, which of the following methods is recommended as best practices (while attemping to maintain as much seperation of concern between the View and the Controller Java code) when for instance edits were made on the XHTML form:

  1. Enforce ZERO DB code (thus never defining or using the Entity Manager) in the @Named backing bean file). The code to persist the changes after all input validation was successfully done, will reside in the Controller. To get these values to the Controller, we will have a function looking perhaps like this (basically send all the components on the xhtml form as parameters to a function in the Controller):public void supplierService (String supName, String SupAddress, String supTel....) The problem with this of course is that one may end up running into methods that takes tens of parameters. Very ugly and difficult to maintain.
  2. Accept that separating some DB code is not possible and every @Named file must have the required JPA DB code to persist and or merge changes to the models (however if this is considered best practice, what is the use of having Controller classes?).
  3. Create a temporary object of the same type as the model and set the attributes of this temporary object to the values obtained from the XHTML mapped components. Then only pass this temporary object to a method in the Controller. This method in the Controller will then persist and or merge the passed object's info. However I feel this may introduce unnecessary object instantiation overhead. Also I am not 100% sure what exactly happens 'behind the scenes' when I have a model named SupplierEntity.java that is mapped via JPA to a PostgreSQL DB and I call this code: SupplierEntity tempSup = new SupplierEntity(); Will JPA via Hibernate on Wildfly actually at this point create a new entity (record in the DB), and as such I cannot use this to create a temporary object to hold the values I am passing to the Controller as a temp instance of the underlying JPA entity, or will Hibernate (using JPA 2.1) ONLY create a new record when I do em.persist(mySupplier); and thus it is safe to use this method to pass objects to the controller's persisting method, instead of passing tens of parameters to the persisting method.
  4. Something completely different than what I mentioned above is considered to be the best practice for separating the MVC components in JSF as much as possible, while still preventing having to pass 50 parameters to the Controller.

Please as said right in the start, this is a meta question regarding best practices. If Stackoverflow is not the right forum for these questions, instead of down voting this into oblivion, please let me know where I should ask instead and I will gladly delete the question from here and create it on the right forum.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Letholdrus
  • 1,261
  • 3
  • 20
  • 36
  • 1
    Start reading this:https://stackoverflow.com/questions/5104094/what-components-are-mvc-in-jsf-mvc-framework and https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao – Kukeltje Apr 04 '18 at 10:07
  • Thanks @Kukeltje I am reading them now :) – Letholdrus Apr 04 '18 at 10:15
  • @Kukeltje Ok, from what I see especially in the Kickoff app on GitHub is that the Named view files are used to get a single instance of an object like 'user'. However the logic and merge / persisting of changes is done in the service / controller. – Letholdrus Apr 04 '18 at 10:26

0 Answers0