18

I'm developing Swing standalone application using Maven. I try to follow MVC pattern. I'm confused with my project structure. I have something like this:

/src/main/java/myName/appName             
/src/main/java/myName/appName/model       
/src/main/java/myName/appName/view
/src/main/java/myName/appName/controller

Now I want to incorporate Spring framework, what makes me place somewhere DAO and BO interfaces and implementations. I have read this article link and the suggested project structure does not suit mine. What crosses my mind is to add this:

/src/main/java/myName/appName/dao
/src/main/java/myName/appName/bo

The content of dao directory would look like this (with Client and Customer classes in model directory):

/src/main/java/myName/appName/dao/ClientDAO.java
/src/main/java/myName/appName/dao/ClientDAOImpl.java
/src/main/java/myName/appName/dao/CustomerDAO.java
/src/main/java/myName/appName/dao/CustomerDAOImpl.java

Is this bad? I want to learn good practices.

polmarex
  • 1,363
  • 5
  • 19
  • 35
  • Which part of the directory structure in your quoted article does not suit you? – Adrian Shum Sep 13 '12 at 01:55
  • @Adrian Shum where would you put DAO and BO classes then? – polmarex Sep 13 '12 at 08:46
  • @Costi Ciudatu why is it bad practice? What to use instead? – polmarex Sep 13 '12 at 08:46
  • 3
    Instead of DaoImpl ? MysqlDao, JpaDao or (worst case) DefaultDao or StandardDao. *Impl means there can be only one implementation or the second will be named *Impl2. There's no need for an interface if there can be only one implementation... – Costi Ciudatu Sep 13 '12 at 09:06
  • Sprign does not require interface for each DAO? In every Spring tutorial I have read, all DAOs had their own interface – polmarex Sep 13 '12 at 09:25

2 Answers2

18

The categorisation

/src/main/java/myName/appName/model        
/src/main/java/myName/appName/view 
/src/main/java/myName/appName/controller 

will cause problems for you later.
The package structure mentioned in the link you provided should suit you. You should have one package for each module/entity.
Eg /src/main/java/myName/appName/customer
and you should put all model, view, controller and dao classes related to customer in this package.

basiljames
  • 4,777
  • 4
  • 24
  • 41
  • I thought it would be good to separate view from model. What if I have view class that is not related to any of the entities? Where should I put it? And also, is it neccessary to create separate folders for model, dao and bo, and then in dao and bo separate folders for implementation? I would like to have something like this:`/src/main/java/myName/appName/customer/Customer.java`, `/src/main/java/myName/appName/customer/CustomerDAO.java`, `/src/main/java/myName/appName/customer/CustomerDAOImpl.java`, `/src/main/java/myName/appName/customer/CustomerBO.java` and BO implementation. – polmarex Sep 13 '12 at 08:43
  • The common classes if any can be put in a folder like `../appName/common/`. For an entity `../customer/bo` and ../customer/dao` can be added for other classes it can be direclty in `../customer`. Name your dao implementation like `MySqlDao` or `Dao`. – basiljames Sep 13 '12 at 17:19
  • 4
    Could you elaborate on why it will cause problems? – Dennis Apr 06 '14 at 21:23
11

You can follow any of the two project structure you have defined in your problem but that should depend on your application size.

If you have a large amount of modules in your application than you can follow the project structure as described by @basijames. Because that will be helpful in managing the code and distributing the work in your team.

If you don't many modules then I prefer you should go for project structure something like below.

/src/main/java/myName/appName/controller   
/src/main/java/myName/appName/model
/src/main/java/myName/appName/service
/src/main/java/myName/appName/dao
/src/main/java/myName/appName/bo

But according to me while creating a maven project you should skip the selecting archtypes.

Hope this helps you. Cheers.

Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44