Recommendations for complex structure in java (DAO binding and connection with Service Layer)

Introduction

I am trying to create a rather complex structure in Java with interfaces, abstract classes and generics. Having no experience working with generics and only average experience in creating good OOP constructs, this begins to prove a rather difficult task.

I have a feeling that what I'm trying to do cannot actually be done, but I can get closer to it. I will try to explain this as concise as possible. I just want to say right away that this structure will represent my DAOs and service levels for accessing the database. To make this question more abstract will only complicate the work.

My DAO layer is completely beautiful. There is a common DAO interface, and for each object there is a DAO interface that extends the general and populates the common types. Then there is an abstract class that is extended by each DAO implementation, which, in turn, implements the corresponding interface. The confusion is likely to read, so here is a diagram showing the DAO for products as an example:

Diagram showing the DAO implementation for Product entities

Now for the service classes I had a similar design. Most methods in a class of service class apply to DAO methods anyway. If you replace each β€œDAO” in the diagram above with β€œService”, you will get the basis for my level of service. But there is one thing I want to do based on the following idea that I have

Each service class for an object will at least have access to one DAO object, namely the DAO of the object for which it is intended.

What...

Question / Problem

If I could create the right OO design so that each class of service has one instance variable for the DAO object of its corresponding object , my level of service would be ideal, in my opinion. Advice on this is welcome if my design is not as good as it seemed.

I implemented it as follows:

Class AbstractService

public abstract class AbstractService<EntityDAO> { EntityDAO entityDAO; public AbstractService() { entityDAO = makeEntityDAO(); //compiler/IDE warning: overridable method call in constructor } abstract EntityDAO makeEntityDAO(); } 

Class ProductServiceImpl

 public class ProductServiceImpl extends AbstractService<ProductDAOImpl> { public ProductServiceImpl() { super(); } @Override ProductDAOImpl makeEntityDAO() { return new ProductDAOImpl(); } } 

The problem with this design is the compiler warning, which I don't like: it has an overridable method call in the constructor (see comment). Now it is designed to be overridden, in fact I am applying it to so that each class of service has a link to the corresponding DAO. Is this the best I can do

I did my best to include everything that you might need, and only what you need for this issue. All I have to say is that comments are welcome and extensive answers even more, thanks for taking the time to read.

Additional Resources in StackOverflow

Understanding Service Levels and DAO

DAO and Service Levels (JPA / Hibernate + Spring)

+6
source share
1 answer

First, a quick note: usually in an application organized in layers such as Presentation / Service / DAO, for example, you have the following rules:

  • Each layer knows only the layer below.
  • He knows only its interfaces, and not the implementation class.

This will provide easier testing, better code encapsulation, and a clearer definition of the various levels (through interfaces that are easily identified as an open API).

However, there is a very common way to deal with this situation in such a way as to provide maximum flexibility: dependency injection . And Spring is the standard implementation of dependency injection (and many other things).

The idea (in short) is that your service will know that it needs IEntityDAO, and that someone will implement it and implement the interface before actually using the service. This someone is called an IOC ( Inversion of Control ) container. It could be Spring , and what it does is usually described by the application configuration file and will be executed when the application starts.

Important Note: The concept is brilliant and powerful, but dead simple stupidity. You can also use an architectural inversion of control framework without a frame with a very simple implementation consisting in a large static method of "assembling" your parts of the application. But in an industrial context, it is better to have a framework that allows you to enter other things, such as connecting to a database, stub clients of web services, JMS queues, etc.

Benefits:

  • You have an easy time for ridicule and testing, as the only thing that depends on the class is the interfaces
  • You have one file of a small set of XML files that describes the entire structure of your application, which is very convenient when your application grows.
  • This is a very widely accepted standard and is well known to many java developers.

Sample Java code:

 public abstract class AbstractService<IEntityDAO> { private IEntityDAO entityDAO; // you don't know the concrete implementation, maybe it a mock for testing purpose public AbstractService() { } protected EntityDAO getEntityDAO() { // only subclasses need this method } public void setEntityDAO(IEntityDAO dao) { // IOC container will call this method this.entityDAO = dao; } } 

And in the spring configuration file you will have something like this:

 <bean id="ProductDAO" class="com.company.dao.ProductDAO" /> [...] <bean id="ProductService" class="com.company.service.ProductService"> <property name="entityDAO" ref="ProductDAO"/> </bean> 
+3
source

All Articles