Zum Hauptinhalt springen
Intelligence View
⚡ tsecurity.de Intelligence

Is it a good practice to use a single Builder pattern for both Creating and Updating an entity?

I am implementing the Builder pattern in a Node.js (TypeScript) backend to instantiate a Permission object before passing it to my repository layer.I want to reuse this same Builder for both creating and updating a permission, but the presence of the id and the validation rules change completely depending on the action:

  • When Creating: The id attribute is never sent (the database generates it). Both name and module are strictly mandatory.

  • When Updating: The id is strictly mandatory. However, name and module become optional, with the condition that at least one of them must be provided (they cannot both be missing). If a field is not provided, it should remain null or undefined so the repository knows it shouldn't overwrite that specific column.

import { CustomError } from "../../../shared";

export class PermissionBuilder {
    private name: string = '';
    private module: string = '';

    constructor() {}

    setName(name: string): PermissionBuilder {
        if (!name || !name.trim()) throw CustomError.badRequest('Name cannot be empty');
        this.name = name;
        return this;
    }

    setModule(module: string): PermissionBuilder {
        if (!module || !module.trim()) throw CustomError.badRequest('Module cannot be empty');
        this.module = module;
        return this;
    }

    build(): { name: string; module: string } {
        return {
            name: this.name,
            module: this.module,
        };
    }
}

My questions are:

  1. Is it considered a good practice to handle this conditional logic (where id is missing for creation but required for partial updates) inside a single Builder, or should I split this into two separate builders (e.g., CreatePermissionBuilder and UpdatePermissionBuilder)?
  2. If keeping a single Builder is the right approach, how should I restructure the internal state and the build() method to handle a payload that dynamically changes its required fields based on whether an id is present or not?
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Is it a good practice to use a single Builder pattern for both Creating and Updating an entity?

Thematisch verwandte Begriffe: good, practice, single, Builder · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-61591 | djust provides Phoenix LiveView-style reactive server-side rendering for…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
News ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

↗ Original-Quelle