0

A good practice is to has business logic inside the close method (AutoCloseable interface) and not invoke the logic manually but via try-with-resource ?

I have a scenario where I write stream way to XML file with use of jax-b and I need to manually write start tag element and start end element. Business logic is in close method but I think is not good practice and what is your opinion?

My example

  public class PhoneBookWriter implements AutoCloseable {
         public void open(Path filePath) {
               // open file
         }

         public void write(PhoneBookInfo phoneBookInfo) {
               // write phone book pojo to xml file
         }

         public void close() {
               // check if stream is not null
               // --> part of business logic -> writeEndElement via XmlOutputWriterStream
               // close stream
  }

What is you opinion about open and close methods? May the method has business logic or only open stream (open method) and close stream (close method)? Should another logic be extracted to dedicated methods?

G. Sz.
  • 55
  • 1
  • 2
  • 4

1 Answers1

0

Writing an End Element is not business logic, if you define business logic as the work directly related to the primary Use Case of the code. This code supports one Use Case: write out PhoneBookInfo entries to persistent or external store. How to open/close the file and the file format details, and even the fact that you chose to use a file directly, are all technical architecture decisions that you or perhaps your Team Lead have made.

Do you have a scenario where the end element should not be written?

If not, then the close() method should perform all technical requirements related to file manipulation and formatting.

AR2
  • 73
  • 9
  • I describe low level class and business logic never should be implemented on low level structures (classes, etc.). – G. Sz. Jan 24 '19 at 15:12