#repeating modules

10 messages · Page 1 of 1 (latest)

frail osprey
#

Hello I started my app with modular monolith architecture and I noticed some problem because 4 modules will look very similar user, student, classroom, class all of them will have just validation and CRUD operations and I dont wanna repeat my code 4times so what to do?

thick pendantBOT
#

This post has been reserved for your question.

Hey @frail osprey! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

earnest gull
#

you can create another module containing the common code and dependencies

#

and then just use that module as a dependency in the other modules

frail osprey
#

I dont think it is that easy look at my facades

    public ClassroomResponseDto addClassroomLayout(ClassroomDto classroomLayoutFromUser) {
        Integer columns = classroomLayoutFromUser.columns();
        Integer rows = classroomLayoutFromUser.rows();
        ValidatorResultFacade validate = classroomValidator.validate(columns, rows);
        String message = validate.resultMessage();
        if(validate.isValid()) {
            repository.save(new Classroom(columns, rows));
        }
        return new ClassroomResponseDto(message, columns, rows);
    }
    public StudentDto addStudent(StudentDto studentInputData) {
        String firstName = studentInputData.firstName();
        String lastName = studentInputData.lastName();
        Byte age = studentInputData.age();

        ValidatorResultFacade validate = studentValidator.validate(firstName, lastName, age);
        if(validate.isValid()) {
            repository.save(new Student(firstName, lastName, age));
        }

        return studentInputData;
    }
earnest gull
#

you could do that (would do exactly the same)

    public <T,U> T addElement(T inputData, Function<T,ValidatorResultFacade> validator, Function<T,U> mapper) {
        ValidatorResultFacade validate = validator.apply(inputData);
        if(validate.isValid()) {
            repository.save(mapper.apply(inputData));
        }

        return inputData;
    }
#

or

    public <T,U> T addElement(T inputData, ValidatorResultFacade validate, Function<T,U> mapper) {
        if(validate.isValid()) {
            repository.save(mapper.apply(inputData));
        }

        return inputData;
    }
#

or you could add methods to your DTO that convert it to a DB object and to validate it and declare these in a common interface for your DTOs

#

but aside from that, you can't do much