<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amr elshaer on Medium]]></title>
        <description><![CDATA[Stories by Amr elshaer on Medium]]></description>
        <link>https://medium.com/@amrelsher07?source=rss-a45bdd28e1bf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ygu3xtVlw7D7cKsKwM1jWQ.jpeg</url>
            <title>Stories by Amr elshaer on Medium</title>
            <link>https://medium.com/@amrelsher07?source=rss-a45bdd28e1bf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 16:04:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amrelsher07/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SQL Server Schema Changes: Metadata-Only Operations, Performance, and EF Core Best Practices]]></title>
            <link>https://amrelsher07.medium.com/sql-server-schema-changes-metadata-only-operations-performance-and-ef-core-best-practices-0a74736b7bad?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a74736b7bad</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[ef-core]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Mon, 16 Nov 2026 07:44:55 GMT</pubDate>
            <atom:updated>2026-11-16T07:44:55.849Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Imagine you have a SQL Server table containing 20 million records, and you need to make a simple database schema change:</p><ul><li>Add a new column.</li><li>Change a nullable column to NOT NULL.</li><li>Add a default value.</li><li>Change an existing column’s data type.</li></ul><p>These operations might look straightforward, especially when using Entity Framework Core migrations.</p><p>But there is an important question:</p><p><strong>Does SQL Server need to update every existing row, or can it perform the operation by updating only the table metadata?</strong></p><p>The difference can be significant.</p><p>For large production databases, understanding how SQL Server handles schema changes can help you avoid unnecessary table scans, transaction log growth, long-running migrations, and application downtime.</p><p>Let’s explore these operations and how to approach them safely with SQL Server and EF Core.</p><h3>1. Metadata-Only Operations vs. Physical Data Changes</h3><p>Before discussing individual schema changes, we need to understand two different types of database operations.</p><h3>Metadata-only operations</h3><p>A metadata-only operation changes the database definition without physically modifying every existing row.</p><p>For example, adding a nullable column without a default value:</p><pre>ALTER TABLE Orders<br>ADD Description NVARCHAR(200) NULL;</pre><p>SQL Server can perform this operation by updating the table metadata.</p><p>Existing rows logically contain NULL in the new column. SQL Server doesn&#39;t need to rewrite every existing record.</p><p>For a table containing millions of rows, this can be extremely fast.</p><h3>Physical data changes</h3><p>Other operations require SQL Server to inspect or modify existing records.</p><p>For example:</p><pre>UPDATE Orders<br>SET Description = N&#39;Unknown&#39;<br>WHERE Description IS NULL;</pre><p>This is a data modification operation.</p><p>SQL Server must process the affected rows, generate transaction log records, and acquire the necessary locks.</p><p>The performance impact depends on:</p><ul><li>Number of affected rows.</li><li>Row size.</li><li>Available indexes.</li><li>Transaction log configuration.</li><li>Locking and concurrent workloads.</li><li>Available disk I/O.</li></ul><p><strong>Important:</strong> A metadata-only operation is not a guarantee of zero locking. Schema changes can still require schema modification locks (Sch-M), potentially blocking concurrent queries while the change is applied.</p><h3>2. Adding a NOT NULL Column with a Default Value</h3><p>This is one of the most interesting SQL Server optimizations.</p><p>Imagine an existing table:</p><pre>CREATE TABLE Orders<br>(<br>    Id BIGINT PRIMARY KEY,<br>    OrderNumber NVARCHAR(50) NOT NULL<br>);</pre><p>It contains millions of records.</p><p>Now we want to add an IsActive column:</p><pre>ALTER TABLE Orders<br>ADD IsActive BIT NOT NULL<br>    CONSTRAINT DF_Orders_IsActive DEFAULT (1);</pre><p>What happens to existing records?</p><p>Every existing record logically receives the value 1.</p><p>But does SQL Server physically update millions of rows?</p><h3>SQL Server fast-add optimization</h3><p>Starting with SQL Server 2012 Enterprise Edition, SQL Server supports an optimization for adding a NOT NULL column with a runtime-constant default value.</p><p>Instead of updating every existing row, SQL Server can store the default value in table metadata.</p><p>When an existing row is accessed, SQL Server returns the metadata-stored default value.</p><p>New rows that omit the column receive the default through the default constraint.</p><p>This means the operation can complete almost instantaneously, regardless of the number of existing rows.</p><p>For example:</p><pre>ALTER TABLE Orders<br>ADD IsActive BIT NOT NULL<br>    CONSTRAINT DF_Orders_IsActive DEFAULT (1);</pre><h3>Important limitations</h3><p>This optimization is not universal.</p><p>Some important restrictions include:</p><ol><li>The SQL Server version and edition must support the optimization.</li><li>The default expression must be a runtime constant.</li><li>Certain data types are not supported for the online operation.</li><li>The maximum possible row size must not exceed SQL Server’s supported row-size limit.</li></ol><p>For example:</p><pre>DEFAULT (1)</pre><p>is suitable.</p><p>A runtime-constant expression such as:</p><pre>DEFAULT (GETUTCDATE())</pre><p>can also qualify.</p><p>However:</p><pre>DEFAULT (NEWID())</pre><p>does not qualify because it produces a different value for each row.</p><p>In that case, SQL Server must perform an offline operation.</p><p><strong>The takeaway:</strong> Adding a NOT NULL column with a default value can be a metadata-only operation, but you must verify your SQL Server edition, version, data type, and default expression before relying on it.</p><h3>3. Adding a Default Constraint to an Existing Column</h3><p>Another common misunderstanding is assuming that adding a default constraint updates existing records.</p><p>Consider this table:</p><pre>CREATE TABLE Orders<br>(<br>    Id BIGINT PRIMARY KEY,<br>    IsActive BIT NULL<br>);</pre><p>We want to add a default value:</p><pre>ALTER TABLE Orders<br>ADD CONSTRAINT DF_Orders_IsActive<br>DEFAULT (1) FOR IsActive;</pre><p>Does this update existing NULL values?</p><p><strong>No.</strong></p><p>A default constraint is applied when inserting a new record and the insert doesn’t explicitly provide a value for that column.</p><p>It does not automatically update existing records.</p><p>For example:</p><pre>INSERT INTO Orders (Id)<br>VALUES (100);</pre><p>The new record receives IsActive = 1.</p><p>But existing records where IsActive is NULL remain unchanged.</p><p>If you want to update existing records, you must explicitly execute:</p><pre>UPDATE Orders<br>SET IsActive = 1<br>WHERE IsActive IS NULL;</pre><p>Be careful with this statement on large tables. It can generate substantial transaction log activity and affect production workloads.</p><h3>4. Changing an Existing Column from NULL to NOT NULL</h3><p>This is another common scenario.</p><p>Suppose we have:</p><pre>CREATE TABLE Orders<br>(<br>    Id BIGINT PRIMARY KEY,<br>    IsActive BIT NULL<br>);</pre><p>Now we want to enforce NOT NULL.</p><p>The first step is checking whether NULL values exist:</p><pre>SELECT COUNT_BIG(*)<br>FROM Orders<br>WHERE IsActive IS NULL;</pre><p>If NULL values exist, SQL Server will reject this operation:</p><pre>ALTER TABLE Orders<br>ALTER COLUMN IsActive BIT NOT NULL;</pre><p>SQL Server requires the existing data to satisfy the new constraint.</p><h3>Recommended approach</h3><p>First, update the existing NULL values:</p><pre>UPDATE Orders<br>SET IsActive = 1<br>WHERE IsActive IS NULL;</pre><p>Then change the column definition:</p><pre>ALTER TABLE Orders<br>ALTER COLUMN IsActive BIT NOT NULL;</pre><p>Optionally, add a default constraint for future inserts:</p><pre>ALTER TABLE Orders<br>ADD CONSTRAINT DF_Orders_IsActive<br>DEFAULT (1) FOR IsActive;</pre><h3>What about performance?</h3><p>Unlike adding a new nullable column, changing an existing nullable column to NOT NULL requires SQL Server to verify that no NULL values violate the constraint.</p><p>This can involve scanning the table and acquiring schema modification locks.</p><p>The operation is not equivalent to a simple metadata-only column addition.</p><p>For a table with millions of records, plan this change carefully.</p><h3>5. Updating Millions of Rows: Avoid One Huge UPDATE</h3><p>Consider this statement:</p><pre>UPDATE Orders<br>SET IsActive = 1<br>WHERE IsActive IS NULL;</pre><p>For a small table, this is perfectly reasonable.</p><p>For a table with millions of affected records, one large transaction can cause problems:</p><ul><li>Significant transaction log growth.</li><li>Long-running transactions.</li><li>Lock escalation.</li><li>Blocking concurrent application requests.</li><li>Increased rollback time if the operation fails.</li></ul><h3>Better approach: Batch updates</h3><p>Instead of updating everything in one transaction, process smaller batches.</p><p>For example:</p><pre>WHILE 1 = 1<br>BEGIN<br>    UPDATE TOP (5000) Orders<br>    SET IsActive = 1<br>    WHERE IsActive IS NULL;</pre><pre>    IF @@ROWCOUNT = 0<br>        BREAK;</pre><pre>    WAITFOR DELAY &#39;00:00:00.100&#39;;<br>END;</pre><p>Each UPDATE statement is a separate autocommit transaction when executed outside an explicit transaction.</p><p>This helps reduce the size and duration of individual transactions.</p><p>You can adjust the batch size based on:</p><ul><li>Database workload.</li><li>Transaction log capacity.</li><li>Lock contention.</li><li>SQL Server performance.</li><li>Acceptable migration duration.</li></ul><p>For very large tables, you should also monitor progress and consider running the backfill as a separate deployment or background operation.</p><h3>6. What About Entity Framework Core Migrations?</h3><p>Now let’s connect these SQL Server concepts to EF Core.</p><p>Consider this entity:</p><pre>public class Order<br>{<br>    public long Id { get; set; }</pre><pre>    public bool? IsActive { get; set; }<br>}</pre><p>We want to make IsActive required and assign a default value of true.</p><p>Our updated entity becomes:</p><pre>public class Order<br>{<br>    public long Id { get; set; }</pre><pre>    public bool IsActive { get; set; }<br>}</pre><p>And we configure the default:</p><pre>modelBuilder.Entity&lt;Order&gt;()<br>    .Property(x =&gt; x.IsActive)<br>    .HasDefaultValue(true);</pre><p>After generating a migration:</p><pre>dotnet ef migrations add MakeOrderIsActiveRequired</pre><p>EF Core may generate an operation similar to:</p><pre>migrationBuilder.AlterColumn&lt;bool&gt;(<br>    name: &quot;IsActive&quot;,<br>    table: &quot;Orders&quot;,<br>    type: &quot;bit&quot;,<br>    nullable: false,<br>    defaultValue: true,<br>    oldClrType: typeof(bool),<br>    oldType: &quot;bit&quot;,<br>    oldNullable: true);</pre><p>There is something important to understand here:</p><p><strong>EF Core describes the desired schema change. It does not guarantee that SQL Server will execute it as a metadata-only operation.</strong></p><p>The generated SQL, SQL Server version, existing data, and operation type determine the actual database impact.</p><p>Also, changing the default constraint does not automatically backfill existing NULL values in an already-existing column.</p><p>You must handle existing data separately.</p><h3>7. Best Practice: Use a Multi-Phase Migration for Large Tables</h3><p>When changing an existing nullable column to required on a large production table, I prefer separating the process into multiple deployment phases.</p><p>This is particularly useful when the application must remain available during deployment.</p><h3>Phase 1: Prepare the database</h3><p>Keep the column nullable and establish a default for new inserts.</p><pre>ALTER TABLE Orders<br>ADD CONSTRAINT DF_Orders_IsActive<br>DEFAULT (1) FOR IsActive;</pre><p>Update the application so new records always provide a valid value, either through application logic or the database default.</p><p>Remember that a database default only applies when the column is omitted from the INSERT statement or DEFAULT is explicitly requested. An explicitly supplied NULL does not use the default.</p><h3>Phase 2: Backfill existing data</h3><p>Run a controlled batch update:</p><pre>WHILE 1 = 1<br>BEGIN<br>    UPDATE TOP (5000) Orders<br>    SET IsActive = 1<br>    WHERE IsActive IS NULL;</pre><pre>    IF @@ROWCOUNT = 0<br>        BREAK;<br>END;</pre><p>Monitor the operation and ensure new application writes cannot introduce additional NULL values.</p><h3>Phase 3: Validate the data</h3><p>Before enforcing the constraint:</p><pre>IF EXISTS<br>(<br>    SELECT 1<br>    FROM Orders<br>    WHERE IsActive IS NULL<br>)<br>BEGIN<br>    THROW 50001, &#39;NULL values still exist in Orders.IsActive&#39;, 1;<br>END;</pre><h3>Phase 4: Enforce NOT NULL</h3><p>Once the data is clean:</p><pre>ALTER TABLE Orders<br>ALTER COLUMN IsActive BIT NOT NULL;</pre><p>Update the EF Core model to match the database schema.</p><p>This approach gives you more control over the backfill, validation, and final schema change.</p><p>For extremely large tables, consider a dedicated backfill job or deployment script rather than placing the entire process inside one EF Core migration transaction.</p><h3>8. EF Core Migration Best Practices for Production</h3><p>EF Core migrations are excellent for managing database schema evolution, but generated migrations should never be treated as automatically optimized production scripts.</p><p>Here are some recommendations:</p><p><strong>1. Always inspect generated migrations.</strong></p><p>Understand whether EF Core generated ADD COLUMN, ALTER COLUMN, UPDATE, or DROP COLUMN.</p><p><strong>2. Generate SQL scripts before production deployment.</strong></p><pre>dotnet ef migrations script --idempotent -o migration.sql</pre><p>This generates a script that checks the migrations history table before applying pending migrations.</p><p><strong>3. Separate schema changes from large data migrations.</strong></p><p>Adding a column and updating millions of existing rows are different operations with different performance characteristics.</p><p><strong>4. Prefer controlled batch updates for large backfills.</strong></p><p>Avoid unnecessarily long-running transactions.</p><p><strong>5. Test against production-like data volumes.</strong></p><p>A migration that takes milliseconds on a development database might behave very differently on a table containing millions of records.</p><p><strong>6. Consider application compatibility during deployment.</strong></p><p>When using rolling deployments, ensure old and new application versions can coexist with the intermediate database schema.</p><p><strong>7. Monitor the transaction log and blocking.</strong></p><p>A successful migration is not necessarily an efficient migration.</p><h3>9. Quick Comparison</h3><p>OperationTypical behaviorMain concernAdd nullable column without defaultMetadata-onlyBrief schema lockingAdd NOT NULL column with constant defaultCan be metadata-only on supported SQL Server editions/versionsFeature restrictionsAdd default constraint to existing columnMetadata/schema changeExisting NULL values remainUpdate existing rowsPhysical data modificationTransaction log and lockingChange NULL to NOT NULLRequires data validationTable scan and schema lockingChange column data typeDepends on conversion and storage impactPossible data rewrite</p><h3>Conclusion</h3><p>The most important lesson is that database schema changes are not all equal.</p><p>A simple ALTER TABLE statement might be a metadata-only operation, or it might trigger a substantial data modification.</p><p>When working with large SQL Server tables:</p><ul><li>Understand how SQL Server executes the operation.</li><li>Distinguish metadata changes from physical data changes.</li><li>Take advantage of SQL Server’s fast-add optimization when supported.</li><li>Backfill large datasets in controlled batches.</li><li>Validate existing data before enforcing constraints.</li><li>Review EF Core-generated SQL before deploying migrations.</li></ul><p><strong>Don’t judge a database migration by how simple its C# code looks. Judge it by what SQL Server actually has to do.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a74736b7bad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generate IDs Upfront: A Simple Technique for Building More Decoupled Microservices]]></title>
            <link>https://amrelsher07.medium.com/generate-ids-upfront-a-simple-technique-for-building-more-decoupled-microservices-92e2a6615efd?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/92e2a6615efd</guid>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Mon, 02 Nov 2026 06:53:57 GMT</pubDate>
            <atom:updated>2026-11-02T06:54:59.246Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b_EVKrCQ9jjxW07epnLMtA.png" /></figure><h3>How generating an identifier early can change data ownership, reduce coupling, and simplify communication between services</h3><h3>Introduction</h3><p>One of the most common mistakes when designing microservices is thinking that splitting an application into multiple services automatically makes it decoupled.</p><p>Imagine splitting an e-commerce application into:</p><ul><li>Sales Service</li><li>Billing Service</li><li>Shipping Service</li><li>Inventory Service</li></ul><p>Each service has its own database and deployment pipeline.</p><p>Sounds like a good microservices architecture, right?</p><p>But consider what happens when Sales needs to communicate with Billing.</p><p>You might pass all the information Billing needs:</p><pre>public Task ProcessPayment(<br>    Guid orderId,<br>    Guid customerId,<br>    string customerName,<br>    string customerEmail,<br>    decimal amount,<br>    string currency,<br>    string paymentToken);</pre><p>Now Billing depends on a large contract containing information owned by Sales.</p><p>Someone might suggest passing only an identifier:</p><pre>public Task ProcessPayment(Guid orderId);</pre><p>That looks much better.</p><p>But how does Billing get the payment information?</p><p>It might call Sales to retrieve the order details.</p><p>We haven’t removed the coupling. We’ve simply moved it somewhere else.</p><p>This raises an important question:</p><p><strong>How can services communicate without depending on each other’s internal data?</strong></p><p>One interesting approach, discussed by Udi Dahan in his presentation <em>Design More Decoupled Services With One Weird Trick</em>, is to generate identifiers upfront.</p><p>Let’s understand the idea using a practical example.</p><h3>1. The problem: Sharing data between services</h3><p>Consider a simple e-commerce checkout process.</p><p>A customer wants to purchase a laptop.</p><p>The checkout process needs:</p><ul><li>Product and order information.</li><li>Payment information.</li><li>Shipping address.</li><li>Inventory reservation.</li></ul><p>A traditional implementation might look like this:</p><pre>Sales Service<br>                      |<br>          +-----------+-----------+<br>          |           |           |<br>          v           v           v<br>       Billing     Shipping    Inventory<br>       Service      Service     Service</pre><p>Sales coordinates everything.</p><p>It collects the data, passes it to other services, and knows how each service works.</p><p>For example:</p><pre>public record PlaceOrderCommand(<br>    Guid CustomerId,<br>    List&lt;OrderItem&gt; Items,<br>    Address ShippingAddress,<br>    PaymentDetails PaymentDetails,<br>    decimal TotalAmount);</pre><p>The problem isn’t the number of parameters itself.</p><p>The real problem is that Sales knows the data structures and responsibilities of other business capabilities.</p><p>If Shipping changes its address structure, Sales might need to change.</p><p>If Billing changes its payment information, Sales might need to change.</p><p>This creates unnecessary coupling.</p><h3>2. Why passing only an ID doesn’t solve the problem</h3><p>Let’s try another approach.</p><p>Instead of passing all the information, we pass only OrderId.</p><pre>public record OrderAccepted(Guid OrderId);</pre><p>Billing receives the event:</p><pre>public class OrderAcceptedConsumer<br>    : IConsumer&lt;OrderAccepted&gt;<br>{<br>    public async Task Consume(<br>        ConsumeContext&lt;OrderAccepted&gt; context)<br>    {<br>        var order = await _orderService<br>            .GetOrder(context.Message.OrderId);</pre><pre>        // Process payment using order information.<br>    }<br>}</pre><p>We have a problem again.</p><p>Billing needs to communicate with Sales to retrieve the information it needs.</p><p>Now Billing depends on:</p><ul><li>Sales API availability.</li><li>Sales API contracts.</li><li>Sales response structure.</li><li>Sales communication infrastructure.</li></ul><p>We have hidden the coupling rather than eliminated it.</p><p>So, what if we change the way we collect and own the data in the first place?</p><h3>3. The idea: Generate the ID upfront</h3><p>Instead of generating an identifier after collecting all the information, generate a workflow identifier when the customer starts checkout.</p><p>For example:</p><pre>var checkoutId = Guid.NewGuid();</pre><p>This identifier represents the checkout process, not necessarily the final order.</p><p>Now each service can use the same identifier to associate its own information with the checkout.</p><p>Consider this design:</p><pre>CheckoutId<br>                      |<br>          +-----------+-----------+<br>          |           |           |<br>          v           v           v<br>        Sales       Billing    Shipping<br>          |           |           |<br>          v           v           v<br>       Sales DB    Billing DB  Shipping DB</pre><p>Each service owns its data.</p><p>For example:</p><p><strong>Sales Database</strong></p><p>CheckoutIdTotalAmountABC-1231500</p><p><strong>Billing Database</strong></p><p>CheckoutIdPaymentTokenABC-123token-123</p><p><strong>Shipping Database</strong></p><p>CheckoutIdAddressABC-123Cairo, Egypt</p><p>Notice that all three services use the same CheckoutId.</p><p>But they don’t share databases, entities, or internal models.</p><p>The identifier simply correlates information belonging to the same business workflow.</p><p>This is the important difference.</p><h3>4. Implementing the idea using .NET</h3><p>Let’s build a simplified example using:</p><ul><li>.NET 8 Minimal APIs</li><li>Entity Framework Core</li><li>MassTransit</li><li>RabbitMQ</li><li>SQL Server</li></ul><p>We’ll use three services: Sales, Billing, and Shipping.</p><h3>Step 1: Sales generates the CheckoutId</h3><p>The frontend starts checkout by calling:</p><pre>POST /checkout</pre><p>Sales generates the identifier.</p><pre>app.MapPost(&quot;/checkout&quot;, async (<br>    SalesDbContext db) =&gt;<br>{<br>    var checkout = new Checkout<br>    {<br>        Id = Guid.NewGuid(),<br>        Status = CheckoutStatus.Draft<br>    };</pre><pre>    db.Checkouts.Add(checkout);</pre><pre>    await db.SaveChangesAsync();</pre><pre>    return Results.Ok(new<br>    {<br>        checkout.Id<br>    });<br>});</pre><p>The frontend receives:</p><pre>{<br>  &quot;id&quot;: &quot;abc-123&quot;<br>}</pre><p>From this point forward, every service uses this identifier to associate its data with the checkout.</p><p><strong>Important:</strong> The identifier is generated by the backend when checkout starts. It doesn’t have to be generated by the browser.</p><h3>Step 2: Sales saves order information</h3><pre>POST /checkout/abc-123/items</pre><pre>app.MapPost(&quot;/checkout/{id:guid}/items&quot;, async (<br>    Guid id,<br>    SalesDbContext db) =&gt;<br>{<br>    var checkout = await db.Checkouts<br>        .FindAsync(id);</pre><pre>    if (checkout is null)<br>        return Results.NotFound();</pre><pre>    checkout.AddItem(&quot;Laptop&quot;, 1500);</pre><pre>    await db.SaveChangesAsync();</pre><pre>    return Results.Ok();<br>});</pre><p>Sales only knows about its own data.</p><h3>Step 3: Billing saves payment information</h3><p>The frontend calls Billing:</p><pre>POST /checkout/abc-123/payment</pre><pre>app.MapPost(&quot;/checkout/{id:guid}/payment&quot;, async (<br>    Guid id,<br>    BillingDbContext db) =&gt;<br>{<br>    var payment = new Payment<br>    {<br>        CheckoutId = id,<br>        PaymentToken = &quot;token-123&quot;,<br>        Status = PaymentStatus.Pending<br>    };</pre><pre>    db.Payments.Add(payment);</pre><pre>    await db.SaveChangesAsync();</pre><pre>    return Results.Ok();<br>});</pre><p>Billing saves its own payment information.</p><p>It doesn’t need to call Sales.</p><h3>Step 4: Shipping saves shipping information</h3><p>The frontend calls Shipping:</p><pre>POST /checkout/abc-123/shipping</pre><pre>app.MapPost(&quot;/checkout/{id:guid}/shipping&quot;, async (<br>    Guid id,<br>    ShippingDbContext db) =&gt;<br>{<br>    var shipment = new Shipment<br>    {<br>        CheckoutId = id,<br>        Address = &quot;Cairo, Egypt&quot;,<br>        DeliveryType = &quot;Express&quot;<br>    };</pre><pre>    db.Shipments.Add(shipment);</pre><pre>    await db.SaveChangesAsync();</pre><pre>    return Results.Ok();<br>});</pre><p>Shipping owns its address and delivery information.</p><p>Again, no dependency on Sales or Billing databases.</p><h3>Step 5: Place the order</h3><p>Once the required checkout information has been collected and validated, the frontend submits the checkout:</p><pre>POST /checkout/abc-123/place-order</pre><p>Sales changes its state and publishes an event.</p><pre>app.MapPost(&quot;/checkout/{id:guid}/place-order&quot;, async (<br>    Guid id,<br>    SalesDbContext db,<br>    IPublishEndpoint publish) =&gt;<br>{<br>    var checkout = await db.Checkouts<br>        .FindAsync(id);</pre><pre>    if (checkout is null)<br>        return Results.NotFound();</pre><pre>    checkout.MarkPendingPayment();</pre><pre>    await db.SaveChangesAsync();</pre><pre>    await publish.Publish(<br>        new OrderAccepted(id));</pre><pre>    return Results.Ok();<br>});</pre><p>The event contract is intentionally small:</p><pre>public record OrderAccepted(Guid CheckoutId);</pre><p>Notice that we’re not passing payment details, addresses, or product information.</p><p>Why?</p><p>Because each service already owns the information it needs.</p><p>In a production implementation, the checkout must also be protected against submission before all required business steps are complete. That can be handled using readiness events or a dedicated checkout workflow coordinator.</p><h3>5. Billing and Shipping consume the event</h3><p>When RabbitMQ receives OrderAccepted, both services can independently react to it.</p><p><strong>Billing Service:</strong></p><pre>public class OrderAcceptedConsumer(<br>    BillingDbContext db)<br>    : IConsumer&lt;OrderAccepted&gt;<br>{<br>    public async Task Consume(<br>        ConsumeContext&lt;OrderAccepted&gt; context)<br>    {<br>        var payment = await db.Payments<br>            .FirstOrDefaultAsync(x =&gt;<br>                x.CheckoutId ==<br>                context.Message.CheckoutId);</pre><pre>        if (payment is null)<br>            throw new InvalidOperationException(<br>                &quot;Payment information not found.&quot;);</pre><pre>        // Charge the payment.<br>    }<br>}</pre><p><strong>Shipping Service:</strong></p><pre>public class OrderAcceptedConsumer(<br>    ShippingDbContext db)<br>    : IConsumer&lt;OrderAccepted&gt;<br>{<br>    public async Task Consume(<br>        ConsumeContext&lt;OrderAccepted&gt; context)<br>    {<br>        var shipment = await db.Shipments<br>            .FirstOrDefaultAsync(x =&gt;<br>                x.CheckoutId ==<br>                context.Message.CheckoutId);</pre><pre>        if (shipment is null)<br>            throw new InvalidOperationException(<br>                &quot;Shipping information not found.&quot;);</pre><pre>        // Prepare shipment.<br>    }<br>}</pre><p>Both services receive the same event.</p><p>But each service retrieves information from its own database.</p><p>That’s the key architectural difference.</p><h3>6. What happens when payment fails?</h3><p>Now let’s consider a real-world scenario.</p><p>A customer completes checkout, but the payment provider declines the card.</p><p>Should we create another checkout?</p><p>No.</p><p>The customer should be able to update the payment method and retry using the same CheckoutId.</p><p>We need to distinguish between checkout status, order status, and payment status.</p><p>For example:</p><pre>public enum OrderStatus<br>{<br>    Draft,<br>    PendingPayment,<br>    Confirmed,<br>    Cancelled<br>}</pre><pre>public enum PaymentStatus<br>{<br>    Pending,<br>    Failed,<br>    Paid<br>}</pre><p>Billing processes the payment:</p><pre>public async Task ProcessPayment(Guid checkoutId)<br>{<br>    var payment = await _db.Payments<br>        .FirstAsync(x =&gt;<br>            x.CheckoutId == checkoutId);</pre><pre>    var result = await _paymentGateway<br>        .ChargeAsync(payment.PaymentToken);</pre><pre>    if (!result.Success)<br>    {<br>        payment.MarkAsFailed();</pre><pre>        await _db.SaveChangesAsync();</pre><pre>        await _publishEndpoint.Publish(<br>            new PaymentFailed(checkoutId));</pre><pre>        return;<br>    }</pre><pre>    payment.MarkAsPaid();</pre><pre>    await _db.SaveChangesAsync();</pre><pre>    await _publishEndpoint.Publish(<br>        new PaymentSucceeded(checkoutId));<br>}</pre><p>The event contracts:</p><pre>public record PaymentFailed(Guid CheckoutId);</pre><pre>public record PaymentSucceeded(Guid CheckoutId);</pre><p>Sales consumes these events and updates its own order status.</p><p>For example, when payment fails:</p><pre>public class PaymentFailedConsumer(<br>    SalesDbContext db)<br>    : IConsumer&lt;PaymentFailed&gt;<br>{<br>    public async Task Consume(<br>        ConsumeContext&lt;PaymentFailed&gt; context)<br>    {<br>        var checkout = await db.Checkouts<br>            .FirstAsync(x =&gt;<br>                x.Id == context.Message.CheckoutId);</pre><pre>        checkout.MarkPaymentFailed();</pre><pre>        await db.SaveChangesAsync();<br>    }<br>}</pre><p>Now the frontend can retrieve the checkout status:</p><pre>GET /checkout/abc-123/status</pre><p>Response:</p><pre>{<br>  &quot;checkoutId&quot;: &quot;abc-123&quot;,<br>  &quot;orderStatus&quot;: &quot;PendingPayment&quot;,<br>  &quot;paymentStatus&quot;: &quot;Failed&quot;<br>}</pre><p>The frontend can display:</p><blockquote><em>Your payment could not be processed. Your order has not been confirmed. Please update your payment method and try again.</em></blockquote><p>The customer can retry:</p><pre>POST /checkout/abc-123/payment/retry</pre><p>Billing processes a new payment attempt using the updated payment method.</p><p>The same CheckoutId is reused.</p><p><strong>A failed payment does not require a new checkout.</strong></p><p>For production, each payment attempt should have its own identifier, and payment operations must be idempotent to prevent duplicate charges.</p><h3>7. How does this improve decoupling?</h3><p>Let’s compare the two approaches.</p><h3>Traditional approach</h3><pre>Sales<br>  |<br>  | OrderId<br>  v<br>Billing<br>  |<br>  | GET /orders/{id}<br>  v<br>Sales API</pre><p>Billing depends on Sales to retrieve the information it needs.</p><h3>Generate the ID upfront</h3><pre>CheckoutId<br>                      |<br>          +-----------+-----------+<br>          |           |           |<br>          v           v           v<br>        Sales       Billing    Shipping<br>          |           |           |<br>          v           v           v<br>       Sales DB    Billing DB  Shipping DB</pre><p>Now:</p><ul><li>Sales owns order information.</li><li>Billing owns payment information.</li><li>Shipping owns delivery information.</li><li>Each service can evolve independently.</li><li>Events communicate business facts.</li><li>The shared identifier correlates the workflow.</li></ul><p>The important point is that <strong>we are not sharing data ownership; we are sharing workflow identity.</strong></p><h3>8. How does this relate to EF Core key generation?</h3><p>There is another important detail.</p><p>If we generate GUIDs in application code, EF Core needs to understand that the application owns the key.</p><p>Consider:</p><pre>public class Checkout<br>{<br>    public Guid Id { get; private set; }</pre><pre>    public static Checkout Create()<br>    {<br>        return new Checkout<br>        {<br>            Id = Guid.NewGuid()<br>        };<br>    }<br>}</pre><p>We generate the identifier before saving the entity.</p><p>But EF Core also needs the correct key-generation configuration.</p><pre>public class CheckoutConfiguration<br>    : IEntityTypeConfiguration&lt;Checkout&gt;<br>{<br>    public void Configure(<br>        EntityTypeBuilder&lt;Checkout&gt; builder)<br>    {<br>        builder.HasKey(x =&gt; x.Id);</pre><pre>        builder.Property(x =&gt; x.Id)<br>            .ValueGeneratedNever();<br>    }<br>}</pre><p>Why ValueGeneratedNever()?</p><p>Because the application generates the key, not the database.</p><p>Without the correct configuration, EF Core can interpret an already-populated generated key as an indication that the entity already exists when it discovers it through a tracked entity graph.</p><p>This can lead to EF Core marking a new entity as Modified instead of Added.</p><p>For example:</p><pre>var checkout = Checkout.Create();</pre><pre>db.Checkouts.Add(checkout);</pre><pre>await db.SaveChangesAsync();</pre><p>This works because Add() explicitly marks the entity as Added.</p><p>However, when a new entity is discovered through a tracked navigation, EF Core uses its key-generation configuration and current key value to determine its state.</p><p>That’s why explicitly configuring application-generated keys is important.</p><h3>When should you use each option?</h3><p>ConfigurationWho generates the ID?ValueGeneratedNever()ApplicationValueGeneratedOnAdd()Database or EF Core provider</p><p>For an application-generated GUID:</p><pre>builder.Property(x =&gt; x.Id)<br>    .ValueGeneratedNever();</pre><p>For a SQL Server identity column:</p><pre>builder.Property(x =&gt; x.Id)<br>    .UseIdentityColumn();</pre><p>The rule is simple:</p><p><strong>If your application generates the key, configure EF Core accordingly. If the database generates it, don’t assign it yourself.</strong></p><p>I covered this behavior and a real entity-tracking bug in more detail in my article:</p><p><a href="https://gist.github.com/AmrElshaer/544e8a6addc728b321d60082fe8919c4">EF Core Key Generation and Entity States: ValueGeneratedNever vs ValueGeneratedOnAdd</a></p><h3>9. Important considerations before using this approach</h3><p>Generating IDs upfront is useful, but it doesn’t automatically solve every microservices problem.</p><p>There are several things to consider.</p><p><strong>1. Use the Transactional Outbox Pattern</strong></p><p>What happens if Sales saves the order but crashes before publishing OrderAccepted?</p><p>You could lose the event.</p><p>The Outbox Pattern ensures that the business data and outgoing event are persisted atomically, then the event is published reliably.</p><p><strong>2. Make consumers idempotent</strong></p><p>RabbitMQ messages can be delivered more than once.</p><p>A consumer must avoid processing the same business operation twice.</p><p>For example, receiving the same payment event twice must not charge the customer twice.</p><p><strong>3. Handle incomplete workflows</strong></p><p>A customer might start checkout and leave before completing it.</p><p>You need an expiration policy for abandoned checkout data.</p><p><strong>4. Don’t confuse correlation with authorization</strong></p><p>Knowing a CheckoutId doesn&#39;t mean a user is allowed to access that checkout.</p><p>Always validate ownership and authorization.</p><p><strong>5. Don’t use events for everything</strong></p><p>Not every CRUD operation needs event-driven communication.</p><p>Use events when something meaningful happens in the business and other capabilities need to react independently.</p><p><strong>6. Remember that contracts still exist</strong></p><p>Even if an event contains only an identifier, consumers still depend on its contract and on the meaning of the identifier.</p><p>Decoupling doesn’t mean having zero dependencies. It means avoiding unnecessary dependencies on another service’s internal implementation.</p><h3>10. When should you use this technique?</h3><p>This approach is particularly useful when:</p><ul><li>A business process involves multiple services.</li><li>Different services collect different parts of the same workflow.</li><li>Each service should own its own data.</li><li>Large event payloads are becoming difficult to maintain.</li><li>Services frequently change because of dependencies on other services’ data structures.</li><li>A workflow needs to survive retries and partial failures.</li></ul><p>It is less useful for simple CRUD operations where introducing asynchronous messaging and distributed workflow management would add unnecessary complexity.</p><h3>Conclusion</h3><p>The biggest lesson is not simply to replace HTTP calls with RabbitMQ messages.</p><p>It is to reconsider how services collect, own, and share information.</p><p>Instead of having one service collect everybody else’s data and distribute it, generate a workflow identifier early.</p><p>Let each service collect and persist its own information.</p><p>Then use events to communicate what happened, while using the shared identifier to correlate the workflow.</p><p>This creates a different kind of architecture:</p><ul><li>Services own their data.</li><li>Services don’t need to query each other for their internal information.</li><li>Events communicate business facts.</li><li>Identifiers connect the different parts of a workflow.</li></ul><p><strong>Don’t share data ownership. Share workflow identity.</strong></p><p>That’s the architectural shift worth understanding when building decoupled microservices.</p><ul><li><a href="https://particular.net/videos/design-more-decoupled-services-with-one-weird-trick?utm_source=chatgpt.com">Udi Dahan — Design More Decoupled Services With One Weird Trick (NDC Oslo 2024)</a></li><li><a href="https://gist.github.com/AmrElshaer/544e8a6addc728b321d60082fe8919c4?utm_source=chatgpt.com">EF Core Key Generation and Entity States — Original Gist</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92e2a6615efd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LEFT JOIN vs OUTER APPLY in SQL Server: Understanding the Real Difference]]></title>
            <link>https://amrelsher07.medium.com/left-join-vs-outer-apply-in-sql-server-understanding-the-real-difference-4696cae6f718?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/4696cae6f718</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[sql-server]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Sun, 19 Jul 2026 14:01:33 GMT</pubDate>
            <atom:updated>2026-07-19T14:01:33.911Z</atom:updated>
            <content:encoded><![CDATA[<p>LEFT JOIN vs OUTER APPLY in SQL Server: Understanding the Real Difference</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83f0d8875563652956b16ae411783919/href">https://medium.com/media/83f0d8875563652956b16ae411783919/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4696cae6f718" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ How to Find Bottlenecks]]></title>
            <link>https://amrelsher07.medium.com/how-to-find-bottlenecks-5212e20e2e93?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/5212e20e2e93</guid>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-design]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Sat, 25 Apr 2026 13:07:32 GMT</pubDate>
            <atom:updated>2026-04-25T13:07:32.466Z</atom:updated>
            <content:encoded><![CDATA[<h3>🚀 How to Find Bottleneck</h3><h3>📌 Introduction</h3><p>When building scalable systems, one of the most important questions is:</p><blockquote><strong><em>How many requests per second can my system handle?</em></strong></blockquote><p>To answer that, you need to understand three core concepts:</p><ul><li><strong>QPS (Queries Per Second)</strong> → throughput</li><li><strong>Efficiency</strong> → real vs theoretical performance</li><li><strong>Bottleneck</strong> → what limits your system</li></ul><h3>⚡ 1. What is QPS?</h3><p>QPS (Queries Per Second) is:</p><blockquote><em>The number of requests your system can handle per second</em></blockquote><h3>✅ Basic Formula</h3><p>[QPS = 1 / average response time]</p><p>Example:</p><ul><li>Request takes <strong>100 ms = 0.1 sec</strong></li></ul><p>[QPS = 1 / 0.1 = 10]</p><p>👉 One worker can handle <strong>10 requests/sec</strong></p><h3>🧠 2. QPS with Multi-Core Systems</h3><p>If your server has multiple CPU cores:</p><p>[QPS = cores/latency]</p><h3>Example:</h3><ul><li>8 cores</li><li>0.2 sec per request</li></ul><p>[QPS = 8 / 0.2 = 40]</p><h3>🔥 3. Real System Formula (Little’s Law)</h3><p>Best formula for real systems:</p><pre>[QPS = concurrency / response time]</pre><h3>Example:</h3><ul><li>200 concurrent requests</li><li>0.5 sec response time</li></ul><p>[QPS = 200 / 0.5 = 400]</p><h3>🏗️ 4. System Architecture View (API + DB)</h3><p>A real request flow:</p><pre>Client → API → Business Logic → Database → API → Response</pre><p>Each layer has its own limit.</p><h3>⚙️ 5. Calculate API Capacity</h3><pre>QPS API = (cores×efficiency)/ CPU time per request</pre><h3>Example:</h3><ul><li>8 cores</li><li>70% efficiency</li><li>10 ms CPU time (0.01 sec)</li></ul><pre>QPS_API = (8 × 0.7) / 0.01 = 560</pre><h3>🗄️ 6. Calculate Database Capacity</h3><pre>QPS_DB = connections/query time</pre><h3>Example:</h3><ul><li>200 DB connections</li><li>50 ms query (0.05 sec)</li></ul><pre>QPS_DB = 200 / 0.05 = 4000</pre><h3>⚠️ 7. Adjust for DB Calls per Request</h3><p>If each API request makes multiple DB calls:</p><pre>DB Load = API QPS × queries per request</pre><h3>Example:</h3><ul><li>API QPS = 560</li><li>3 queries per request</li></ul><pre>DB load = 560 × 3 = 1680</pre><p>👉 DB can handle 4000 → OK</p><h3>🧠 8. Find the Bottleneck</h3><h3>Golden Rule:</h3><pre>Max QPS = MIN(API QPS, DB QPS)</pre><h3>Example:</h3><ul><li>API = 560</li><li>DB = 4000</li></ul><p>👉 <strong>Final QPS = 560 (API is bottleneck)</strong></p><h3>📉 9. What is Efficiency?</h3><p>Efficiency measures how close you are to ideal performance:</p><pre>Efficiency = actual QPS / theoretical QPS</pre><h3>Example</h3><h3>Step 1: Theoretical QPS</h3><pre>QPS = 8 / 0.01 = 800</pre><h3>Step 2: Load Test Result</h3><ul><li>Actual QPS = 480</li></ul><h3>Step 3: Efficiency</h3><pre>Efficiency = 480 / 800 = 60%</pre><h3>🧠 13. Final Mental Model</h3><pre>API capacity → DB capacity → external services<br>        ↓<br>   slowest one wins</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5212e20e2e93" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connecting SQL Server to MCP using Data API Builder (DAB)]]></title>
            <link>https://amrelsher07.medium.com/connecting-sql-server-to-mcp-using-data-api-builder-dab-531ddfd3a2b8?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/531ddfd3a2b8</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Tue, 24 Mar 2026 10:50:01 GMT</pubDate>
            <atom:updated>2026-03-24T10:50:01.637Z</atom:updated>
            <content:encoded><![CDATA[<p>Modern applications — especially AI-powered tools — need a secure and structured way to access databases.<br>Using <strong>Microsoft Data API Builder (DAB)</strong> with <strong>MCP (Model Context Protocol)</strong> provides a powerful way to expose SQL Server data as APIs that AI agents and tools can safely consume.</p><h3>Why Use DAB with MCP ?</h3><h3>Secure Data Access</h3><ul><li>Role-based permissions</li><li>Fine-grained control over operations (read, create, update, delete)</li></ul><h3>2. No Backend Code Needed</h3><ul><li>No need to build ASP.NET APIs</li><li>DAB generates REST, GraphQL, and MCP endpoints automatically</li></ul><h3>3. AI-Ready (MCP Integration)</h3><ul><li>MCP allows AI tools (like copilots, agents) to query your database</li><li>Standardized JSON-RPC communication</li></ul><h3>4. Full CRUD Support</h3><p>You can:</p><ul><li>✅ Read data</li><li>➕ Insert data</li><li>✏️ Update data</li><li>❌ Delete data</li></ul><h3>Step-by-Step Setup</h3><h3>Step 1: Install DAB</h3><pre>dotnet tool install -g Microsoft.DataApiBuilder</pre><p>Verify installation:</p><pre>dab --version</pre><h3>🧩 Step 2: Initialize Configuration File</h3><p>Create a new config: you can use another database provider like MySQL or PostgreSQL</p><pre>dab init --database-type mssql --connection-string &quot;Server=.;Database=TestDb;Trusted_Connection=True;&quot;</pre><p>This generates:</p><pre>dab-config.json</pre><h3>🧩 Step 3: Understand the Config Structure</h3><p>Basic structure:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://aka.ms/dab-config-schema&quot;,<br>  &quot;data-source&quot;: {<br>    &quot;database-type&quot;: &quot;mssql&quot;,<br>    &quot;connection-string&quot;: &quot;your-connection-string&quot;<br>  },<br>  &quot;entities&quot;: {}<br>}</pre><h3>🧩 Step 4: Add an Entity (Table Mapping)</h3><p>Example: expose Users table</p><pre>{<br>  &quot;entities&quot;: {<br>    &quot;Users&quot;: {<br>      &quot;source&quot;: &quot;dbo.Users&quot;,<br>      &quot;permissions&quot;: [<br>        {<br>          &quot;role&quot;: &quot;anonymous&quot;,<br>          &quot;actions&quot;: [ &quot;read&quot; ]<br>        }<br>      ]<br>    }<br>  }<br>}</pre><h3>🧩 Step 5: Enable Full CRUD Operations</h3><pre>{<br>  &quot;entities&quot;: {<br>    &quot;Users&quot;: {<br>      &quot;source&quot;: &quot;dbo.Users&quot;,<br>      &quot;permissions&quot;: [<br>        {<br>          &quot;role&quot;: &quot;anonymous&quot;,<br>          &quot;actions&quot;: [ &quot;create&quot;, &quot;read&quot;, &quot;update&quot;, &quot;delete&quot; ]<br>        }<br>      ]<br>    }<br>  }<br>}</pre><h3>🧩 Step 6: Connect MCP Client</h3><p>Configure your MCP client:</p><pre>&quot;sql-mcp-server&quot;: {<br>  &quot;type&quot;: &quot;stdio&quot;,<br>  &quot;command&quot;: &quot;dab&quot;,<br>  &quot;args&quot;: [<br>    &quot;start&quot;,<br>    &quot;--mcp-stdio&quot;,<br>    &quot;role:anonymous&quot;,<br>    &quot;--config&quot;,<br>    &quot;C:\\projects\\DAB\\dab-config.json&quot;<br>  ]<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQw5UQF9dfOqtaJ6IC7a_Q.png" /></figure><p>Also, can update the data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*tkmoWlHfw3xH56r3DoQ4nA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*m8KwegeLyUWKQt4kSt5s9A.png" /></figure><p>If you want to test the api without MCQ, you can run</p><pre>dab start</pre><p>and can navigate to the browser like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*hNXlkNkkC2E0-PyyDJcX4Q.png" /></figure><h3>🔐 Best Practices</h3><h3>✅ Use Roles Properly</h3><pre>&quot;role&quot;: &quot;admin&quot;</pre><p>Instead of exposing everything as anonymous</p><h3>✅ Limit Columns</h3><pre>&quot;columns&quot;: [ &quot;Id&quot;, &quot;Name&quot; ]</pre><h3>🎯 When Should You Use This?</h3><p>Use DAB + MCP when:</p><ul><li>You want <strong>AI agents to access your DB</strong></li><li>You need <strong>quick API without backend</strong></li><li>You want <strong>secure, declarative data exposure</strong></li></ul><h3>🏁 Conclusion</h3><p>Using <strong>Microsoft Data API Builder with MCP</strong> transforms your SQL Server into an <strong>AI-ready data service</strong> with minimal effort.</p><p>You get:</p><ul><li>🔐 Secure access</li><li>⚡ Instant APIs</li><li>🤖 AI integration</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=531ddfd3a2b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ExecuteAsync finally runs on a separate thread]]></title>
            <link>https://amrelsher07.medium.com/executeasync-finally-runs-on-a-separate-thread-11f53190e037?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/11f53190e037</guid>
            <category><![CDATA[background-task]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[background-jobs]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Sat, 22 Nov 2025 13:38:09 GMT</pubDate>
            <atom:updated>2025-11-22T13:38:09.691Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/217b87d6e2e6b81980b284cb8853a611/href">https://medium.com/media/217b87d6e2e6b81980b284cb8853a611/href</a></iframe><p><a href="https://learn.microsoft.com/en-us/dotnet/core/compatibility/extensions/10.0/backgroundservice-executeasync-task">Breaking change: BackgroundService runs all of ExecuteAsync as a Task - .NET</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11f53190e037" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keep Your Code Clean: Auto-Format C# with Husky + CSharpier Before Every Commit:]]></title>
            <link>https://amrelsher07.medium.com/keep-your-code-clean-auto-format-c-with-husky-csharpier-before-every-commit-e8600018cc2c?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8600018cc2c</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Mon, 22 Sep 2025 17:04:42 GMT</pubDate>
            <atom:updated>2025-09-22T17:04:42.360Z</atom:updated>
            <content:encoded><![CDATA[<h4>⚙️ Husky + .NET Setup (with CSharpier)</h4><p>This project uses <a href="https://github.com/typicode/husky.net"><strong>Husky</strong></a> for Git hooks to ensure code quality and formatting before commits.<br> We’ve integrated it with <a href="https://csharpier.com/"><strong>CSharpier</strong></a> to auto-format C# files.</p><h3>🚀 Installation</h3><h3>1. Install Local Tool Manifest</h3><p>Create a manifest (if not already exists):</p><pre>dotnet new tool-manifest</pre><h3>2. Install Tools</h3><p>Install Husky and CSharpier as local tools:</p><pre>dotnet tool install Husky<br>dotnet tool install csharpier</pre><p>This will create/update .config/dotnet-tools.json.</p><p>Commit this file to source control so the whole team uses the same versions.</p><h3>🔄 Restore Tools</h3><p>On any new machine or CI/CD environment, run:</p><pre>dotnet tool restore</pre><p>This installs all tools listed in .config/dotnet-tools.json.</p><h3>⚡ Setup Husky</h3><h3>1. Enable Git Hooks</h3><p>Initialize Husky in your repo:</p><pre>dotnet husky install</pre><p>This creates a .husky/ folder.</p><h3>2. Add Pre-Commit Hook</h3><p>Run:</p><pre>dotnet husky set pre-commit -c &quot;dotnet husky run&quot;</pre><p>This creates .husky/pre-commit which executes our configured tasks.</p><h3>📝 Husky Task Runner Config</h3><p>In .task-runner.json:</p><pre>{<br>  &quot;tasks&quot;: [<br>    {<br>      &quot;name&quot;: &quot;Run csharpier&quot;,<br>      &quot;command&quot;: &quot;dotnet&quot;,<br>      &quot;args&quot;: [ &quot;csharpier&quot;, &quot;format&quot;, &quot;${staged}&quot; ],<br>      &quot;include&quot;: [ &quot;**/*.cs&quot; ]<br>    }<br>  ]<br>}</pre><h3>Explanation</h3><ul><li>command: &quot;dotnet&quot; → runs a .NET CLI tool.</li><li>csharpier format → formats code.</li><li>${staged} → only formats staged files.</li><li>include: [ &quot;**/*.cs&quot; ] → only C# files are included.</li></ul><h3>✅ Usage</h3><ul><li>Stage your changes:</li></ul><p>git add MyClass.cs</p><ul><li>Commit:</li></ul><p>git commit -m &quot;Added new feature&quot;</p><p>👉 Husky will run csharpier format on staged .cs files before completing the commit.<br> If formatting fails, the commit is blocked until fixed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8600018cc2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zero-Downtime Schema Migrations on Large Production Tables]]></title>
            <link>https://amrelsher07.medium.com/zero-downtime-schema-migrations-on-large-production-tables-0bdc27d3ad40?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/0bdc27d3ad40</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[high-availability]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 06:34:10 GMT</pubDate>
            <atom:updated>2025-09-11T06:34:10.843Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with large production databases, schema migrations can be extremely risky.<br> For example, imagine you have an Employees table with <strong>50 million rows</strong> and heavy traffic.Now the business requests a new column called current_salary with a default value of 0.<br> If you simply run:</p><pre>ALTER TABLE employees <br>ADD COLUMN current_salary INT NOT NULL DEFAULT 0;</pre><p>On many databases, this will trigger a <strong>full table rewrite</strong> to insert 0 into every row.<br> That means <strong>long locks, downtime, and possibly broken queries</strong>—the last thing any production system needs.</p><p>So how do we evolve schema safely, without downtime? Let’s walk through several approaches.</p><h3>1. Shadow Table + Backfill</h3><p>One approach is to create a <strong>shadow table</strong> that mirrors the production table but includes the new schema.</p><ul><li>The shadow table starts empty.</li><li>You run a <strong>backfill procedure</strong> that copies data from the production table in small, controlled batches.</li><li>Once the shadow table is in sync, you can <strong>switch over</strong> (via rename, view, or migration) with minimal disruption.</li></ul><p>This works well but may be complex to implement, especially with high write traffic.</p><h3>Example: Adding current_salary to Employees</h3><p>Instead of altering the huge Employees table directly:</p><ol><li><strong>Create a shadow table</strong> with the new schema:</li></ol><pre>CREATE TABLE employees_shadow (<br>    id BIGINT PRIMARY KEY,<br>    name TEXT NOT NULL,<br>    current_salary INT NOT NULL DEFAULT 0<br>    -- copy other columns as needed<br>);</pre><p><strong>Backfill data in batches</strong> (to avoid locks):</p><pre>INSERT INTO employees_shadow (id, name, current_salary)<br>SELECT id, name, 0<br>FROM employees<br>WHERE id BETWEEN 1 AND 100000;</pre><pre>-- repeat for the next batch until all rows are copied</pre><p>You can run this as a background job or scheduled task to copy rows in <strong>small chunks</strong> instead of rewriting 50M rows at once.</p><ol><li><strong>Keep shadow table in sync</strong>:</li></ol><ul><li>Use triggers or application-level dual-writes:<br> Whenever a new row is inserted/updated in employees, write the same change to employees_shadow.</li></ul><pre>CREATE TRIGGER sync_employee_insert<br>AFTER INSERT ON employees<br>FOR EACH ROW<br>INSERT INTO employees_shadow (id, name, current_salary)<br>VALUES (NEW.id, NEW.name, 0);</pre><ol><li><strong>Switch traffic</strong>:</li></ol><ul><li>Once the shadow table is fully backfilled and synced, rename tables (swap) or point the app to the shadow table.</li></ul><pre>ALTER TABLE employees RENAME TO employees_old;<br>ALTER TABLE employees_shadow RENAME TO employees;</pre><h3>Advantages</h3><ul><li>✅ <strong>Zero downtime</strong> (backfill happens gradually).</li><li>✅ <strong>Controlled migration</strong> (batch copy avoids big locks).</li><li>✅ <strong>Rollback friendly</strong> (old table still exists until you drop it).</li></ul><h3>2. One-to-One Table for New Data</h3><p>Another approach is to <strong>introduce a new table</strong> that stores the additional data (like salaries) in a one-to-one relationship with the Employees table.</p><ul><li>Only rows that actually need salary data are inserted.</li><li>This avoids storing millions of NULL or placeholder values.</li><li>Existing application logic can be adjusted to join against the new table when needed.</li></ul><p>This keeps the legacy table untouched while gradually introducing the new data model.</p><h3>One-to-One Table for New Data</h3><p>Sometimes, instead of altering a massive table directly (which can cause locking, rewrite costs, and downtime), you introduce a <strong>new table</strong> with a <strong>1:1 relationship</strong> to the original table.</p><p>This new table stores the new column(s), but <strong>only when needed</strong>.</p><h3>Example: Adding current_salary to Employees</h3><p>Instead of altering the Employees table (50M rows), create a new table:</p><pre>CREATE TABLE employee_salaries (<br>    employee_id BIGINT PRIMARY KEY,<br>    current_salary INT NOT NULL DEFAULT 0,<br>    FOREIGN KEY (employee_id) REFERENCES employees(id)<br>);</pre><h3>How it works</h3><p><strong>Employees table stays unchanged</strong> → no schema rewrite or downtime.</p><p><strong>New salary data goes to the new table</strong>:</p><p>If an employee has a salary → insert a row in employee_salaries.</p><p>If not → no row is needed.</p><p><strong>Reads</strong> can be handled by joining the two tables:</p><pre>SELECT e.id, e.name, COALESCE(s.current_salary, 0) AS current_salary<br>FROM employees e<br>LEFT JOIN employee_salaries s ON e.id = s.employee_id;</pre><h3>3. Using a View During Transition</h3><p>You can also use a <strong>database view</strong> as a temporary abstraction:</p><ul><li>Create a view that selects from the original table and exposes the new column (using a default value or computed expression).</li><li>Applications query the view instead of the base table.</li><li>Once the real column is fully backfilled and stable, you can drop the view and switch back to the table.</li></ul><p>This allows a smooth transition without exposing partial migrations to the application layer.</p><p>Let’s expand <strong>“Using a View During Transition”</strong> with a practical example.</p><h3>Problem</h3><p>You want to add a column current_salary with a default of 0 to a huge Employees table that already has <strong>millions of rows</strong>.</p><p>If you add it directly with NOT NULL DEFAULT 0, the DB might try to <strong>rewrite the whole table</strong>, which is risky.</p><h3>Step 1 — Add column as nullable (fast, metadata-only)</h3><pre>ALTER TABLE employees <br>ADD COLUMN current_salary INT NULL;</pre><p>At this point:</p><ul><li>Old rows → NULL in current_salary</li><li>New rows → application might write NULL if not provided</li></ul><h3>Step 2 — Create a View that applies the default</h3><p>Instead of making applications handle NULL everywhere, you can expose a <strong>view</strong> that replaces NULL with 0.</p><pre>CREATE VIEW employees_v AS<br>SELECT <br>    id,<br>    name,<br>    department,<br>    COALESCE(current_salary, 0) AS current_salary<br>FROM employees;</pre><p>Here:</p><ul><li>COALESCE(current_salary, 0) ensures any NULL values show as 0.</li><li>Applications query employees_v instead of employees.</li></ul><h3>Step 3 — Application uses the view</h3><p>Your app queries:</p><pre>SELECT id, name, current_salary FROM employees_v;</pre><p>Output:</p><p>id name current_salary 1 Alice 0 2 Bob 0 3 Charlie 5000</p><p>Even though in the real table Alice and Bob have NULL, the app <strong>always sees </strong><strong>0</strong>.</p><h3>Step 4 — Backfill in the background</h3><p>Run a background job to update old rows in small batches:</p><pre>UPDATE employees<br>SET current_salary = 0<br>WHERE current_salary IS NULL<br>LIMIT 10000;  -- batch size</pre><p>Repeat until all rows are updated.</p><h3>Step 5 — Final migration</h3><p>Once the backfill is complete:</p><pre>ALTER TABLE employees <br>ALTER COLUMN current_salary SET NOT NULL,<br>ALTER COLUMN current_salary SET DEFAULT 0;</pre><p>Then drop the view:</p><pre>DROP VIEW employees_v;</pre><p>Now the application can safely query the real table with the new column.</p><p>✅ <strong>Why is this useful?</strong></p><ul><li>No downtime: adding the column nullable is metadata-only.</li><li>The app sees consistent data (thanks to the view).</li><li>Background backfill happens without blocking queries.</li><li>Final migration is small and safe.</li></ul><h3>4. Database Engine Improvements</h3><p>Modern databases have improved schema migration performance significantly:</p><ul><li><strong>PostgreSQL ≥ 11</strong>: Adding a column with a default no longer rewrites the whole table. The default is stored in metadata:</li><li>New rows automatically get the default.</li><li>Old rows return the default on read, even if no physical value exists.</li><li><strong>MySQL ≥ 8 (Instant DDL)</strong>: Supports similar behavior for many schema changes.</li></ul><p>Older versions, however, still require a <strong>full rewrite</strong>, so always check your DB version.</p><h3>5. Application-Layer Default Handling</h3><p>Another option is to make the column <strong>nullable</strong> and let the <strong>application layer</strong> handle defaults:</p><ul><li>Add the new column as NULLABLE.</li><li>The application treats NULL as if it were the default value (e.g., 0).</li><li>Over time, you can backfill the data in batches.</li><li>Once complete, enforce NOT NULL with a migration.</li></ul><p>This reduces database-level locking and pushes some responsibility to the application.</p><h3>6. Online Schema Migration Tools</h3><p>For very large tables, you may need <strong>online migration tools</strong> such as:</p><ul><li><a href="https://www.percona.com/doc/percona-toolkit/pt-online-schema-change.html">pt-online-schema-change</a></li><li><a href="https://github.com/github/gh-ost">gh-ost</a></li><li><a href="https://reorg.github.io/pg_repack/">pg_repack</a></li></ul><p>These tools create a shadow copy of the table, migrate schema safely in the background, and switch with minimal downtime.</p><h3>Final Thoughts</h3><p>Schema migrations are not just about SQL commands — they are about <strong>operational safety</strong>.<br> When dealing with <strong>large production tables</strong>, blindly running ALTER TABLE can cause hours of downtime.</p><p>Instead, combine strategies:</p><ul><li>Shadow tables and backfill.</li><li>One-to-one extension tables.</li><li>Views for abstraction.</li><li>Application-layer defaults.</li><li>Modern DB features.</li><li>Online migration tools.</li></ul><p>The <strong>best solution depends</strong> on your database version, data size, and traffic profile.</p><blockquote><em>Zero-downtime schema migrations require careful planning, but with the right strategy, you can evolve your data model safely without bringing production down.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0bdc27d3ad40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Massive Models to Capability-Driven Design: A Guide to Better Software Architecture]]></title>
            <link>https://amrelsher07.medium.com/from-massive-models-to-capability-driven-design-a-guide-to-better-software-architecture-ad343c2caeb8?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad343c2caeb8</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Sat, 23 Aug 2025 15:01:36 GMT</pubDate>
            <atom:updated>2025-08-23T15:13:50.372Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>One of the most common architectural mistakes in software development is creating “massive models” that attempt to represent everything about a real-world entity in a single class. While this might seem logical at first — after all, there’s only one customer in the real world — it leads to tightly coupled, difficult-to-maintain systems that become increasingly fragile over time.</p><p>The key insight that transforms how we think about software design is this: <strong>“The same thing in the real world is often not the same thing in your system.”</strong> This article explores how to break free from massive models and embrace capability-driven design for more maintainable, scalable software architectures.</p><h3>The Problem: The Massive Model Anti-Pattern</h3><h3>What is a Massive Model?</h3><p>A massive model is a single class or entity that attempts to capture every piece of information and behavior related to a real-world concept across different business capabilities. These models become “god objects” that know and do too much.</p><h3>Example: The Massive Customer Model</h3><p>Consider this typical “Customer” class that tries to represent everything about a customer:</p><pre>// ❌ Anti-pattern: Massive Customer Model<br>public class Customer<br>{<br>    // Basic identity<br>    public int CustomerId { get; set; }<br>    public string Name { get; set; }<br>    public string Email { get; set; }<br>    public string PhoneNumber { get; set; }<br>    <br>    // Order management data<br>    public string ShippingAddress { get; set; }<br>    public string BillingAddress { get; set; }<br>    public List&lt;Order&gt; OrderHistory { get; set; }<br>    <br>    // CRM and analytics data<br>    public decimal TotalLifetimeValue { get; set; }<br>    public List&lt;Interaction&gt; CrmInteractions { get; set; }<br>    public List&lt;ContactPreference&gt; Preferences { get; set; }<br>    <br>    // Accounting data<br>    public List&lt;Invoice&gt; Invoices { get; set; }<br>    public List&lt;Payment&gt; Payments { get; set; }<br>    public string TaxId { get; set; }<br>    <br>    // Marketing data<br>    public List&lt;Campaign&gt; CampaignResponses { get; set; }<br>    public string CustomerSegment { get; set; }<br>    <br>    // ... potentially dozens more fields<br>}</pre><h3>The Hidden Costs of Massive Models</h3><p>This approach creates several critical problems:</p><ol><li><strong>High Coupling</strong>: Changes in one area affect unrelated functionalities</li><li><strong>Difficult Testing</strong>: Testing becomes complex when models have too many responsibilities</li><li><strong>Poor Scalability</strong>: Different teams can’t work independently on customer-related features</li><li><strong>Violation of Single Responsibility Principle</strong>: The model serves too many masters</li><li><strong>Database Performance Issues</strong>: Loading unnecessary data for simple operations</li></ol><h3>The Solution: Capability-Driven Models</h3><h3>Understanding Capabilities</h3><p>A capability represents a distinct business function or concern within your system. Instead of organizing models by real-world entities, we organize them by what the system needs to do.</p><p><strong>Key Principle</strong>: Models should be driven by capabilities, not purely by data.</p><h3>Decomposing the Massive Customer Model</h3><p>Let’s break down our massive Customer model into capability-driven models:</p><h4>1. Sales Capability — Customer Orders</h4><pre>// ✅ Sales capability: Focused on order processing<br>public class OrderCustomer<br>{<br>    public int CustomerId { get; set; }<br>    public string Name { get; set; }<br>    public string Email { get; set; }<br>    public string ShippingAddress { get; set; }<br>    public string BillingAddress { get; set; }<br>    public CustomerTier Tier { get; set; } // For pricing/discounts<br>    <br>    // Methods specific to order processing<br>    public bool IsEligibleForDiscount(decimal orderAmount) { /* ... */ }<br>    public Address GetPreferredShippingAddress() { /* ... */ }<br>}</pre><h4>2. CRM Capability — Customer Relationships</h4><pre>// ✅ CRM capability: Focused on customer relationship management<br>public class CrmContact<br>{<br>    public int ContactId { get; set; }<br>    public int CustomerId { get; set; } // Reference to sales customer<br>    public string Name { get; set; }<br>    public string Email { get; set; }<br>    public string PhoneNumber { get; set; }<br>    public List&lt;Interaction&gt; Interactions { get; set; }<br>    public List&lt;ContactPreference&gt; Preferences { get; set; }<br>    public LeadStatus Status { get; set; }<br>    public decimal EstimatedValue { get; set; }<br>    <br>    // Methods specific to CRM operations<br>    public void RecordInteraction(Interaction interaction) { /* ... */ }<br>    public bool ShouldContact(ContactReason reason) { /* ... */ }<br>}</pre><h4>3. Accounting Capability — Financial Records</h4><pre>// ✅ Accounting capability: Focused on financial transactions<br>public class AccountingCustomer<br>{<br>    public int AccountId { get; set; }<br>    public int CustomerId { get; set; } // Reference to sales customer<br>    public string LegalName { get; set; }<br>    public string TaxId { get; set; }<br>    public string BillingAddress { get; set; }<br>    public PaymentTerms Terms { get; set; }<br>    public decimal CreditLimit { get; set; }<br>    public List&lt;Invoice&gt; Invoices { get; set; }<br>    public List&lt;Payment&gt; Payments { get; set; }<br>    <br>    // Methods specific to accounting operations<br>    public decimal GetOutstandingBalance() { /* ... */ }<br>    public bool IsPaymentOverdue() { /* ... */ }<br>}</pre><h4>4. Analytics Capability — Customer Intelligence</h4><pre>// ✅ Analytics capability: Focused on customer insights<br>public class CustomerAnalytics<br>{<br>    public int CustomerId { get; set; }<br>    public decimal TotalLifetimeValue { get; set; }<br>    public decimal AverageOrderValue { get; set; }<br>    public int OrderFrequency { get; set; }<br>    public DateTime LastPurchaseDate { get; set; }<br>    public string CustomerSegment { get; set; }<br>    public List&lt;ProductCategory&gt; PreferredCategories { get; set; }<br>    <br>    // Methods specific to analytics<br>    public CustomerSegment CalculateSegment() { /* ... */ }<br>    public decimal PredictNextOrderValue() { /* ... */ }<br>}</pre><h3>Another Example: Breaking Down the Massive Order Model</h3><h3>The Problem: Order vs. Shipment Confusion</h3><p>A common mistake is treating an order and its shipment as the same entity:</p><pre>// ❌ Anti-pattern: Massive Order Model<br>public class Order<br>{<br>    // Order data<br>    public int OrderId { get; set; }<br>    public int CustomerId { get; set; }<br>    public DateTime OrderDate { get; set; }<br>    public List&lt;OrderItem&gt; Items { get; set; }<br>    public decimal TotalAmount { get; set; }<br>    <br>    // Shipment data (wrongly mixed in)<br>    public string TrackingNumber { get; set; }<br>    public string Carrier { get; set; }<br>    public string CurrentTrackingStatus { get; set; }<br>    public List&lt;WarehouseRoute&gt; RouteHistory { get; set; }<br>    public List&lt;string&gt; DeliveryAttempts { get; set; }<br>    public DateTime ExpectedDeliveryDate { get; set; }<br>}</pre><p><strong>The Reality</strong>: An order is not a shipment. A shipment is a complex logistics process that might involve multiple warehouses, carriers, and delivery attempts. It’s a completely different business capability.</p><h3>The Solution: Separate Order and Shipment Models</h3><h4>Sales Capability — Order Processing</h4><pre>// ✅ Sales capability: What the customer bought<br>public class Order<br>{<br>    public int OrderId { get; set; }<br>    public int CustomerId { get; set; }<br>    public DateTime OrderDate { get; set; }<br>    public List&lt;OrderItem&gt; Items { get; set; }<br>    public decimal TotalAmount { get; set; }<br>    public OrderStatus Status { get; set; }<br>    public string ShippingAddress { get; set; }<br>    <br>    // Methods specific to order processing<br>    public bool CanBeCancelled() { /* ... */ }<br>    public decimal CalculateTax() { /* ... */ }<br>    public void MarkAsProcessed() { /* ... */ }<br>}</pre><h4>Logistics Capability — Shipment Management</h4><pre>// ✅ Logistics capability: How items get to the customer<br>public class Shipment<br>{<br>    public int ShipmentId { get; set; }<br>    public int OrderId { get; set; } // Reference to the originating order<br>    public string TrackingNumber { get; set; }<br>    public string Carrier { get; set; }<br>    public ShipmentStatus Status { get; set; }<br>    public List&lt;ShipmentLeg&gt; Route { get; set; }<br>    public List&lt;DeliveryAttempt&gt; DeliveryAttempts { get; set; }<br>    public DateTime EstimatedDeliveryDate { get; set; }<br>    <br>    // Methods specific to logistics<br>    public void UpdateLocation(Location location) { /* ... */ }<br>    public bool IsDeliverable() { /* ... */ }<br>    public void RecordDeliveryAttempt(DeliveryAttempt attempt) { /* ... */ }<br>}</pre><h3>Benefits of Capability-Driven Design</h3><h3>1. Reduced Coupling</h3><p>Each model serves a single capability, reducing interdependencies between different business functions.</p><h3>2. Improved Testability</h3><p>Smaller, focused models are easier to test in isolation.</p><h3>3. Better Team Autonomy</h3><p>Different teams can work on different capabilities without stepping on each other’s toes.</p><h3>4. Enhanced Scalability</h3><p>Individual capabilities can be scaled independently based on their specific needs.</p><h3>5. Clearer Business Logic</h3><p>Each model contains only the data and behavior relevant to its specific capability.</p><h3>6. Easier Maintenance</h3><p>Changes in one capability don’t ripple through unrelated parts of the system.</p><h3>Implementation Guidelines</h3><h3>1. Identify Distinct Capabilities</h3><p>Ask yourself:</p><ul><li>What are the different business functions that use this entity?</li><li>What are the distinct contexts in which this entity appears?</li><li>What different teams or departments work with this entity?</li></ul><h3>2. Define Clear Boundaries</h3><p>Each capability-driven model should have:</p><ul><li>A single, well-defined responsibility</li><li>Its own data that’s relevant to its capability</li><li>Methods that operate within its boundary</li><li>Clear interfaces for communication with other capabilities</li></ul><h3>3. Manage Relationships Carefully</h3><pre>// Use references instead of embedding full objects<br>public class OrderCustomer<br>{<br>    public int CustomerId { get; set; }<br>    // Don&#39;t embed CrmContact or AccountingCustomer here<br>    <br>    // If you need data from other capabilities, use services<br>    public async Task&lt;CrmContact&gt; GetCrmContactAsync(ICrmService crmService)<br>    {<br>        return await crmService.GetContactByCustomerIdAsync(CustomerId);<br>    }<br>}</pre><h3>4. Use Domain Services for Cross-Capability Operations</h3><pre>public class CustomerService<br>{<br>    private readonly IOrderRepository _orderRepository;<br>    private readonly ICrmRepository _crmRepository;<br>    private readonly IAccountingRepository _accountingRepository;<br>    <br>    public async Task&lt;CustomerSummary&gt; GetCustomerSummaryAsync(int customerId)<br>    {<br>        var orderCustomer = await _orderRepository.GetCustomerAsync(customerId);<br>        var crmContact = await _crmRepository.GetContactAsync(customerId);<br>        var accountingCustomer = await _accountingRepository.GetCustomerAsync(customerId);<br>        <br>        return new CustomerSummary<br>        {<br>            // Aggregate data from different capabilities<br>            Name = orderCustomer.Name,<br>            LastInteraction = crmContact.LastInteractionDate,<br>            OutstandingBalance = accountingCustomer.GetOutstandingBalance()<br>        };<br>    }<br>}</pre><h3>Common Pitfalls and How to Avoid Them</h3><h3>1. Over-Fragmentation</h3><p>Don’t create separate models for every minor variation. Focus on genuinely distinct capabilities.</p><h3>2. Circular Dependencies</h3><p>Avoid models that depend on each other. Use references and services instead.</p><h3>3. Inconsistent Data</h3><p>Implement proper synchronization mechanisms when the same real-world entity appears in multiple models.</p><h3>4. Performance Concerns</h3><p>Cache frequently accessed cross-capability data, but don’t let performance concerns drive you back to massive models.</p><h3>Conclusion</h3><p>Breaking down massive models into capability-driven ones is a fundamental shift in how we think about software architecture. Instead of modeling our software after the physical world, we model it after what our software needs to do.</p><p>This approach leads to:</p><ul><li><strong>More maintainable code</strong> that’s easier to change</li><li><strong>Better team collaboration</strong> with clear ownership boundaries</li><li><strong>Improved system performance</strong> through focused, lightweight models</li><li><strong>Greater business agility</strong> with the ability to evolve capabilities independently</li></ul><p>Remember: <strong>Your models should be driven by capabilities, not purely by data.</strong> The same thing in the real world can and should be different things in your system, each optimized for its specific capability and context.</p><p>The next time you find yourself creating a model that “does everything,” step back and ask: “What distinct capabilities does this represent?” Then courageously break it apart. Your future self — and your team — will thank you for it.</p><h3>Further Reading</h3><ul><li>Domain-Driven Design by Eric Evans</li><li>Clean Architecture by Robert C. Martin</li><li>Building Microservices by Sam Newman</li><li>Patterns of Enterprise Application Architecture by Martin Fowler</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad343c2caeb8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Delete Behaviors in EF Core and How They Fit into Domain-Driven Design (DDD)]]></title>
            <link>https://amrelsher07.medium.com/delete-behaviors-in-ef-core-and-how-they-fit-into-domain-driven-design-ddd-084a36eb082b?source=rss-a45bdd28e1bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/084a36eb082b</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Amr elshaer]]></dc:creator>
            <pubDate>Mon, 19 May 2025 18:00:38 GMT</pubDate>
            <atom:updated>2025-05-19T18:00:38.257Z</atom:updated>
            <content:encoded><![CDATA[<h3>🧱 Delete Behaviors in EF Core and How They Fit into Domain-Driven Design (DDD)</h3><p>When using <strong>Entity Framework Core</strong> in combination with <strong>Domain-Driven Design (DDD)</strong>, it’s important to understand how <strong>delete behaviors</strong> work and how they should be aligned with your <strong>domain model</strong>. In this article, we explore how EF Core handles delete behaviors and how these options relate to the principles of DDD, especially when working with aggregates.</p><h3>🚀 Quick Primer: Delete Behaviors in EF Core</h3><p>EF Core provides several delete behaviors that determine what happens to related entities when a principal entity is deleted:</p><p>Delete Behavior Description Cascade Deletes dependent entities automatically when the principal is deleted Restrict Prevents deletion of the principal if dependents exist SetNull Sets the foreign key to null on dependents when the principal is deleted NoAction Similar to Restrict, but actual behavior depends on the database provider ClientSetNull Sets FK to null in-memory (for optional relationships only)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*6ehwpeB3nevlyodhHfiPGg.png" /></figure><p>These can be configured in the OnModelCreating method using Fluent API:</p><pre>modelBuilder.Entity&lt;Order&gt;()<br>    .HasMany(o =&gt; o.OrderItems)<br>    .WithOne(i =&gt; i.Order)<br>    .HasForeignKey(i =&gt; i.OrderId)<br>    .OnDelete(DeleteBehavior.Cascade);</pre><h3>🧭 A Refresher on Domain-Driven Design (DDD)</h3><p>In DDD, the primary building block is the <strong>Aggregate</strong>:</p><ul><li>An <strong>Aggregate</strong> is a cluster of domain objects treated as a single unit.</li><li>One entity in the aggregate is the <strong>Aggregate Root</strong>, and all changes must go through it.</li><li>The root enforces <strong>invariants</strong> and <strong>encapsulates</strong> the behavior of its children.</li></ul><p>Example:</p><pre>Aggregate: Order<br>- Order (Aggregate Root)<br>- OrderItems (Owned by Order)</pre><p>Deleting an Order should delete its OrderItems, but it should happen <strong>according to domain rules</strong>, not just via raw EF Core operations.</p><h3>🎯 DeleteBehavior.Cascade and DDD</h3><h3>✅ When to Use Cascade</h3><p>Use DeleteBehavior.Cascade <strong>only</strong> when:</p><ul><li>The child entity has <strong>no independent existence</strong> without the parent.</li><li>The child is <strong>fully encapsulated</strong> inside the aggregate.</li><li>There are <strong>no domain rules or side effects</strong> to consider during deletion.</li></ul><p>This matches well with DDD because:</p><ul><li>The aggregate root (Order) owns and controls the lifecycle of its children (OrderItems).</li><li>Deleting the root justifies deleting its parts.</li></ul><blockquote><strong><em>Rule of thumb</em></strong><em>: If the child is a </em>value object-like<em> entity (i.e., no lifecycle outside the parent), cascading deletion is acceptable.</em></blockquote><h3>⚠️ When Not to Use Cascade</h3><p>Avoid cascade deletion when:</p><ul><li>The child is <strong>shared</strong> by multiple aggregates.</li><li>You need to <strong>raise a domain event</strong>, log activity, or do some <strong>side effect</strong> during deletion.</li><li>You are using <strong>soft deletes</strong> or <strong>archival</strong> strategies.</li><li>You want to preserve the record for <strong>audit/history</strong>.</li></ul><p>In these cases, the domain logic should <strong>explicitly handle deletions</strong>. You may want to use DeleteBehavior.Restrict to <strong>prevent EF from auto-deleting</strong>.</p><pre>modelBuilder.Entity&lt;Invoice&gt;()<br>    .HasMany(i =&gt; i.Payments)<br>    .WithOne(p =&gt; p.Invoice)<br>    .OnDelete(DeleteBehavior.Restrict); // Enforces domain responsibility</pre><h3>🧠 DDD-Friendly Deletion Strategy</h3><p>Instead of relying on EF Core’s delete behavior, let the <strong>aggregate root</strong> control deletion:</p><pre>public class Order : AggregateRoot<br>{<br>    private readonly List&lt;OrderItem&gt; _items = new();</pre><pre>    public void Cancel()<br>    {<br>        if (_items.Any(i =&gt; i.IsShipped))<br>            throw new InvalidOperationException(&quot;Cannot delete an order with shipped items.&quot;);</pre><pre>        // Raise domain event<br>        this.AddDomainEvent(new OrderCancelledEvent(this.Id));<br>        <br>        _items.Clear(); // or mark as deleted<br>    }<br>}</pre><p>Then in your <strong>application layer</strong>, call a repository method:</p><pre>var order = _orderRepository.GetById(id);<br>order.Cancel();<br>_orderRepository.Delete(order);</pre><p>This respects DDD boundaries and keeps behavior within the domain.</p><h3>🧩 Soft Deletes and EF Core</h3><p>If your domain requires <strong>soft deletes</strong> (marking entities as deleted without actually removing them), cascading deletes won’t work as expected.</p><p>Use a IsDeleted flag, and configure EF Core to apply a global query filter:</p><pre>modelBuilder.Entity&lt;Order&gt;()<br>    .HasQueryFilter(o =&gt; !o.IsDeleted);</pre><p>Handle deletions in the domain model:</p><pre>public void MarkAsDeleted() =&gt; IsDeleted = true;</pre><h3>🔚 Conclusion</h3><p>Simple owned entities ✅ Cascade is fine</p><p>Complex domain logic ❌ Avoid cascade, use domain methods</p><p>Shared entities ❌ Avoid cascade, prefer Restrict</p><p>Soft deletes ❌ Avoid cascade, use IsDeleted flag</p><p>Domain events required ❌ Use explicit deletion in domain layer</p><p>By carefully choosing delete behaviors and aligning them with your domain model, you ensure consistency, maintainability, and true adherence to <strong>Domain-Driven Design</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=084a36eb082b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>