A better domain layer

Let's now revisit the domain layer created in Chapter 3, Reverse Engineering the Domain Layer with JPA. Defining an ancestor class for all entities in this layer is not only the best practice but will also make our domain layer far easier to enhance in the future. Our ancestor class is defined as follows:

package com.gieman.tttracker.domain;

import java.io.Serializable;

public abstract class AbstractEntity implements Serializable{
    
}

Although this class has an empty implementation, we will add functionality in subsequent chapters.

We will also define an appropriate interface that has one generic method to return the ID of the entity:

package com.gieman.tttracker.domain;

public interface EntityItem<T> {
    
    public T getId();
    
}

Our domain layer can now extend our base AbstractEntity class and implement the EntityItem interface. The changes required to our Company class follows:

public class Company extends AbstractEntity implements EntityItem<Integer> {

// many more lines of code here

    @Override
    public Integer getId() {
        return idCompany;
    }    
}

In a similar way, we can change the remaining domain classes:

public class Project extends AbstractEntity implements EntityItem<Integer> {

// many more lines of code here

    @Override
    public Integer getId() {
        return idProject;
    }    
}
public class Task extends AbstractEntity implements EntityItem<Integer> {

// many more lines of code here

    @Override
    public Integer getId() {
        return idTask;
    }    
}
public class User extends AbstractEntity implements EntityItem<String> {

// many more lines of code here

    @Override
    public String getId() {
        return username;
    }    
}
public class TaskLog extends AbstractEntity implements EntityItem<Integer> {

// many more lines of code here

    @Override
    public Integer getId() {
        return idTaskLog;
    }    
}

We will now be well prepared for future changes in the domain layer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.154.252