jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.53k stars 4.02k forks source link

Please set attribute access modifiers to protected instead of private for all service classes and controller classes #23696

Closed vw98075 closed 2 months ago

vw98075 commented 1 year ago
Overview of the feature request

If JHipster is used as a building block of a real-world application, almost all of those generated classes need to be extended to separate customized code from the generated code. However, such an extended class won't be able to access its parent class attributes unless it changes the attribute modifiers to protected from private in its parent class.

Motivation for or Use Case

If JHipster is meant for real-world application usage, not just for demos or small-scale prototyping, it needs to be built for customization.

For example, a new web tier controller method is needed in addition to those generated ones. Based on the Single Responsibility Principle principle, a controller class with the new method is created by extending the existing controller class instead of creating an independent controller class, because it is for the same responsibility and the same consumer. In this case, the protected access modifier would make this type of customization litter easier.

Related issues or PR
mshima commented 1 year ago

Add this to .blueprint/app/generator.mjs file.

import BaseApplicationGenerator from 'generator-jhipster/generators/base-application';

export default class extends BaseApplicationGenerator {
  constructor(args, opts, features) {
    super(args, opts, { ...features, sbsBlueprint: true });
  }

  get [BaseApplicationGenerator.POST_WRITING]() {
    return this.asPostWritingTaskGroup({
      async postWritingTemplateTask() {
        this.queueTransformStream(async function* (generator) {
          for await (const file of generator) {
            if (file.path.endsWith('.java') && !file.path.includes('test')) {
              file.contents = Buffer.from(file.contents.toString().replaceAll('private', 'protected'));
            }
            yield file;
          }
        });
      },
    });
  }
}
desprez commented 8 months ago

Hi, +1 Yes Please, please, please !!!

and also for Angular, (may be also for Vue & react)

You preconize to extends "services" classes for further customizations pattern-3---side-by-side

but these classes must be open for extention (private -> protected).

if not lot of boiler code will be duplicated.

mraible commented 8 months ago

Please create a PR with this enhancement. I'd be happy to review it!

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days