Pirzada
Pirzada

Reputation: 4713

JavaEE naming file and packages basics

Developing small sample BookStore application and got a questions if someone explain to me please.

Using Model 2 architecture (MVC) pattern with JSP & Servlet. Hoping to add CDI dependency injection.

Sorry for the trouble.

1 - Naming convention.

In the screenshot. Is my folder structure and naming files is correct?

Ex:

I named my files like:

BookRepository.java implements IBook = Data access layer. Methods to add,update,remove etc

IBook.java = Interface for above methods.

Online tutorial I am following uses file names like

BookRepositoryImpl.java = CRUD methods implementation

BookRepository = Interface

Question: Is there any specific convention when we name the files in JavaEE?

How do you name you files and packages?

BookStore Application

Upvotes: 0

Views: 1390

Answers (1)

Arjan Tijms
Arjan Tijms

Reputation: 38163

There isn't any one and only true convention for naming those artifacts in Java EE.

It does however feels a bit strange if your entity/model is called Book, and that IBook is then an interface of a corresponding DAO/Repository. I would expect that to be IBookRepository instead.

I mentioned that there aren't any real conventions regarding the terms, but having said that ISomething for interfaces is not as common in Java as it's in e.g. C#. Eclipse uses this convention and a few other projects also do, but it's not that common.

More common would be to use BookRepository for the interface and then SomeTechBookRepository for the implementation, where "SomeTech" could be e.g. "JPA" or "JDBC". Also consider the term DAO instead of Repository.

Another recent trend in Java EE is to forego the interface for the repository/dao if you don't really need it yet (this topic is debatable). If you used the naming convention BookDAO for the implementation class and don't have an interface, then it's relatively easy later on to make BookDAO the interface and add e.g. a JPABookDAO.

Finally a JSP / Servlet based approach is a bit out of date these days in Java EE. Java EE comes with a MVC framework out of the box (JSF) and support for Services/DAOs (EJB) and persistence (JPA). For a CRUD example of these technologies see: http://jdevelopment.nl/sample-crud-app-with-jsf-and-richfaces

Upvotes: 1

Related Questions