Search code examples
javapackage3-tier

Where to place Reader/Writer/Emailer ... *er classes in this package structure?


The way I was initially taught to structure 3-tier is as follows:

  • dao
  • domain
  • services
  • utils
  • jsf (beans) ...

A *er class that is instantiatable would fit none of these packages especially utils where classes would mostly group static methods. It could be added to services as a side-thought but that feels awkward.

Since then I've seen a more elaborate package structure (probably borrowed from Maven):

  • constant (harcoded constants and enums)
  • dao
  • dao.impl (implementations of interfaces)
  • model
  • resources (for property and config files)
  • service
  • service.impl
  • ui ...

However, I still don't see where one could place an *er class and now I see other types of classes popping up like custom Exceptions and this original pattern for Spring (see below). In general, it seems that these are types of classes that you'd commonly find in frameworks/APIs.

import org.springframework.context.ApplicationContext;

public class AppContext {  

    private static ApplicationContext ctx;  

    /** 
     * Injected from the class "ApplicationContextProvider" which is automatically 
     * loaded during Spring-Initialization. 
     */  
    public static void setApplicationContext(ApplicationContext applicationContext) {  
        ctx = applicationContext;  
    }  

    /** 
     * Get access to the Spring ApplicationContext from everywhere in your Application. 
     * 
     * @return 
     */  
    public static ApplicationContext getApplicationContext() {  
        return ctx;  
    }  
}

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware {  

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {  
        // Wiring the ApplicationContext into a static method  
        AppContext.setApplicationContext(ctx);  
    }
} 

How would you group these or any other "unclassifiables"?


Solution

  • It depends on what Read/Write and Emailer classes do.

    Assuming Emailer class sends e-mails, even thoguh it is instantiable class, would fit in the util or helper package, since it is not tightly bound to the application logic.

    For exceptions, I saw approach when they put the exception classes into a separate *.exceptions package right next to the package where it is being used, or inside the package where it is used.

    e.g.:

    application.dao
    application.dao.impl
    application.dao.exceptions