dimanche 18 février 2018

CRUD DAO Persistence design

Recently I have really focused on writing clean code and implementing designs and I have stumbled accross a situation where I have several options but cannot decide which one is the appropriate one. I am working on a software that requires persistence on a collection of objects. I decided to implement a DAO Pattern. The thing is that persistency could both be Json OR Xml so I implemented it this way:

I created a Generic DAO:

public interface GenericDao<T> {
    public boolean add(T type);
    public boolean change(T type);
    public void delete(T type);
}

Then I created a CarDAO:

public interface CarDao extends GenericDao<Car> {
    public Car getByIdentificationNumber(int id);
    public void process();
}

For JSON persistence:

JsonGenericDao:

public class JsonGenericDao<T> implements GenericDao<T>  {

    public boolean add(T type) {
        // implement ADD for json
    }
    public boolean change(T type) {
        // implement Change for json
    }

    public void delete(T type) {
        // implement Delete for json
    }
}

JsonCarDao:

public class JsonCarDao extends JsonGenericDao<Task> implements CarDao {

    public Car getByIdentificationNumber(int id) {
        // Implement logic
    }

    public void process() {
        // Logic
    }
}

JsonCarDao extends JsonGenericDao to include the Add, Change, Delete and it also provides additional methods.

The same way is implemented XmlGenericDao and XmlCarDao.

So I end up with the possibility of using XmlCarDao OR JsonCarDao depending on the persistence I want to use.

When implementing the persistence, I used JAXB for XML and Gson for JSON. I made an EntityCollection<T> class to store the objects inside and I would convert this collection to either XML OR JSON depending on the persistence used and I would retrieve the information from the file to this collection, change what needs to be changed and then rewrite the file.

There are two ways I can implement it:

Option 1:

I could implement the persistence using Gson inside JsonGenericDao and do the same for JAXB inside XmlGenericDao.

Option 2:

I can create an interface Persister<T> and write two classes that implement this interface, therefore JsonPersister<T> and XmlPersister<T> with methods such as update(T type) and acquireAllFromFile(), one of which is going to rewrite the whole file with the new data, and the other one is going to retrieve the information from the file. (Same thing could be done in Option 1 but without making the additional classes)

Then inside JsonGenericDao<T> I can use: JsonPersister<EntityCollection<T>> and inside XmlGenericDao<T> I can use: XmlPersister<EntityCollection<T>> therefore packing everything.

The problem here though is thinking about this, it would mean that I can get rid of JsonGenericDao and XmlGenericDao and implement a single PersistenceGenericDao which is going to use a Persister interface inside its CONSTRUCTOR to specify if JsonPersister should be used or XmlPersister should be used. Now this seems like something I can do.. but it also appears to me that it messes up my initial DAO design. Is it an appropriate thing to do or is it bad practice?

Aucun commentaire:

Enregistrer un commentaire