Java – How to design an application that read files using MVC pattern

design-patternsfile handlingjavamvc

I am going to write an application that reads data from a csv file, and display that data to the user. I intend to apply an MVC design pattern to create this application. A user will interact with this application through a console. Hence the view is a console.

There will be a Main.java that is responsible for :

  • Presenting a menu that a user can choose in the console.
  • Able to handle event in the console, e.g.: a user choose an option to read two csvfiles and ask the application to display differences between the two csv files.
  • Execute appropriate business logic for selected events.

To do this the Main.java will have a Main method that displays menu to a user in a console.

I am considering to make the Main.java as a Controller, because this class is responsible for taking the user input, and figure out what to do with the input. To do this, The Main.java will call a class called FileService.java, which is a service class. FileService.java is responsible for:

  • Read a csv file.
  • Display the content of the text file sorted according to some rule.
  • Display differences content of 2 csv files.

The FileService will parse the csv file using a csv parser, and stores the content of the files on a disk using MapDB. The intention of using MapDB is to allow user read huge file (e.g.: 5 GB), and perform operations to the content of the file. Once the file has been read, Main.java could display the content of the file, or display the differences between the 2 text files, when a user select the option in the console.

The FileService is intended to be the Model part in this application. And, the FileService is completely decoupled from the view and the controller, which is the Main.java.

Does this simple design achieve separation of concern addressed by MVC ?

Response to @tgkprog:

where is the view? if main is the controller and FileService the model/ business part then you need a view class too.

Response : When I built a web application, a view would be, but not limited to, a JSP, or an HTML page. In the context of the CSV file reader application, what would be the equivalent of a HTML or a JSP page ? This is some thing that I am not sure. The menu displayed in the console can be simply achieved inside the main method of the Main.java.

if the files are 5mb then i think a console app will be quite inadequate to display differences unless there are very few?

Response: I could dump the difference in a text file, and let user view the text file. This would be more helpful for them I think. Thank you for pointing this out.

Best Answer

In short, you want to create an MVC app that allows a user to identify a file; read the file; and then push the contents of that file into a database layer. Additionally, you want to treat the file handling routines as a Model.

View

  • This will be your console application providing user input & feedback.
  • User can select a file (via whatever means you pick)
  • View will either call controller with file name / descriptor or leave it in a place where the controller can poll and pick it up. I'd have the View call the Controller, but I'm just pointing out other ways of doing it.

Controller

  • Receives file name from View
  • Calls Model.FileServices with file name
  • Receives data returned from Model.FileServices, aka contents of file.
  • Calls Model.DBAccessLayer to store data returned from Model.FileServices.
  • Provide updates to View regarding transfer progress

Note, you'll end up with at least two Model modules. One for the FileServices and one for the DBAccessLayer.

So that's all well and good, but as file sizes increase then your system may not have enough memory to handle the entire file contents. There's a couple of options to resolve that issue.

  1. Have the FileServices implement a skip-take type call. This will allow you to pick up ### Byte blocks from the file and push to the DBA Model.

  2. Implement a method within the Model.DBA that essentially creates a stream between the FileServices and the DBAccessLayer. That would remove the Controller from acting as a traffic director, but it complicates the DBAccessLayer instead.

Of the two options, I would prefer the first as I think it more appropriately assigns responsibilities for the associated actions.

Related Topic