1

I created a web application which gets the data from the user from the console and stores the data in MongoDB and pass the data to the Angular frontend using Spring rest api.
Whole system is working finely and now I want to rearrange the folder structure according to a design pattern and as shown below I tried to use MVC model but I'm not sure if the file rearrangement is correct I don't know much about design patterns. So please help me with this matter and if the below structure is wrong please help me to make it correct.

enter image description here

Controlller

BookController - Class with rest api to pass data to angular frontend (REST Controller class)

Model

Book - Parent class with variables and relevant methods
Novel - Child class with variables and relevant methods
Poems - Child class with variables and relevant methods
MyBookManager - BookManager interface implemented class which includes crud operations with mongoDB

View

BookManager - Interface related to MyBookManager
Console - Class which takes the input from users from the the console and calls methods in MyBookManager to save them to DB

Repository

BookRepository - Interface which extends MongoRepository

Remaining classes

BookStoreApplication - Class which includes the main method(Running class)
ErrorHandle - Class which includes methods for validations

ArchieVon
  • 183
  • 1
  • 2
  • 16

1 Answers1

1

Overall good arrangement of the classes. The only adjustment I can suggest is to move the BookManager and MyBookManager in the Controller package. The reason is because it's between the view (Console) and the model (database), it can change the model (by updating the db), and it may contain some business logic on how to parse the input of the user properly.

If you want to further decouple the BookManager from the Controller, then you can call it BookService and place it inside a package called Service (along with MyBookService).

Regarding design patterns, MVC is an architectural pattern rather than a design pattern. I link you to other questions for more info:

Marc
  • 2,738
  • 1
  • 17
  • 21
  • BookManager is an interface it contains the abstract methods and implementation is in MyBookManager class. So you are suggesting to move the interface under the controller ? – ArchieVon Jul 12 '20 at 10:35
  • Yes I suggest to move the BookManager and implementation in the Controller. Otherwise you can create another package called Service for BookService and MyBookService. I updated my answer. Please upvote it if you found it useful ;) – Marc Jul 12 '20 at 14:39
  • 1
    So if I create a service package can I include the ErrorHandle class also inside the service package ? – ArchieVon Jul 20 '20 at 10:33
  • Good question. What kind of validations is in your ErrorHandle? Personally I place the classes with @ExceptionHandler methods in the Controller package because they are triggered in the controller layer. – Marc Jul 20 '20 at 13:08
  • So I get the book details and stuff from the console as user inputs, so there are errors regarding user inputs and methods to handle those errors are in that class E.g - non numeric errors handling method , etc. Is it okay to include that class also in the service package – ArchieVon Jul 21 '20 at 18:56
  • Since it contains only the logic for the input validation, which happens in the View layer, I would include it in the View package. The service layer (BookService) operates on the input that is already validated. But if BookService uses ErrorHandle then it's ok to include ErrorHandle in the Service package. Depends where the validation is done. – Marc Jul 22 '20 at 08:38