Skip to content

Getting started with a domain

Schaaf, Martin edited this page Jun 24, 2015 · 1 revision

We want to structure our code buy domains. In almost every case all code for a domain goes in one package like org.dicadeveloper.weplantaforest.reports.co2. Where all code related to co2 report calculation goes in. You can make such domain as fine grained as you want. Having everything for users under *.users could be to much but could also make sense. This depends on your arguments mainly ;-). But what we really don't want to have is several containers for controllers and entities and ... Where everything that is a controller or entity is goes in so that we end up with a lot together unrelated classes in a package. We don't want a trash can for plastic, class and food for all people instead we want a trash can for all trash from one person and if necessary we differentiate inside that trash can.

  1. Define your set of REST calls if necessary in annypoint for your domain
  2. Create a package your domain e.g. org.dicadeveloper.weplantaforest.projects
  3. Create your entity, controller, DTO, repository and service for your domain

Entity

@Entity
@Table(name = "Plant")
public class Project implements Base {
    
    @Id
    @GeneratedValue
    @Column(name = "_plantId")
    private long _id;

    @Override
    public long getId() {
        return _id;
    }

    public void setId(long id) {
        _id = id;
    }

    ...
}

Controller

@RestController
public class ProjectController {

    private ProjectService _projectService;

    private ProjectResourceAssembler _projectResourceAssembler;

    @Autowired
    public ProjectController(ProjectService projectService, ProjectResourceAssembler projectResourceAssembler) {
        _projectService = projectService;
        _projectResourceAssembler = projectResourceAssembler;
    }

    @RequestMapping(value = "/rest/v1/projects", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON })
    public PagedResources<ProjectDto> get(@PageableDefault(size = UtilConstants.DEFAULT_RETURN_RECORD_COUNT, page = 0, sort = { "_name" }) Pageable pageable,
            @Nullable PagedResourcesAssembler assembler) {
        Page<ProjectDto> projects = _projectService.findAll(pageable);
        PagedResources<ProjectDto> pagedResource = null;
        if (null != assembler) {
            pagedResource = assembler.toResource(projects, _projectResourceAssembler);
        } else {
            pagedResource = new PagedResources<ProjectDto>(new ArrayList<ProjectDto>(), null);
        }
        return pagedResource;
    }
}

DTO

public class ProjectDto implements BaseDto {

    @Mapping("_id")
    private Long _id;

    public ProjectDto() {
    }

    public void setId(Long id) {
        _id = id;
    }

    public Long getDtoId() {
        return _id;
    }

    ...
}

Repository

@Repository("projectRepository")
public interface ProjectRepository extends JpaRepository<Project, Long> {

}

Service

@Service
public class ProjectServiceImpl extends GenericServiceImpl<Project, ProjectDto, Long> implements ProjectService {

    @Autowired
    public ProjectServiceImpl(DozerBeanMapper mapper, @Qualifier("projectRepository") ProjectRepository projectRespository) {
        super(mapper, projectRespository);
    }
}
  1. Announce your components, entities and services to the application by adding your package to @EnableJpaRepositories and @EntityScan in the org.dicadeveloper.weplantaforest.Application class.
  2. Now start the application by calling gradle bootRun and open http://localhost:8080/dev/index.html to check if your URLs are mapped and if they return what you expect.