<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Juan Jose Yamunaque Castillo</title>
    <description>The latest articles on DEV Community by Juan Jose Yamunaque Castillo (@juan_joseyamunaquecasti).</description>
    <link>https://dev.to/juan_joseyamunaquecasti</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3953423%2F563b407f-c6b4-40a1-b40c-c06c864caec5.png</url>
      <title>DEV Community: Juan Jose Yamunaque Castillo</title>
      <link>https://dev.to/juan_joseyamunaquecasti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/juan_joseyamunaquecasti"/>
    <language>en</language>
    <item>
      <title>Is it a good practice to use a single Builder pattern for both Creating and Updating an entity?</title>
      <dc:creator>Juan Jose Yamunaque Castillo</dc:creator>
      <pubDate>Wed, 27 May 2026 02:29:17 +0000</pubDate>
      <link>https://dev.to/juan_joseyamunaquecasti/is-it-a-good-practice-to-use-a-single-builder-pattern-for-both-creating-and-updating-an-entity-lgj</link>
      <guid>https://dev.to/juan_joseyamunaquecasti/is-it-a-good-practice-to-use-a-single-builder-pattern-for-both-creating-and-updating-an-entity-lgj</guid>
      <description>&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When Creating: The id attribute is never sent (the database generates it). Both name and module are strictly mandatory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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,
        };
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;My questions are:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;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)?&lt;/li&gt;
&lt;li&gt;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?&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>architecture</category>
      <category>discuss</category>
      <category>softwareengineering</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
