Reputation: 339
i have spring cloud project and per microservice i think i have many packages. also i have model package witch contains entity-s and custom request response objects .
question 1 : what must be entity classes package name ? entity or model ?
question 2: where must save Searchrequest & Searchresponse and other request response classes ? in model package or i must create other package ?
question 3 : exits some Standard for package structuring ? (pleas give me link )
Upvotes: 6
Views: 12786
Reputation: 19060
Create package is free, so don't worry to create many packages. The objective of creating packages is organize your code and delimite some responsibilities or contexts.
question 1 : what must be entity classes package name ? entity or model ?
You need to understand the difference between these two before choose the better name:
A model object represents data in the MVC (Model View Controller) pattern. An entity object represents data in the ORM (Object Relational Mapping) pattern. They could be the same object. Or not.
So, you can use the Entity as a Model class, even I understand this as a bad practice. If it's your case, maybe you prefer to use model
as the package name. If not, entity
is the right choice.
question 2: where must save Searchrequest & Searchresponse and other request response classes ? in model package or i must create other package ?
This classes are very correlated with the controller package. So, you can create these two packages and put the request and response there, each one in his package:
controller.response
controller.request
question 3 : exits some Standard for package structuring ? (pleas give me link )
See this answer: Are there best practices for (Java) package organization?
Upvotes: 2
Reputation: 131346
There is not strict rules. You have to use a naming that is self explanatory (not needed to dig into the classes content to understand its meaning). That is right for micro-service, web application, batch and any kind of applications.
I think i have many packages
The main role of packages is to sort things in a human readable way.
For example if you have 2 or 3 classes by package, you can wonder on the relevance of them.
Similarly if you have 50 classes in a package, you can also wonder if you should not split them in subpackages.
Note that the package has also a role in terms of accessibility : indeed the package private
access level allows to set/reduce the accessibility of a class or a class member to the classes of the current package. It may also be very useful in terms of design and of isolation.
About your 3 questions :
question 1 : what must be entity classes package name ? entity or model ?
In Java, entity
refers generally to JPA entity (overall in a Spring project).
While model
refers to a more general concept that includes entity
but not only since the data model can also be the DTO
objects and any data specialization of your model.
If you use a single data objects layer (JPA entities that are also used as JSON representation) , using the model
term makes completely sense. If you use several level of abstraction, a package by abstraction level makes more sense.
question 2: where must save Searchrequest & Searchresponse and other request response classes ? in model package or i must create other package ?
If these objects go through every layer : in model
, otherwise probably elsewhere.
question 3 : exits some Standard for package structuring ? (please give me link )
3) Not really but as a rule of thumb, it should be self explanatory, very clear and shared by the team of the projects to favor consistency between projects.
Upvotes: 2
Reputation: 83
The package name for the database table should be entity.
for Q2 and Q3 Packaging Structure
Hope this helps,Let me know:)
Upvotes: 1
Reputation: 360
There is no specific standard structure, It will vary according to your need.
Mostly the SERVICE package will have all the service classes which have business logic and to connect with DAO classes, Controller will have all the rest end points, Model package will contain the pojo classes.
Upvotes: 3