简体   繁体   中英

Java Coupling - Should I depend on Service or Repository Layer across different modules?

I have implemented a CodeService which will retrieve a list of countries from the code table.

In my ShippingService, I would like to check if the order is shipped to a certain country.

In this case, should I be using CodeService or CodeDAO to retrieve the list of countries.

public interface CodeService {
   public List<String> getCountryList();
}


@Service
public class CodeServiceImpl implements CodeService {

   @Autowired
   CodeDAO codeDao

   public List<String> getCountryList() {
       return codeDao.getCountryList();      
   }
}


@Service
public class ShippingServiceImpl implements ShippingService {

   @Autowired
   CodeDAO codeDao;

   @Autowired
   CodeSevice codeService;


   public void addOrder(Order order) {

     List<String> countries = codeService.getCountryList(); 
    //List<String> countries = codeDao.getCountryList();    

   }
}

If you dont have any additional logic such as in this case, I think its best to call directly to the DAO.

Pros: Calling always the service, would allow you to seperate completly the DAO layer from you app code. Cons: you will create 2 redundant classes for each dao, that will only delegate the call to the DAO.

The services layer should be used for business logic over the fetched data. For example: if you want to add permissions to the fetched countries.

If additional logic would be added to the countries in the future, its best to do a refactoring and create the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM