<?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>Forem: ABP.IO</title>
    <description>The latest articles on Forem by ABP.IO (@abp_io).</description>
    <link>https://forem.com/abp_io</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%2F3723489%2Ffed2d127-60d2-4db0-a199-b848ed38b079.png</url>
      <title>Forem: ABP.IO</title>
      <link>https://forem.com/abp_io</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abp_io"/>
    <language>en</language>
    <item>
      <title>Building Production-Ready LLM Applications with .NET: A Practical Guide</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Mon, 24 Nov 2025 07:33:03 +0000</pubDate>
      <link>https://forem.com/abp_io/building-production-ready-llm-applications-with-net-a-practical-guide-3a8i</link>
      <guid>https://forem.com/abp_io/building-production-ready-llm-applications-with-net-a-practical-guide-3a8i</guid>
      <description>&lt;h1&gt;
  
  
  Building Production-Ready LLM Applications with .NET: A Practical Guide
&lt;/h1&gt;

&lt;p&gt;Large Language Models (LLMs) have evolved rapidly, and integrating them into production .NET applications requires staying current with the latest approaches. In this article, I'll share practical tips and patterns I've learned while building LLM-powered systems, covering everything from API changes in GPT-5 to implementing efficient RAG (Retrieval Augmented Generation) architectures.&lt;/p&gt;

&lt;p&gt;Whether you're building a chatbot, a knowledge base assistant, or integrating AI into your enterprise applications, these production-tested insights will help you avoid common pitfalls and build more reliable systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Temperature Paradigm Shift: GPT-5 Changes Everything
&lt;/h2&gt;

&lt;p&gt;If you've been working with GPT-4 or earlier models, you're familiar with the &lt;code&gt;temperature&lt;/code&gt; and &lt;code&gt;top_p&lt;/code&gt; parameters for controlling response randomness. &lt;strong&gt;Here's the critical update&lt;/strong&gt; : GPT-5 no longer supports these parameters!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way (GPT-4)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var chatRequest = new ChatOptions
{
    Temperature = 0.7, // ✅ Worked with GPT-4
    TopP = 0.9 // ✅ Worked with GPT-4
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The New Way (GPT-5)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var chatRequest = new ChatOptions
{
    RawRepresentationFactory = (client =&amp;gt; new ChatCompletionOptions()
    {
#pragma warning disable OPENAI001
        ReasoningEffortLevel = "minimal",
#pragma warning restore OPENAI001
    })
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why the change?&lt;/strong&gt; GPT-5 incorporates an internal reasoning and verification process. Instead of controlling randomness, you now specify how much computational effort the model should invest in reasoning through the problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Freasoning-effort-diagram.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Freasoning-effort-diagram.svg" alt="Reasoning Effort Levels" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing the Right Reasoning Level
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Low&lt;/strong&gt; : Quick responses for simple queries (e.g., "What's the capital of France?")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Medium&lt;/strong&gt; : Balanced approach for most use cases&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High&lt;/strong&gt; : Complex reasoning tasks (e.g., code generation, multi-step problem solving)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pro Tip&lt;/strong&gt; : Reasoning tokens are included in your API costs. Use "High" only when necessary to optimize your budget.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  System Prompts: The "Lost in the Middle" Problem
&lt;/h2&gt;

&lt;p&gt;Here's a critical insight that can save you hours of debugging: &lt;strong&gt;Important rules must be repeated at the END of your prompt!&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ What Doesn't Work
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You are a helpful assistant.
RULE: Never share passwords or sensitive information.

[User Input]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ✅ What Actually Works
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You are a helpful assistant.
RULE: Never share passwords or sensitive information.

[User Input]

⚠️ REMINDER: Apply the rules above strictly, ESPECIALLY regarding passwords.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; LLMs suffer from the "Lost in the Middle" phenomenon—they pay more attention to the beginning and end of the context window. Critical instructions buried in the middle are often ignored.&lt;/p&gt;

&lt;h2&gt;
  
  
  RAG Architecture: The Parent-Child Pattern
&lt;/h2&gt;

&lt;p&gt;Retrieval Augmented Generation (RAG) is essential for grounding LLM responses in your own data. The most effective pattern I've found is the &lt;strong&gt;Parent-Child approach&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Frag-parent-child.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Frag-parent-child.svg" alt="RAG Parent-Child Architecture" width="1000" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Split documents into hierarchies&lt;/strong&gt; :&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Store both in vector database&lt;/strong&gt; with references&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Query flow&lt;/strong&gt; :&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Overlap Strategy
&lt;/h3&gt;

&lt;p&gt;Always use overlapping chunks to prevent information loss at boundaries!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chunk 1: Token 0-500
Chunk 2: Token 400-900 ← 100 token overlap
Chunk 3: Token 800-1300 ← 100 token overlap

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Standard recommendation&lt;/strong&gt; : 10-20% overlap (for 500 tokens, use 50-100 token overlap)&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation with Semantic Kernel
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.SemanticKernel.Text;

var chunks = TextChunker.SplitPlainTextParagraphs(
    documentText, 
    maxTokensPerParagraph: 500,
    overlapTokens: 50
);

foreach (var chunk in chunks)
{
    var embedding = await embeddingService.GenerateEmbeddingAsync(chunk);
    await vectorDb.StoreAsync(chunk, embedding);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PostgreSQL + pgvector: The Pragmatic Choice
&lt;/h2&gt;

&lt;p&gt;For .NET developers, choosing a vector database can be overwhelming. After evaluating multiple options, &lt;strong&gt;PostgreSQL with pgvector&lt;/strong&gt; is the most practical choice for most scenarios.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fpgvector-integration.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fpgvector-integration.svg" alt="pgvector Integration" width="950" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why pgvector?
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;Use existing SQL knowledge&lt;/strong&gt; - No new query language to learn&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;EF Core integration&lt;/strong&gt; - Works with your existing data access layer&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;JOIN with metadata&lt;/strong&gt; - Combine vector search with traditional queries&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;WHERE clause filtering&lt;/strong&gt; - Filter by tenant, user, date, etc.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;ACID compliance&lt;/strong&gt; - Transaction support for data consistency&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;No separate infrastructure&lt;/strong&gt; - One database for everything&lt;/p&gt;
&lt;h3&gt;
  
  
  Setting Up pgvector with EF Core
&lt;/h3&gt;

&lt;p&gt;First, install the NuGet package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package Pgvector.EntityFrameworkCore

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define your entity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Pgvector;
using Pgvector.EntityFrameworkCore;

public class DocumentChunk
{
    public Guid Id { get; set; }
    public string Content { get; set; }
    public Vector Embedding { get; set; } // 👈 pgvector type
    public Guid ParentChunkId { get; set; }
    public DateTime CreatedAt { get; set; }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure in DbContext:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected override void OnModelCreating(ModelBuilder builder)
{
    builder.HasPostgresExtension("vector");

    builder.Entity&amp;lt;DocumentChunk&amp;gt;()
        .Property(e =&amp;gt; e.Embedding)
        .HasColumnType("vector(1536)"); // 👈 OpenAI embedding dimension

    builder.Entity&amp;lt;DocumentChunk&amp;gt;()
        .HasIndex(e =&amp;gt; e.Embedding)
        .HasMethod("hnsw") // 👈 Fast approximate search
        .HasOperators("vector_cosine_ops");
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Performing Vector Search
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Pgvector.EntityFrameworkCore;

public async Task&amp;lt;List&amp;lt;DocumentChunk&amp;gt;&amp;gt; SearchAsync(string query)
{
    // 1. Convert query to embedding
    var queryVector = await _embeddingService.GetEmbeddingAsync(query);

    // 2. Search
    return await _context.DocumentChunks
        .OrderBy(c =&amp;gt; c.Embedding.L2Distance(queryVector)) // 👈 Lower is better
        .Take(5)
        .ToListAsync();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Source&lt;/strong&gt; : &lt;a href="https://github.com/pgvector/pgvector-dotnet?tab=readme-ov-file#entity-framework-core" rel="noopener noreferrer"&gt;Pgvector.NET on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Smart Tool Usage: Make RAG a Tool, Not a Tax
&lt;/h2&gt;

&lt;p&gt;A common mistake is calling RAG on every single user message. This wastes tokens and money. Instead, &lt;strong&gt;make RAG a tool&lt;/strong&gt; and let the LLM decide when to use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Expensive Approach
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Always call RAG, even for "Hello"
var context = await PerformRAG(userMessage);
var response = await chatClient.CompleteAsync($"{context}\n\n{userMessage}");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ✅ Smart Approach
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[KernelFunction]
[Description("Search the company knowledge base for information")]
public async Task&amp;lt;string&amp;gt; SearchKnowledgeBase(
    [Description("The search query")] string query)
{
    var results = await _vectorDb.SearchAsync(query);
    return string.Join("\n---\n", results.Select(r =&amp;gt; r.Content));
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The LLM will call &lt;code&gt;SearchKnowledgeBase&lt;/code&gt; only when needed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Hello" → No tool call&lt;/li&gt;
&lt;li&gt;"What was our 2024 revenue?" → Calls tool&lt;/li&gt;
&lt;li&gt;"Tell me a joke" → No tool call&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Multilingual RAG: Query Translation Strategy
&lt;/h2&gt;

&lt;p&gt;When your documents are in one language (e.g., English) but users query in another (e.g., Turkish), you need a translation strategy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fmultilingual-rag.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fmultilingual-rag.svg" alt="Multilingual RAG Architecture" width="1000" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution Options
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Option 1&lt;/strong&gt; : Use an LLM that automatically calls tools in English&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Many modern LLMs can do this if properly instructed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Option 2&lt;/strong&gt; : Tool chain approach&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[KernelFunction]
[Description("Translate text to English")]
public async Task&amp;lt;string&amp;gt; TranslateToEnglish(string text)
{
    // Translation logic
}

[KernelFunction]
[Description("Search knowledge base (English only)")]
public async Task&amp;lt;string&amp;gt; SearchKnowledgeBase(string englishQuery)
{
    // Search logic
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The LLM will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Call &lt;code&gt;TranslateToEnglish("2024 geliri nedir?")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Get "What was 2024 revenue?"&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;SearchKnowledgeBase("What was 2024 revenue?")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return results and respond in Turkish&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Model Context Protocol (MCP): Beyond In-Process Tools
&lt;/h2&gt;

&lt;p&gt;Microsoft and Anthropic recently released official C# SDKs for the Model Context Protocol (MCP). This is a game-changer for tool reusability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fmcp-architecture.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fmcp-architecture.svg" alt="MCP Architecture" width="900" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  MCP vs. Semantic Kernel Plugins
&lt;/h3&gt;

&lt;p&gt;| Feature | SK Plugins | MCP Servers | |---------|-----------|-------------| | &lt;strong&gt;Process&lt;/strong&gt; | In-process | Out-of-process (stdio/http) | | &lt;strong&gt;Reusability&lt;/strong&gt; | Application-specific | Cross-application | | &lt;strong&gt;Examples&lt;/strong&gt; | Used within your app | VS Code Copilot, Claude Desktop |&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an MCP Server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Extensions.Hosting;

var builder = Host.CreateEmptyApplicationBuilder(settings: null);

builder.Services.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();

await builder.Build().RunAsync();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define your tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[McpServerToolType]
public static class FileSystemTools
{
    [McpServerTool, Description("Read a file from the file system")]
    public static async Task&amp;lt;string&amp;gt; ReadFile(string path)
    {
        // ⚠️ SECURITY: Always validate paths!
        if (!IsPathSafe(path)) 
            throw new SecurityException("Invalid path");

        return await File.ReadAllTextAsync(path);
    }

    private static bool IsPathSafe(string path)
    {
        // Implement path traversal prevention
        var fullPath = Path.GetFullPath(path);
        return fullPath.StartsWith(AllowedDirectory);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your MCP server can now be used by VS Code Copilot, Claude Desktop, or any other MCP client!&lt;/p&gt;

&lt;h2&gt;
  
  
  Chat History Management: Truncation + RAG Hybrid
&lt;/h2&gt;

&lt;p&gt;For long conversations, storing all history in the context window becomes impractical. Here's the pattern that works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fchat-history-hybrid.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fchat-history-hybrid.svg" alt="Chat History Hybrid Strategy" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Lossy Approach
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;First 50 messages → Summarize with LLM → Single summary message

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt; : Detail loss (fidelity loss)&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Hybrid Approach
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Recent messages&lt;/strong&gt; (last 5-10): Keep in prompt for immediate context&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Older messages&lt;/strong&gt; : Store in vector database as a tool
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[KernelFunction]
[Description("Search conversation history for past discussions")]
public async Task&amp;lt;string&amp;gt; SearchChatHistory(
    [Description("What to search for")] string query)
{
    var relevantMessages = await _vectorDb.SearchAsync(query);
    return string.Join("\n", relevantMessages.Select(m =&amp;gt; 
        $"[{m.Timestamp}] {m.Role}: {m.Content}"));
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The LLM retrieves only relevant past context when needed, avoiding summary-induced information loss.&lt;/p&gt;

&lt;h2&gt;
  
  
  RAG vs. Fine-Tuning: Choose Wisely
&lt;/h2&gt;

&lt;p&gt;A common misconception is using fine-tuning for knowledge injection. Here's when to use each:&lt;/p&gt;

&lt;p&gt;| Purpose | RAG | Fine-Tuning | |---------|-----|-------------| | &lt;strong&gt;Goal&lt;/strong&gt; | Memory (provide facts) | Behavior (teach style) | | &lt;strong&gt;Updates&lt;/strong&gt; | Dynamic (add docs anytime) | Static (requires retraining) | | &lt;strong&gt;Cost&lt;/strong&gt; | Low dev, higher inference | High dev, lower inference | | &lt;strong&gt;Hallucination&lt;/strong&gt; | Reduces | Doesn't reduce | | &lt;strong&gt;Use Case&lt;/strong&gt; | Company docs, FAQs | Brand voice, specific format |&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common mistake&lt;/strong&gt; : "Let's fine-tune on our company documents" ❌&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Better approach&lt;/strong&gt; : Use RAG! ✅&lt;/p&gt;

&lt;p&gt;Fine-tuning is for teaching the model &lt;em&gt;how&lt;/em&gt; to respond, not &lt;em&gt;what&lt;/em&gt; to know.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source&lt;/strong&gt; : &lt;a href="https://www.oracle.com/artificial-intelligence/generative-ai/retrieval-augmented-generation-rag/rag-fine-tuning/" rel="noopener noreferrer"&gt;Oracle - RAG vs Fine-Tuning&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Why SVG is Superior for LLM-Generated Images
&lt;/h2&gt;

&lt;p&gt;When using LLMs to generate diagrams and visualizations, always request SVG format instead of PNG or JPG.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why SVG?
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;Text-based&lt;/strong&gt; → LLMs produce better results&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Lower cost&lt;/strong&gt; → Fewer tokens than base64-encoded images&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Editable&lt;/strong&gt; → Easy to modify after generation&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Scalable&lt;/strong&gt; → Perfect quality at any size&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Version control friendly&lt;/strong&gt; → Works great in Git&lt;/p&gt;

&lt;h3&gt;
  
  
  Example Prompt
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Create an architecture diagram showing PostgreSQL with pgvector integration.
Format: SVG, 800x400 pixels. Show: .NET Application → EF Core → PostgreSQL → Vector Search.
Use arrows to connect stages. Color scheme: Blue tones.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fsvg-diagram-example.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-22-building-production-ready-llm-applications%2Fimages%2Fsvg-diagram-example.svg" alt="SVG Diagram Example" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All diagrams in this article were generated as SVG, resulting in excellent quality and lower token costs!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pro Tip&lt;/strong&gt; : If you don't need photographs or complex renders, always choose SVG.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Architecture Roadmap: Putting It All Together
&lt;/h2&gt;

&lt;p&gt;Here's the recommended stack for building production LLM applications with .NET:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Orchestration&lt;/strong&gt; : Microsoft.Extensions.AI + Semantic Kernel (when needed)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vector Database&lt;/strong&gt; : PostgreSQL + Pgvector.EntityFrameworkCore&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAG Pattern&lt;/strong&gt; : Parent-Child chunks with 10-20% overlap&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools&lt;/strong&gt; : MCP servers for reusability&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reasoning&lt;/strong&gt; : ReasoningEffortLevel instead of temperature&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompting&lt;/strong&gt; : Critical rules at the end&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost Optimization&lt;/strong&gt; : Make RAG a tool, not automatic&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Let me summarize the most important production tips:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Temperature is gone&lt;/strong&gt; → Use &lt;code&gt;ReasoningEffortLevel&lt;/code&gt; with GPT-5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rules at the end&lt;/strong&gt; → Combat "Lost in the Middle"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAG as a tool&lt;/strong&gt; → Reduce costs significantly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parent-Child pattern&lt;/strong&gt; → Search small, respond with large&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always use overlap&lt;/strong&gt; → 10-20% is the standard&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pgvector for most cases&lt;/strong&gt; → Unless you have billions of vectors&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MCP for reusability&lt;/strong&gt; → One codebase, works everywhere&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SVG for diagrams&lt;/strong&gt; → Better results, lower cost&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hybrid chat history&lt;/strong&gt; → Recent in prompt, old in vector DB&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAG &amp;gt; Fine-tuning&lt;/strong&gt; → For knowledge, not behavior&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>applicationdevelopme</category>
      <category>postgres</category>
      <category>llms</category>
      <category>mcp</category>
    </item>
    <item>
      <title>Announcing Server-Side Rendering Support for ABP Framework Angular Applications</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Thu, 20 Nov 2025 06:46:59 +0000</pubDate>
      <link>https://forem.com/abp_io/announcing-server-side-rendering-support-for-abp-framework-angular-applications-19hm</link>
      <guid>https://forem.com/abp_io/announcing-server-side-rendering-support-for-abp-framework-angular-applications-19hm</guid>
      <description>&lt;h1&gt;
  
  
  Announcing Server-Side Rendering (SSR) Support for ABP Framework Angular Applications
&lt;/h1&gt;

&lt;p&gt;We are pleased to announce that &lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt; has become available for ABP Framework Angular applications! This highly requested feature brings major gains in performance, SEO, and user experience to your Angular applications based on ABP Framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Server-Side Rendering (SSR)?
&lt;/h2&gt;

&lt;p&gt;Server-Side Rendering refers to an approach which renders your Angular application on the server as opposed to the browser. The server creates the complete HTML for a page and sends it to the client, which can then show the page to the user. This poses many advantages over traditional client-side rendering.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why SSR Matters for ABP Angular Applications
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Improved Performance
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Quicker visualization of the first contentful paint (FCP)&lt;/strong&gt;: Because prerendered HTML is sent over from the server, users will see content quicker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better perceived performance&lt;/strong&gt; : Even on slower devices, the page will be displaying something sooner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Less JavaScript parsing time&lt;/strong&gt; : For example, the initial page load will not require parsing and executing a large bundle of JavaScript.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Enhanced SEO
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improved indexing by search engines&lt;/strong&gt; : Search engine bots are able to crawl and index your content quicker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved rankings in search&lt;/strong&gt; : The quicker the content loads and the easier it is to access, the better your SEO score.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preview when sharing on social channels&lt;/strong&gt; : Rich previews with the appropriate meta tags are generated when sharing links on social platforms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Better User Experience
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Support for low bandwidth&lt;/strong&gt; : Users with slower Internet connections will have a better experience&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Progressive enhancement&lt;/strong&gt; : Users can start accessing the content before JavaScript has loaded&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better accessibility&lt;/strong&gt; : Screen readers and other assistive technologies can access the content immediately&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started with SSR
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Adding SSR to an Existing Project
&lt;/h3&gt;

&lt;p&gt;You can easily add SSR support to your existing ABP Angular application using the Angular CLI with ABP schematics:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Adds SSR configuration to your project&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ng generate @abp/ng.schematics:ssr-add

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Short form&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ng g @abp/ng.schematics:ssr-add

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have multiple projects in your workspace, you can specify which project to add SSR to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ng g @abp/ng.schematics:ssr-add &lt;span class="nt"&gt;--project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;my-project

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to skip the automatic installation of dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ng g @abp/ng.schematics:ssr-add &lt;span class="nt"&gt;--skip-install&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Gets Configured
&lt;/h2&gt;

&lt;p&gt;When you add SSR to your ABP Angular project, the schematic automatically:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Installs necessary dependencies&lt;/strong&gt; : Adds &lt;code&gt;@angular/ssr&lt;/code&gt; and related packages&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creates Server Configuration&lt;/strong&gt; : Creates &lt;code&gt;server.ts&lt;/code&gt; and related files&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updates Project Structure&lt;/strong&gt; :

&lt;ul&gt;
&lt;li&gt;Creates &lt;code&gt;main.server.ts&lt;/code&gt; to bootstrap the server&lt;/li&gt;
&lt;li&gt;Adds &lt;code&gt;app.config.server.ts&lt;/code&gt; for standalone apps (or &lt;code&gt;app.module.server.ts&lt;/code&gt; for NgModule apps)&lt;/li&gt;
&lt;li&gt;Configures server routes in &lt;code&gt;app.routes.server.ts&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updates Build Configuration&lt;/strong&gt; : updates &lt;code&gt;angular.json&lt;/code&gt; to include:

&lt;ul&gt;
&lt;li&gt;a &lt;code&gt;serve-ssr&lt;/code&gt; target for local SSR development&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;prerender&lt;/code&gt; target for static site generation&lt;/li&gt;
&lt;li&gt;Proper output paths for browser and server bundles&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Supported Configurations
&lt;/h2&gt;

&lt;p&gt;The ABP SSR schematic supports both modern and legacy Angular build configurations:&lt;/p&gt;

&lt;h3&gt;
  
  
  Application Builder (Suggested)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The new &lt;code&gt;@angular-devkit/build-angular:application&lt;/code&gt; builder&lt;/li&gt;
&lt;li&gt;Optimized for Angular 17+ apps&lt;/li&gt;
&lt;li&gt;Enhanced performance and smaller bundle sizes&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Server Builder (Legacy)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The original &lt;code&gt;@angular-devkit/build-angular:server&lt;/code&gt; builder&lt;/li&gt;
&lt;li&gt;Designed for legacy Angular applications&lt;/li&gt;
&lt;li&gt;Compatible with legacy applications&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Running Your SSR Application
&lt;/h2&gt;

&lt;p&gt;After adding SSR to your project, you can run your application in SSR mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Development mode with SSR&lt;/span&gt;
ng serve

&lt;span class="c"&gt;# Or specifically target SSR development server&lt;/span&gt;
npm run serve:ssr

&lt;span class="c"&gt;# Build for production&lt;/span&gt;
npm run build:ssr

&lt;span class="c"&gt;# Preview production build&lt;/span&gt;
npm run serve:ssr:production

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Important Considerations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Browser-Only APIs
&lt;/h3&gt;

&lt;p&gt;Some browser APIs are not available on the server. Use platform checks to conditionally execute code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;isPlatformBrowser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PLATFORM_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inject&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;platformId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PLATFORM_ID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isPlatformBrowser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;platformId&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Code that uses browser-only APIs&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Running in browser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Storage APIs
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;localStorage&lt;/code&gt; and &lt;code&gt;sessionStorage&lt;/code&gt; are not accessible on the server. Consider using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cookies for server-accessible data.&lt;/li&gt;
&lt;li&gt;The state transfer API for hydration.&lt;/li&gt;
&lt;li&gt;ABP's built-in storage abstractions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Third-Party Libraries
&lt;/h3&gt;

&lt;p&gt;Please ensure that any third-party libraries you use are compatible with SSR. These libraries can require:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic imports for browser-only code.&lt;/li&gt;
&lt;li&gt;Platform-specific service providers.&lt;/li&gt;
&lt;li&gt;Custom Angular Universal integration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ABP Framework Integration
&lt;/h2&gt;

&lt;p&gt;The SSR implementation is natively integrated with all of the ABP Framework features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication &amp;amp; Authorization&lt;/strong&gt; : The OAuth/OpenID Connect flow functions seamlessly with ABP&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-tenancy&lt;/strong&gt; : Fully supports tenant resolution and switching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Localization&lt;/strong&gt; : Server-side rendering respects the locale&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Permission Management&lt;/strong&gt; : Permission checks work on both server and client&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration&lt;/strong&gt; : The ABP configuration system is SSR-ready&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Performance Tips
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Utilize State Transfer&lt;/strong&gt; : Send data from server to client to eliminate redundant HTTP requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimize Images&lt;/strong&gt; : Proper image loading strategies, such as lazy loading and responsive images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache API Responses&lt;/strong&gt; : At the server, implement proper caching strategies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor Bundle Size&lt;/strong&gt; : Keep your server bundle optimized&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Prerendering&lt;/strong&gt; : The prerender target should be used for static content.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Server-side rendering can be a very effective feature in improving your ABP Angular application's performance, SEO, and user experience. Our new SSR schematic will make it easier than ever to add SSR to your project.&lt;/p&gt;

&lt;p&gt;Try it today and let us know what you think!&lt;/p&gt;




</description>
      <category>angular</category>
      <category>abp</category>
      <category>prerendering</category>
      <category>abpframework</category>
    </item>
    <item>
      <title>ABP.IO Platform 10.0 Final Has Been Released!</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Wed, 19 Nov 2025 10:56:04 +0000</pubDate>
      <link>https://forem.com/abp_io/abpio-platform-100-final-has-been-released-3mc8</link>
      <guid>https://forem.com/abp_io/abpio-platform-100-final-has-been-released-3mc8</guid>
      <description>&lt;h1&gt;
  
  
  ABP.IO Platform 10.0 Final Has Been Released!
&lt;/h1&gt;

&lt;p&gt;We are glad to announce that &lt;a href="https://abp.io/" rel="noopener noreferrer"&gt;ABP&lt;/a&gt; 10.0 stable version has been released today.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's New With Version 10.0?
&lt;/h2&gt;

&lt;p&gt;All the new features were explained in detail in the &lt;a href="https://abp.io/community/announcements/announcing-abp-10-0-release-candidate-86lrnyox" rel="noopener noreferrer"&gt;10.0 RC Announcement Post&lt;/a&gt;, so there is no need to review them again. You can check it out for more details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with 10.0
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How to Upgrade an Existing Solution
&lt;/h3&gt;

&lt;p&gt;You can upgrade your existing solutions with either ABP Studio or ABP CLI. In the following sections, both approaches are explained:&lt;/p&gt;

&lt;h3&gt;
  
  
  Upgrading via ABP Studio
&lt;/h3&gt;

&lt;p&gt;If you are already using the ABP Studio, you can upgrade it to the latest version. ABP Studio periodically checks for updates in the background, and when a new version of ABP Studio is available, you will be notified through a modal. Then, you can update it by confirming the opened modal. See &lt;a href="https://abp.io/docs/latest/studio/installation#upgrading" rel="noopener noreferrer"&gt;the documentation&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;After upgrading the ABP Studio, then you can open your solution in the application, and simply click the &lt;strong&gt;Upgrade ABP Packages&lt;/strong&gt; action button to instantly upgrade your solution:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5cf5n3s6pjhd64sa96ru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5cf5n3s6pjhd64sa96ru.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Upgrading via ABP CLI
&lt;/h3&gt;

&lt;p&gt;Alternatively, you can upgrade your existing solution via ABP CLI. First, you need to install the ABP CLI or upgrade it to the latest version.&lt;/p&gt;

&lt;p&gt;If you haven't installed it yet, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet tool &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; Volo.Abp.Studio.Cli

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or to update the existing CLI, you can run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet tool update &lt;span class="nt"&gt;-g&lt;/span&gt; Volo.Abp.Studio.Cli

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing/updating the ABP CLI, you can use the &lt;a href="https://abp.io/docs/latest/CLI#update" rel="noopener noreferrer"&gt;&lt;code&gt;update&lt;/code&gt; command&lt;/a&gt; to update all the ABP related NuGet and NPM packages in your solution as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;abp update

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can run this command in the root folder of your solution to update all ABP related packages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Migration Guides
&lt;/h2&gt;

&lt;p&gt;There are a few breaking changes in this version that may affect your application. Please read the migration guide carefully, if you are upgrading from v9.x: &lt;a href="https://abp.io/docs/10.0/release-info/migration-guides/abp-10-0" rel="noopener noreferrer"&gt;ABP Version 10.0 Migration Guide&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Community News
&lt;/h2&gt;

&lt;h3&gt;
  
  
  New ABP Community Articles
&lt;/h3&gt;

&lt;p&gt;As always, exciting articles have been contributed by the ABP community. I will highlight some of them here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://abp.io/community/members/alper" rel="noopener noreferrer"&gt;Alper Ebiçoğlu&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/optimize-your-dotnet-app-for-production-for-any-.net-app-wa24j28e" rel="noopener noreferrer"&gt;Optimize your .NET app for production Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/optimize-your-dotnet-app-for-production-for-any-.net-app-2-78xgncpi" rel="noopener noreferrer"&gt;Optimize your .NET app for production Part 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/return-code-vs-exceptions-which-one-is-better-1rwcu9yi" rel="noopener noreferrer"&gt;Return Code vs Exceptions: Which One is Better?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/sumeyye.kurtulus" rel="noopener noreferrer"&gt;Sumeyye Kurtulus&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/building-scalable-angular-apps-with-reusable-ui-components-b9npiff3" rel="noopener noreferrer"&gt;Building Scalable Angular Apps with Reusable UI Components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/angular-library-linking-made-easy-paths-workspaces-and-5z2ate6e" rel="noopener noreferrer"&gt;Angular Library Linking Made Easy: Paths, Workspaces and Symlinks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/erdem.caygor" rel="noopener noreferrer"&gt;erdem çaygör&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/building-dynamic-forms-in-angular-for-enterprise-6r3ewpxt" rel="noopener noreferrer"&gt;Building Dynamic Forms in Angular for Enterprise&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/from-server-to-browser-angular-transferstate-explained-m99zf8oh" rel="noopener noreferrer"&gt;From Server to Browser: Angular TransferState Explained&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/mansur.besleney" rel="noopener noreferrer"&gt;Mansur Besleney&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/top-10-exception-handling-mistakes-in-net-jhm8wzvg" rel="noopener noreferrer"&gt;Top 10 Exception Handling Mistakes in .NET&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/berkansasmaz" rel="noopener noreferrer"&gt;Berkan Şaşmaz&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/how-to-dynamically-set-the-connection-string-in-ef-core-30k87fpj" rel="noopener noreferrer"&gt;How to Dynamically Set the Connection String in EF Core&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/oguzhan.agir" rel="noopener noreferrer"&gt;Oğuzhan Ağır&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/the-asp.net-core-dependency-injection-system-3vbsdhq8" rel="noopener noreferrer"&gt;The ASP.NET Core Dependency Injection System&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/selmankoc" rel="noopener noreferrer"&gt;Selman Koç&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/5-things-keep-in-mind-when-deploying-clustered-environment-i9byusnv" rel="noopener noreferrer"&gt;5 Things Keep in Mind When Deploying Clustered Environment&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/m.aliozkaya" rel="noopener noreferrer"&gt;Muhammet Ali ÖZKAYA&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/repository-pattern-in-asp.net-core-2dudlg3j" rel="noopener noreferrer"&gt;Repository Pattern in ASP.NET Core&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/armagan" rel="noopener noreferrer"&gt;Armağan Ünlü&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/UI-UX-Trends-That-Will-Shape-2026-bx4c2kow" rel="noopener noreferrer"&gt;UI/UX Trends That Will Shape 2026&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/salih" rel="noopener noreferrer"&gt;Salih&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/what-is-that-domain-service-in-ddd-for-.net-developers-uqnpwjja" rel="noopener noreferrer"&gt;What is That Domain Service in DDD for .NET Developers?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/building-an-api-key-management-system-with-abp-framework-28gn4efw" rel="noopener noreferrer"&gt;Building an API Key Management System with ABP Framework&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://abp.io/community/members/fahrigedik" rel="noopener noreferrer"&gt;Fahri Gedik&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://abp.io/community/articles/signal-based-forms-in-angular-21-9qentsqs" rel="noopener noreferrer"&gt;Signal-Based Forms in Angular&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Thanks to the ABP Community for all the content they have published. You can also &lt;a href="https://abp.io/community/posts/create" rel="noopener noreferrer"&gt;post your ABP related (text or video) content&lt;/a&gt; to the ABP Community.&lt;/p&gt;

&lt;h2&gt;
  
  
  About the Next Version
&lt;/h2&gt;

&lt;p&gt;The next feature version will be 10.1. You can follow the &lt;a href="https://github.com/abpframework/abp/milestones" rel="noopener noreferrer"&gt;release planning here&lt;/a&gt;. Please &lt;a href="https://github.com/abpframework/abp/issues/new" rel="noopener noreferrer"&gt;submit an issue&lt;/a&gt; if you have any problems with this version.&lt;/p&gt;

</description>
      <category>abp</category>
      <category>release</category>
      <category>abpio</category>
      <category>abpframework</category>
    </item>
    <item>
      <title>Signal-Based Forms in Angular 21: Why You’ll Never Miss Reactive Forms Again</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Tue, 18 Nov 2025 08:32:44 +0000</pubDate>
      <link>https://forem.com/abp_io/signal-based-forms-in-angular-21-why-youll-never-miss-reactive-forms-again-n7l</link>
      <guid>https://forem.com/abp_io/signal-based-forms-in-angular-21-why-youll-never-miss-reactive-forms-again-n7l</guid>
      <description>&lt;h1&gt;
  
  
  Signal-Based Forms in Angular 21: Why You’ll Never Miss Reactive Forms Again
&lt;/h1&gt;

&lt;p&gt;Angular 21 introduces one of the most exciting developments in the modern edition of Angular: &lt;strong&gt;Signal-Based Forms&lt;/strong&gt;. Built directly on the reactive foundation of Angular signals, this new experimental API provides a cleaner, more intuitive, strongly typed, and ergonomic approach for managing form state—without the heavy boilerplate of Reactive Forms.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Important:&lt;/strong&gt; Signal Forms are &lt;em&gt;experimental&lt;/em&gt;. Their API can change. Avoid using them in critical production scenarios unless you understand the risks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Despite this, Signal Forms clearly represent Angular’s future direction.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Why Signal Forms?
&lt;/h2&gt;

&lt;p&gt;Traditionally in Angular, building forms has involved several concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking values&lt;/li&gt;
&lt;li&gt;Managing UI interaction states (touched, dirty)&lt;/li&gt;
&lt;li&gt;Handling validation&lt;/li&gt;
&lt;li&gt;Keeping UI and model in sync&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reactive Forms solved many challenges but introduced their own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verbosity FormBuilder API&lt;/li&gt;
&lt;li&gt;Required subscriptions (valueChanges)&lt;/li&gt;
&lt;li&gt;Manual cleaning&lt;/li&gt;
&lt;li&gt;Difficult nested forms&lt;/li&gt;
&lt;li&gt;Weak type-safety&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Signal Forms solve these problems through:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1." Automatic synchronization&lt;br&gt;&lt;br&gt;
2." Full type safety&lt;br&gt;&lt;br&gt;
3." Schema-based validation&lt;br&gt;&lt;br&gt;
4." Fine-grained reactivity&lt;br&gt;&lt;br&gt;
5." Drastically reduced boilerplate&lt;br&gt;&lt;br&gt;
6." Natural integration with Angular Signals&lt;/p&gt;


&lt;h3&gt;
  
  
  1. Form Models — The Core of Signal Forms
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;form model&lt;/strong&gt; is simply a writable signal holding the structure of your form data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/forms/signals&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Field&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;input type="email" [field]="loginForm.email" /&amp;gt;
    &amp;lt;input type="password" [field]="loginForm.password" /&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;loginModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;loginForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;form&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loginModel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;form(model)&lt;/code&gt; creates a &lt;strong&gt;Field Tree&lt;/strong&gt; that maps directly to your model.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Achieving Full Type Safety
&lt;/h3&gt;

&lt;p&gt;Although TypeScript can infer types from object literals, defining explicit interfaces provides maximum safety and better IDE support.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;LoginData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;loginModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;LoginData&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;loginForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;form&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loginModel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;loginForm.email&lt;/code&gt; → &lt;code&gt;FieldTree&amp;lt;string&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Accessing invalid fields like &lt;code&gt;loginForm.username&lt;/code&gt; results in compile-time errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This level of type safety surpasses Reactive Forms.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Reading Form Values
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Read from the model (entire form):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;onSubmit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loginModel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Read from an individual field:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Current email: {{ loginForm.email().value() }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each field exposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;value()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;valid()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;errors()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dirty()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;touched()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All as signals.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Updating Form Models Programmatically
&lt;/h3&gt;

&lt;p&gt;Signal Forms allow three update methods.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Replace the entire model
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;alice@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Patch specific fields
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newEmail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Update a single field
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userForm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This eliminates the need for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;patchValue()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setValue()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;formGroup.get('field')&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  5. Automatic Two-Way Binding With &lt;code&gt;[field]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;[field]&lt;/code&gt; directive enables perfect two-way data binding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;[field]=&lt;/span&gt;&lt;span class="s"&gt;"userForm.name"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How it works:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;User input → Field state → Model&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model updates → Field state → Input UI&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No subscriptions.&lt;br&gt;&lt;br&gt;
No event handlers.&lt;br&gt;&lt;br&gt;
No boilerplate.&lt;/p&gt;

&lt;p&gt;Reactive Forms could never achieve this cleanly.&lt;/p&gt;


&lt;h3&gt;
  
  
  6. Nested Models and Arrays
&lt;/h3&gt;

&lt;p&gt;Models can contain nested object structures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;userModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;street&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Access fields easily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;[field]=&lt;/span&gt;&lt;span class="s"&gt;"userForm.address.street"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrays are also supported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;orderModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Field state persists even when array items move, thanks to identity tracking.&lt;/p&gt;




&lt;h3&gt;
  
  
  7. Schema-Based Validation
&lt;/h3&gt;

&lt;p&gt;Validation is clean and centralized:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/forms/signals&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;form&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Field validation state is reactive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;formRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;formRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;formRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;touched&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Validation no longer scatters across components.&lt;/p&gt;




&lt;h3&gt;
  
  
  8. When Should You Use Signal Forms?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  New Angular 21+ apps
&lt;/h4&gt;

&lt;p&gt;Signal-first architecture is the new standard.&lt;/p&gt;

&lt;h4&gt;
  
  
  Teams wanting stronger type safety
&lt;/h4&gt;

&lt;p&gt;Every field is exactly typed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Devs tired of Reactive Form boilerplate
&lt;/h4&gt;

&lt;p&gt;Signal Forms drastically simplify code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Complex UI with computed reactive form state
&lt;/h4&gt;

&lt;p&gt;Signals integrate perfectly.&lt;/p&gt;

&lt;h4&gt;
  
  
  ❌ Avoid if:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;You need long-term stability&lt;/li&gt;
&lt;li&gt;You rely on mature Reactive Forms features&lt;/li&gt;
&lt;li&gt;Your app must avoid experimental APIs&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  9. Reactive Forms vs Signal Forms
&lt;/h3&gt;

&lt;p&gt;| Feature | Reactive Forms | Signal Forms | |--------|----------------|--------------| | Boilerplate | High | Very low | | Type-safety | Weak | Strong | | Two-way binding | Manual | Automatic | | Validation | Scattered | Centralized schema | | Nested forms | Verbose | Natural | | Subscriptions | Required | None | | Change detection | Zone-heavy | Fine-grained |&lt;/p&gt;

&lt;p&gt;Signal Forms feel like the "modern Angular mode," while Reactive Forms increasingly feel legacy.&lt;/p&gt;




&lt;h3&gt;
  
  
  10. Full Example: Login Form
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Field&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;form (ngSubmit)="submit()"&amp;gt;
      &amp;lt;input type="email" [field]="form.email" /&amp;gt;
      &amp;lt;input type="password" [field]="form.password" /&amp;gt;
      &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;form&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Minimal. Reactive. Completely type-safe.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Signal Forms in Angular 21 represent a big step forward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaner API&lt;/li&gt;
&lt;li&gt;Stronger type safety&lt;/li&gt;
&lt;li&gt;Automatic two-way binding&lt;/li&gt;
&lt;li&gt;Centralized validation&lt;/li&gt;
&lt;li&gt;Fine-grained reactivity&lt;/li&gt;
&lt;li&gt;Dramatically better developer experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although these are experimental, they clearly show the future of Angular's form ecosystem. Once you get into using Signal Forms, you may never want to use Reactive Forms again.&lt;/p&gt;




</description>
      <category>angular</category>
      <category>newfeatures</category>
    </item>
    <item>
      <title>Building an API Key Management System with ABP Framework</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Mon, 17 Nov 2025 10:45:38 +0000</pubDate>
      <link>https://forem.com/abp_io/building-an-api-key-management-system-with-abp-framework-12cd</link>
      <guid>https://forem.com/abp_io/building-an-api-key-management-system-with-abp-framework-12cd</guid>
      <description>&lt;h1&gt;
  
  
  Building an API Key Management System with ABP Framework
&lt;/h1&gt;

&lt;p&gt;API keys are one of the most common authentication methods for APIs, especially for machine-to-machine communication. In this article, I'll explain what API key authentication is, when to use it, and how to implement a complete API key management system using ABP Framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is API Key Authentication?
&lt;/h2&gt;

&lt;p&gt;An API key is a unique identifier used to authenticate requests to an API. Unlike user credentials (username/password) or OAuth tokens, API keys are designed for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Programmatic access&lt;/strong&gt; - Scripts, CLI tools, and automated processes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service-to-service communication&lt;/strong&gt; - Microservices authenticating with each other&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Third-party integrations&lt;/strong&gt; - External systems accessing your API&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IoT devices&lt;/strong&gt; - Embedded systems with limited authentication capabilities&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mobile/Desktop apps&lt;/strong&gt; - Native applications that need persistent authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Use API Keys?
&lt;/h2&gt;

&lt;p&gt;While modern authentication methods like OAuth2 and JWT are excellent for user authentication, API keys offer distinct advantages in certain scenarios:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simplicity&lt;/strong&gt; : No complex OAuth flows or token refresh mechanisms. Just include the key in your request header.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Long-lived&lt;/strong&gt; : Unlike JWT tokens that expire in minutes/hours, API keys can remain valid for months or years, making them ideal for automated systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Revocable&lt;/strong&gt; : You can instantly revoke a compromised key without affecting user credentials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Granular Control&lt;/strong&gt; : Different keys for different purposes (read-only, admin, specific services).&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Use Cases
&lt;/h2&gt;

&lt;p&gt;Here are some practical scenarios where API key authentication shines:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Mobile Applications
&lt;/h3&gt;

&lt;p&gt;Your mobile app needs to call your backend APIs. Instead of storing user credentials or managing token refresh flows, use an API key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Mobile app configuration&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;apiClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApiClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.yourapp.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;apiClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetApiKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sk_mobile_prod_abc123...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Microservice Communication
&lt;/h3&gt;

&lt;p&gt;Service A needs to call Service B's protected endpoints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Order Service calling Inventory Service&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpRequestMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"https://inventory-service/api/products"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"X-Api-Key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_configuration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"InventoryService:ApiKey"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Third-Party Integrations
&lt;/h3&gt;

&lt;p&gt;You're providing APIs to external partners or customers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Customer's integration script&lt;/span&gt;
curl &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-Api-Key: pk_partner_xyz789..."&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     https://api.yourplatform.com/api/orders

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implementing API Key Management in ABP Framework
&lt;/h2&gt;

&lt;p&gt;Now let's see how to build a complete API key management system using ABP Framework. I've created an open-source implementation that you can use in your projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Overview
&lt;/h3&gt;

&lt;p&gt;The implementation consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User-based API keys&lt;/strong&gt; - Each key belongs to a specific user&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Permission delegation&lt;/strong&gt; - Keys inherit user permissions with optional restrictions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secure storage&lt;/strong&gt; - Keys are hashed with SHA-256&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefix-based lookup&lt;/strong&gt; - Fast key resolution with caching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web UI&lt;/strong&gt; - Manage keys through a user-friendly interface&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-tenancy support&lt;/strong&gt; - Full ABP multi-tenancy compatibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03sx6yhes5ythpgjgx6e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03sx6yhes5ythpgjgx6e.png" alt="API Keys Management UI" width="800" height="243"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Architecture Overview
&lt;/h3&gt;

&lt;p&gt;The solution follows ABP's modular architecture with four main layers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────────┐
│ Web Layer (UI) │
│ • Razor Pages for CRUD operations │
│ • JavaScript for client interactions │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ AspNetCore Layer (Middleware) │
│ • Authentication Handler │
│ • API Key Resolver (Header/Query) │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ Application Layer (Business Logic) │
│ • ApiKeyAppService (CRUD operations) │
│ • DTO mappings and validations │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ Domain Layer (Core Business) │
│ • ApiKey Entity &amp;amp; Manager │
│ • IApiKeyRepository │
│ • Domain services &amp;amp; events │
└─────────────────────────────────────────────┘

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Components
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Domain Layer - The Core Entity
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApiKey&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;FullAuditedAggregateRoot&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;IMultiTenant&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;TenantId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;UserId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Prefix&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;KeyHash&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;ExpiresAt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Key format: {prefix}_{key}&lt;/span&gt;
    &lt;span class="c1"&gt;// Only the hash is stored, never the actual key&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Design Decisions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefix-based lookup&lt;/strong&gt; : Keys have format &lt;code&gt;prefix_actualkey&lt;/code&gt;. The prefix is indexed for fast database lookups.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SHA-256 hashing&lt;/strong&gt; : The actual key is hashed and never stored in plain text.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User association&lt;/strong&gt; : Each key belongs to a user, inheriting their permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Soft delete&lt;/strong&gt; : Deleted keys are marked as deleted but not removed from database for audit purposes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Authentication Flow
&lt;/h4&gt;

&lt;p&gt;Here's how authentication works when a request arrives:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-15-building-an-api-key-management-system%2Fimages%2Fauth-flow.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fabpframework%2Fabp%2Fdev%2Fdocs%2Fen%2FCommunity-Articles%2F2025-11-15-building-an-api-key-management-system%2Fimages%2Fauth-flow.svg" alt="Authentication Flow" width="700" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1. Extract API key from request&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"X-Api-Key"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;FirstOrDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AuthenticateResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NoResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// 2. Split prefix and key&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// 3. Find key by prefix (cached)&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;apiKeyEntity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_apiKeyRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindByPrefixAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiKeyEntity&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AuthenticateResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid API key"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 4. Verify hash&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;keyHash&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HashHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ComputeSha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiKeyEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;KeyHash&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;keyHash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AuthenticateResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid API key"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 5. Check expiration and active status&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiKeyEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExpiresAt&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UtcNow&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;apiKeyEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AuthenticateResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Fail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"API key expired or inactive"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// 6. Create claims principal with user identity&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;claims&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Claim&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AbpClaimTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;apiKeyEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AbpClaimTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TenantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;apiKeyEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TenantId&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ApiKeyId"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;apiKeyEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;AuthenticateResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Creating and Managing API Keys
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Creating a new key:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ueo3qih1930pk58q2i0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ueo3qih1930pk58q2i0.png" alt="Create API Key Modal" width="800" height="864"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApiKeyManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;(&lt;/span&gt;&lt;span class="n"&gt;ApiKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;CreateAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;expiresAt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Generate unique prefix&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;GenerateUniquePrefixAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Generate secure random key&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GenerateSecureRandomString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Hash the key for storage&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;keyHash&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HashHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ComputeSha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ApiKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;GuidGenerator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;keyHash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;expiresAt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;CurrentTenant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_apiKeyRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;InsertAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Return both entity and the full key (prefix_key)&lt;/span&gt;
        &lt;span class="c1"&gt;// This is the ONLY time the actual key is visible&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;$"&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;_&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt; : The actual key is returned only once during creation. After that, only the hash is stored.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5jk6hlrnkvaqxot7qw6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5jk6hlrnkvaqxot7qw6.png" alt="Created Key - Copy Once" width="800" height="522"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Using API Keys in Your Application
&lt;/h3&gt;

&lt;p&gt;Once created, clients can use the API key to authenticate:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP Header (Recommended):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-Api-Key: sk_prod_abc123def456..."&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     https://api.example.com/api/products

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;JavaScript:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/api/products&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X-Api-Key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sk_prod_abc123def456...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;C# HttpClient:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DefaultRequestHeaders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"X-Api-Key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sk_prod_abc123def456..."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://api.example.com/api/products"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Python:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;X-Api-Key&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sk_prod_abc123def456...&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://api.example.com/api/products&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Permission Management
&lt;/h3&gt;

&lt;p&gt;API keys inherit the user's permissions, but you can further restrict them:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrccmujwjr6cxk42yqig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrccmujwjr6cxk42yqig.png" alt="Permission Management" width="800" height="696"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This allows scenarios like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read-only API key for reporting tools&lt;/li&gt;
&lt;li&gt;Limited scope keys for third-party integrations&lt;/li&gt;
&lt;li&gt;Service-specific keys with minimal permissions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Check if current request is authenticated via API key&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CurrentUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindClaim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ApiKeyId"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;apiKeyId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CurrentUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindClaim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ApiKeyId"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Additional API key specific logic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Performance Considerations
&lt;/h2&gt;

&lt;p&gt;The implementation uses several optimizations:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Prefix-based indexing&lt;/strong&gt; : Database lookups are done by prefix (indexed column), not the full key hash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Distributed caching&lt;/strong&gt; : API keys are cached after first lookup, dramatically reducing database queries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Cache configuration&lt;/span&gt;
&lt;span class="n"&gt;Configure&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AbpDistributedCacheOptions&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;KeyPrefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ApiKey:"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Cache invalidation&lt;/strong&gt; : When a key is modified or deleted, cache is automatically invalidated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical Performance:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cached lookup: &lt;strong&gt;&amp;lt; 5ms&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Database lookup: &lt;strong&gt;&amp;lt; 50ms&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Cache hit rate: &lt;strong&gt;~95%&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security Best Practices
&lt;/h2&gt;

&lt;p&gt;When implementing API key authentication, follow these guidelines:&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Always use HTTPS&lt;/strong&gt; - Never send API keys over unencrypted connections&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Use different keys per environment&lt;/strong&gt; - Separate keys for dev, staging, production&lt;/p&gt;

&lt;p&gt;❌ &lt;strong&gt;Don't log the full key&lt;/strong&gt; - Only log the prefix for debugging&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;The complete source code is available on GitHub:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repository&lt;/strong&gt; : &lt;a href="https://github.com/salihozkara/AbpApikeyManagement" rel="noopener noreferrer"&gt;github.com/salihozkara/AbpApikeyManagement&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To integrate it into your ABP project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clone or download the repository&lt;/li&gt;
&lt;li&gt;Add project references to your solution&lt;/li&gt;
&lt;li&gt;Add module dependencies to your modules&lt;/li&gt;
&lt;li&gt;Run EF Core migrations to create the database tables&lt;/li&gt;
&lt;li&gt;Navigate to &lt;code&gt;/ApiKeyManagement&lt;/code&gt; to start managing keys
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In your Web module&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;DependsOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ApiKeyManagementWebModule&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;YourWebModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AbpModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In your HttpApi.Host module&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;DependsOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ApiKeyManagementHttpApiModule&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;YourHttpApiHostModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AbpModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;API key authentication remains a crucial part of modern API security, especially for machine-to-machine communication. While it shouldn't replace user authentication methods like OAuth2 for user-facing applications, it's perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated scripts and tools&lt;/li&gt;
&lt;li&gt;Service-to-service communication&lt;/li&gt;
&lt;li&gt;Third-party integrations&lt;/li&gt;
&lt;li&gt;Long-lived access without token refresh complexity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The implementation shown here demonstrates how ABP Framework's modular architecture, DDD principles, and built-in features (multi-tenancy, caching, permissions) can be leveraged to build a production-ready API key management system.&lt;/p&gt;

&lt;p&gt;The solution is open-source and ready to be integrated into your ABP projects. Feel free to explore the code, suggest improvements, or adapt it to your specific needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub Repository: &lt;a href="https://github.com/salihozkara/AbpApikeyManagement" rel="noopener noreferrer"&gt;salihozkara/AbpApikeyManagement&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ABP Framework: &lt;a href="https://abp.io" rel="noopener noreferrer"&gt;abp.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ABP Documentation: &lt;a href="https://abp.io/docs/latest" rel="noopener noreferrer"&gt;docs.abp.io&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>permissionmanagement</category>
      <category>api</category>
      <category>abpframework</category>
    </item>
    <item>
      <title>What is That Domain Service in DDD for .NET Developers</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Mon, 10 Nov 2025 06:37:51 +0000</pubDate>
      <link>https://forem.com/abp_io/what-is-that-domain-service-in-ddd-for-net-developers-59li</link>
      <guid>https://forem.com/abp_io/what-is-that-domain-service-in-ddd-for-net-developers-59li</guid>
      <description>&lt;h1&gt;
  
  
  What is That Domain Service in DDD for .NET Developers?
&lt;/h1&gt;

&lt;p&gt;When you start applying &lt;strong&gt;Domain-Driven Design (DDD)&lt;/strong&gt; in your .NET projects, you'll quickly meet some core building blocks: &lt;strong&gt;Entities&lt;/strong&gt; , &lt;strong&gt;Value Objects&lt;/strong&gt; , &lt;strong&gt;Aggregates&lt;/strong&gt; , and finally… &lt;strong&gt;Domain Services&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But what exactly &lt;em&gt;is&lt;/em&gt; a Domain Service, and when should you use one?&lt;/p&gt;

&lt;p&gt;Let's break it down with practical examples and ABP Framework implementation patterns.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fph4r4vfywn9kphtckj0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fph4r4vfywn9kphtckj0h.png" alt="Diagram showing layered architecture: UI, Application, Domain (Entities, Value Objects, Domain Services), Infrastructure boundaries" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Core Idea of Domain Services
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Domain Service&lt;/strong&gt; represents &lt;strong&gt;a domain concept that doesn't naturally belong to a single Entity or Value Object&lt;/strong&gt; , but still belongs to the &lt;strong&gt;domain layer&lt;/strong&gt; - &lt;em&gt;not&lt;/em&gt; to the application or infrastructure.&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If your business logic doesn't fit into a single Entity, but still expresses a business rule, that's a good candidate for a Domain Service.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Example: Money Transfer Between Accounts
&lt;/h2&gt;

&lt;p&gt;Imagine a simple &lt;strong&gt;banking system&lt;/strong&gt; where you can transfer money between accounts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AggregateRoot&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Domain model should be created in a valid state.&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;openingBalance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;openingBalance&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Opening balance cannot be negative."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;openingBalance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Withdrawal amount must be positive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Insufficient balance."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Deposit amount must be positive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In a richer domain you might introduce a &lt;code&gt;Money&lt;/code&gt; value object (amount + currency + rounding rules) instead of a raw &lt;code&gt;decimal&lt;/code&gt; for stronger invariants.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Implementing a Domain Service
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7p5cuflcd6qimxb5w728.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7p5cuflcd6qimxb5w728.png" alt="Conceptual illustration showing how a domain service coordinates two aggregates" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ReferenceEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cannot transfer to the same account."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Transfer amount must be positive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Naming Convention&lt;/strong&gt; : ABP suggests using the &lt;code&gt;Manager&lt;/code&gt; or &lt;code&gt;Service&lt;/code&gt; suffix for domain services. We typically use &lt;code&gt;Manager&lt;/code&gt; suffix (e.g., &lt;code&gt;IssueManager&lt;/code&gt;, &lt;code&gt;OrderManager&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : This is a synchronous domain operation. The domain service focuses purely on business rules without infrastructure concerns like database access or event publishing. For cross-cutting concerns, use Application Service layer or domain events.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Domain Service vs. Application Service
&lt;/h2&gt;

&lt;p&gt;Here's a quick comparison:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm81zvy8z8d5onq5oymw3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm81zvy8z8d5onq5oymw3.png" alt="Side-by-side comparison: Domain Service (pure business rule) vs Application Service (orchestrates repositories, transactions, external systems)" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;| Layer | Responsibility | Example | | ----------------------- | -------------------------------------------------------------------------------- | ---------------------------- | | &lt;strong&gt;Domain Service&lt;/strong&gt; | Pure business rule spanning entities/aggregates | &lt;code&gt;MoneyTransferManager&lt;/code&gt; | | &lt;strong&gt;Application Service&lt;/strong&gt; | Orchestrates use cases, handles repositories, transactions, external systems | &lt;code&gt;BankAppService&lt;/code&gt; |&lt;/p&gt;




&lt;h2&gt;
  
  
  The Application Service Layer
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;Application Service&lt;/strong&gt; orchestrates the domain logic and handles infrastructure concerns:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb57wjfze84fnsjmxhwkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb57wjfze84fnsjmxhwkg.png" alt="ABP solution layout highlighting Domain layer (Entities, Value Objects, Domain Services) separate from Application and Infrastructure layers" width="800" height="742"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAppService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ApplicationService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="n"&gt;_moneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BankAppService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;accountRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="n"&gt;moneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_accountRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;accountRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_moneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;moneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;TransferAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;fromId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;toId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;_moneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UpdateAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UpdateAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : Domain services are automatically registered to Dependency Injection with a &lt;strong&gt;Transient&lt;/strong&gt; lifetime when inheriting from &lt;code&gt;DomainService&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Benefits of ABP's DomainService Base Class
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;DomainService&lt;/code&gt; base class gives you access to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Localization&lt;/strong&gt; (&lt;code&gt;IStringLocalizer L&lt;/code&gt;) - Multi-language support for error messages&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging&lt;/strong&gt; (&lt;code&gt;ILogger Logger&lt;/code&gt;) - Built-in logger for tracking operations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Local Event Bus&lt;/strong&gt; (&lt;code&gt;ILocalEventBus LocalEventBus&lt;/code&gt;) - Publish local domain events&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distributed Event Bus&lt;/strong&gt; (&lt;code&gt;IDistributedEventBus DistributedEventBus&lt;/code&gt;) - Publish distributed events&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GUID Generator&lt;/strong&gt; (&lt;code&gt;IGuidGenerator GuidGenerator&lt;/code&gt;) - Sequential GUID generation for better database performance&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clock&lt;/strong&gt; (&lt;code&gt;IClock Clock&lt;/code&gt;) - Abstraction for date/time operations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example with ABP Features
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt; : While domain services &lt;em&gt;can&lt;/em&gt; publish domain events using the event bus, they should remain focused on business rules. Consider whether event publishing belongs in the domain service or the application service based on your consistency boundaries.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferredEvent&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;FromAccountId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;ToAccountId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;Amount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;TransferAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ReferenceEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"SameAccountTransferNotAllowed"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"InvalidTransferAmount"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

        &lt;span class="c1"&gt;// Log the operation&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"Transferring {Amount} from {From} to {To}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Publish local event for further policies (limits, notifications, audit, etc.)&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;LocalEventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PublishAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;MoneyTransferredEvent&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;FromAccountId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;ToAccountId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;Amount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Local Events&lt;/strong&gt; : By default, event handlers are executed within the same Unit of Work. If an event handler throws an exception, the database transaction is rolled back, ensuring consistency.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Keep Domain Services Pure and Focused on Business Rules
&lt;/h3&gt;

&lt;p&gt;Domain services should only contain business logic. They should not be responsible for application-level concerns like database transactions, authorization, or fetching entities from a repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Good ✅ Pure rule: receives aggregates already loaded.&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Business rules and coordination&lt;/span&gt;
        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Bad ❌ Mixing application and domain concerns.&lt;/span&gt;
&lt;span class="c1"&gt;// This logic belongs in an Application Service.&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MoneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;accountRepository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_accountRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;accountRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;TransferAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;fromId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;toId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Don't fetch entities inside a domain service.&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Leverage Entity Methods First
&lt;/h3&gt;

&lt;p&gt;Always prefer encapsulating business logic within an entity's methods when the logic belongs to a single aggregate. A domain service should only be used when a business rule spans multiple aggregates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Good ✅ - Internal state change belongs in the entity&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AggregateRoot&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Insufficient balance"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Balance&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Use Domain Service only when logic spans multiple aggregates&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Delegates to entity&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Delegates to entity&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Prefer Domain Services over Anemic Entities
&lt;/h3&gt;

&lt;p&gt;Avoid placing business logic that coordinates multiple entities directly into an application service. This leads to an "Anemic Domain Model," where entities are just data bags and the business logic is scattered in application services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad ❌ - Business logic is in the Application Service (Anemic Domain)&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAppService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ApplicationService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;TransferAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;fromId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;toId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// This is domain logic and should be in a Domain Service&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ReferenceEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cannot transfer to the same account."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Transfer amount must be positive."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Use Meaningful Names
&lt;/h3&gt;

&lt;p&gt;ABP recommends naming domain services with a &lt;code&gt;Manager&lt;/code&gt; or &lt;code&gt;Service&lt;/code&gt; suffix based on the business concept they represent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Good ✅&lt;/span&gt;
&lt;span class="n"&gt;MoneyTransferManager&lt;/span&gt;
&lt;span class="n"&gt;OrderManager&lt;/span&gt;
&lt;span class="n"&gt;IssueManager&lt;/span&gt;
&lt;span class="n"&gt;InventoryAllocationService&lt;/span&gt;

&lt;span class="c1"&gt;// Bad ❌&lt;/span&gt;
&lt;span class="n"&gt;AccountHelper&lt;/span&gt;
&lt;span class="n"&gt;OrderProcessor&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Advanced Example: Order Processing with Inventory Check
&lt;/h2&gt;

&lt;p&gt;Here's a more complex scenario showing domain service interaction with domain abstractions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Domain abstraction - defines contract but implementation is in infrastructure&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IInventoryChecker&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IDomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;IsAvailableAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IInventoryChecker&lt;/span&gt; &lt;span class="n"&gt;_inventoryChecker&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;OrderManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IInventoryChecker&lt;/span&gt; &lt;span class="n"&gt;inventoryChecker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_inventoryChecker&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inventoryChecker&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Validates and coordinates order processing with inventory&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;ProcessAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Inventory&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// First pass: validate availability using domain abstraction&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_inventoryChecker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsAvailableAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ProductId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Quantity&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"InsufficientInventory"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ProductId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Second pass: perform reservations&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Reserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ProductId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Processing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Domain Abstractions&lt;/strong&gt; : The &lt;code&gt;IInventoryChecker&lt;/code&gt; interface is a domain service contract. Its implementation can be in the infrastructure layer, but the contract belongs to the domain. This keeps the domain layer independent of infrastructure details while still allowing complex validations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caution&lt;/strong&gt; : Always perform validation and action atomically within a single transaction to avoid race conditions (TOCTOU - Time Of Check Time Of Use).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transaction Boundaries&lt;/strong&gt; : When a domain service coordinates multiple aggregates, ensure the Application Service wraps the operation in a Unit of Work to maintain consistency. ABP's &lt;code&gt;[UnitOfWork]&lt;/code&gt; attribute or Application Services' built-in UoW handling ensures this automatically.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Common Pitfalls and How to Avoid Them
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Bloated Domain Services
&lt;/h3&gt;

&lt;p&gt;Don't let domain services become "god objects" that do everything. Keep them focused on a single business concept.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad ❌ - Too many responsibilities&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AccountManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CalculateInterest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GenerateStatement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ValidateAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SendNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Good ✅ - Split by business concept&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InterestCalculationManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Circular Dependencies Between Aggregates
&lt;/h3&gt;

&lt;p&gt;When domain services coordinate multiple aggregates, be careful about creating circular dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Consider using Domain Events instead of direct coupling&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;ProcessAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Processing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Instead of directly modifying Customer aggregate here,&lt;/span&gt;
        &lt;span class="c1"&gt;// publish an event that CustomerManager can handle&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;LocalEventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PublishAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;OrderProcessedEvent&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;CustomerId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CustomerId&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Confusing Domain Service with Domain Event Handlers
&lt;/h3&gt;

&lt;p&gt;Domain services orchestrate business operations. Domain event handlers react to state changes. Don't mix them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Domain Service - Orchestrates business logic&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;TransferAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;LocalEventBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PublishAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;MoneyTransferredEvent&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;FromAccountId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;ToAccountId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;Amount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Domain Event Handler - Reacts to domain events&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferredEventHandler&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="n"&gt;ILocalEventHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MoneyTransferredEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ITransientDependency&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;HandleEventAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MoneyTransferredEvent&lt;/span&gt; &lt;span class="n"&gt;eventData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Send notification, update analytics, etc.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Testing Domain Services
&lt;/h2&gt;

&lt;p&gt;Domain services are easy to test because they have minimal dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager_Tests&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Should_Transfer_Money_Between_Accounts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Arrange&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fromAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1000m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;500m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MoneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Act&lt;/span&gt;
        &lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;200m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Assert&lt;/span&gt;
        &lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;800m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;700m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Should_Throw_When_Insufficient_Balance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fromAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;500m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MoneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Throw&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
            &lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;200m&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Should_Throw_When_Amount_Is_NonPositive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fromAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MoneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Throw&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
            &lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0m&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Throw&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
            &lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;5m&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Should_Throw_When_Same_Account&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MoneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Throw&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BusinessException&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
            &lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10m&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Integration Testing with ABP Test Infrastructure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoneyTransferManager_IntegrationTests&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BankingDomainTestBase&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;MoneyTransferManager&lt;/span&gt; &lt;span class="n"&gt;_transferManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MoneyTransferManager_IntegrationTests&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_transferManager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MoneyTransferManager&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;_accountRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Should_Transfer_And_Persist_Changes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Arrange&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fromAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1000m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;500m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;InsertAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;InsertAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;UnitOfWorkManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveChangesAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Act&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_transferManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TransferAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;200m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;UnitOfWorkManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveChangesAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Assert&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;updatedFrom&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromAccount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;updatedTo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_accountRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toAccount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;updatedFrom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;800m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;updatedTo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;700m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  When NOT to Use a Domain Service
&lt;/h2&gt;

&lt;p&gt;Not every operation needs a domain service. Avoid over-engineering:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Simple CRUD Operations&lt;/strong&gt; : Use Application Services directly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Aggregate Operations&lt;/strong&gt; : Use Entity methods&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure Concerns&lt;/strong&gt; : Use Infrastructure Services&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application Workflow&lt;/strong&gt; : Use Application Services
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Don't create a domain service for this ❌&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AccountBalanceReader&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;GetBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Just use the property directly ✅&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Domain Services&lt;/strong&gt; are domain-level, not application-level&lt;/li&gt;
&lt;li&gt;They encapsulate &lt;strong&gt;business logic that doesn't belong to a single entity&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;They keep your &lt;strong&gt;entities clean&lt;/strong&gt; and &lt;strong&gt;business logic consistent&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;In ABP, inherit from &lt;code&gt;DomainService&lt;/code&gt; to get built-in features&lt;/li&gt;
&lt;li&gt;Keep them &lt;strong&gt;focused&lt;/strong&gt; , &lt;strong&gt;pure&lt;/strong&gt; , and &lt;strong&gt;testable&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Next time you're writing a business rule that doesn't clearly belong to an entity, ask yourself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Is this a Domain Service?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If it's pure domain logic that coordinates multiple entities or implements a business rule, &lt;strong&gt;put it in the domain layer&lt;/strong&gt; - your future self (and your team) will thank you.&lt;/p&gt;

&lt;p&gt;Domain Services are a powerful tool in your DDD toolkit. Use them wisely to keep your domain model clean, expressive, and maintainable.&lt;/p&gt;




</description>
      <category>ddd</category>
      <category>abpframework</category>
      <category>domainservice</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>UI &amp; UX Trends That Will Shape 2026</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Wed, 05 Nov 2025 14:56:43 +0000</pubDate>
      <link>https://forem.com/abp_io/ui-ux-trends-that-will-shape-2026-3e5j</link>
      <guid>https://forem.com/abp_io/ui-ux-trends-that-will-shape-2026-3e5j</guid>
      <description>&lt;h1&gt;
  
  
  UI &amp;amp; UX Trends That Will Shape 2026
&lt;/h1&gt;

&lt;p&gt;Cinematic, gamified, high-wow-factor websites with scroll-to-play videos or scroll-to-tell stories are wonderful to experience, but you won't find these trends in this article. If you're interested in design trends directly related to the software world, such as &lt;strong&gt;performance&lt;/strong&gt; , &lt;strong&gt;accessibility&lt;/strong&gt; , &lt;strong&gt;understandability&lt;/strong&gt; , and &lt;strong&gt;efficiency&lt;/strong&gt; , grab a cup of coffee and enjoy.&lt;/p&gt;

&lt;p&gt;As we approach the end of 2025, I'd like to share with you the most important user interface and user experience design trends that have become more of a &lt;strong&gt;toolkit&lt;/strong&gt; than a trend, and that continue to evolve and become a part of our lives. I predict we'll see a lot of them in 2026.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Simplicity and Speed ​​
&lt;/h2&gt;

&lt;p&gt;Designing understandable and readable applications is becoming far more important than designing in line with trends and fashion. In the software and business world, preferences are shifting more and more toward the &lt;strong&gt;right design&lt;/strong&gt; over the cool design. As designers developing a product whose direct target audience is software developers, we design our products for the designers' enjoyment, but for the &lt;strong&gt;end user's ease of use&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Users no longer care so much about the flashiness of a website. True converts are primarily interested in your product, service, or content. What truly matters to them is how easily and quickly they can access the information they're looking for.&lt;/p&gt;

&lt;p&gt;More users, more sales, better promotion, and a higher conversion rate... The elements that serve these goals are optimized solutions and thoughtful details in our designs, more than visual displays.&lt;/p&gt;

&lt;p&gt;If the "loading" icon appears too often on your digital product, you might not be doing it right. If you fail to optimize speed, the temporary effect of visual displays won't be enough to convert potential users into customers. Remember, the moment people start waiting, you've lost at least half of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Dark Mode - Still, and Forever
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65fkmwp0gkqyk57ih5dz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65fkmwp0gkqyk57ih5dz.png" alt="data-model" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dark Mode is no longer an option; it's a &lt;strong&gt;standard&lt;/strong&gt;. It's become a necessity, not a choice, especially for users who spend hours staring at screens and are accustomed to dark themes in code editors and terminals. However, the approach to dark mode isn't simply about inverting colors; it's much deeper than that. The key is managing contrast and depth.&lt;/p&gt;

&lt;p&gt;The layer hierarchy established in a light-colored design doesn't lose its impact when switched to dark mode. The colors, shadows, highlights, and contrasting elements used to create an &lt;strong&gt;easily perceivable hierarchy&lt;/strong&gt; should be carefully considered for each mode. Our &lt;a href="https://leptontheme.com/" rel="noopener noreferrer"&gt;LeptonX theme&lt;/a&gt;'s Light, Dark, Semi-dark, and System modes offer valuable insights you might want to explore.&lt;/p&gt;

&lt;p&gt;You might also want to take a look at the dark and light modes we designed with these elements in mind in &lt;a href="https://abp.io/get-started" rel="noopener noreferrer"&gt;ABP Studio&lt;/a&gt; and the &lt;a href="https://abp.io/docs/latest/" rel="noopener noreferrer"&gt;ABP.io Documents page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Bento Grid - A Timeless Trend
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstypu8fb2ltye1nogf5a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstypu8fb2ltye1nogf5a.png" alt="data-model" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;People don't read your website; they &lt;strong&gt;scan&lt;/strong&gt; it.&lt;/p&gt;

&lt;p&gt;Bento Grid, an indispensable trend for designers looking to manage their attention, looks set to remain a staple in 2026, just as it was in 2025. No designer should ignore the fact that many tech giants, especially Apple and Samsung, are still using bento grids on their websites. The bento grid appears not only on websites but also in operating systems, VR headset interfaces, game console interfaces, and game designs.&lt;/p&gt;

&lt;p&gt;The golden rule is &lt;strong&gt;contrast&lt;/strong&gt; and &lt;strong&gt;balance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The attractiveness and effectiveness of bento designs depend on certain factors you should consider when implementing them. If you ignore these rules, even with a proven method like bento, you can still alienate users.&lt;/p&gt;

&lt;p&gt;The bento grid is one of the best ways to display different types of content inclusively. When used correctly, it's also a great way to manipulate reading order, guiding the user's eye. Improper contrast and hierarchy can also create a negative experience. Designers should use this to guide the reader's eye: "Read here first, then read here."&lt;/p&gt;

&lt;p&gt;When creating a bento, you inherently have to sacrifice some of your "whitespace." This design has many elements for the user to focus on, and it actually strays from our first point, "Simplicity". Bento design, whose boundaries are drawn from the outset and independent of content, requires care not to include more or less than what is necessary. Too much content makes it boring; too little content makes it very close to meaningless.&lt;/p&gt;

&lt;p&gt;Bento grids should aim for a balanced design by using both simple text and sophisticated visuals. This visual can be an illustration, a video that starts playing when hovered over, a static image, or a large title. Only one or two cards on the screen at a time should have attention.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Larger Fonts, High Readability
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkyn1loghqex6z3efxf5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkyn1loghqex6z3efxf5w.png" alt="data-model" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Large fonts have been a trend for several years, and it seems web designers are becoming more and more bold. The increasing preference for larger fonts every year is a sign that this trend will continue into 2026. This trend is about more than just using large font sizes in headlines.&lt;/p&gt;

&lt;p&gt;Creating a cohesive typographic scale and proper line height and letter spacing are critical elements to consider when creating this trend. As the font size increases, line height should decrease, and the space between letters should be narrower.&lt;/p&gt;

&lt;p&gt;The browser default font size, which we used to see in body text and paragraphs and has now become standard, is 16 pixels. In the last few years, we've started seeing body font sizes of 17 or 18 pixels more frequently. The increasing importance of readability every year makes this more common. Font sizes in rem values, rather than px, provide the most efficient results.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Micro Animations
&lt;/h2&gt;

&lt;p&gt;Unless you're a web design agency designing a website to impress potential clients, you should avoid excessive changes, including excessive image changes during scrolling, and scroll direction changes. There's still room for oversized images and scroll animations. But be sure to create the visuals yourself.&lt;/p&gt;

&lt;p&gt;The trend I'm talking about here is &lt;strong&gt;micro animations&lt;/strong&gt; , not macro ones. Small movements, not large ones.&lt;/p&gt;

&lt;p&gt;The animation approach of 2025 is &lt;strong&gt;functional&lt;/strong&gt; and &lt;strong&gt;performance-sensitive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Microanimations exist to provide immediate feedback to the user. Instant feedback, like a button's shadow increasing when hovered over, a button's slight collapse when clicked, or a "Save" icon changing to a "Confirm" icon when saving data, keeps your designs alive.&lt;/p&gt;

&lt;p&gt;We see the real impact of the micro-animation trend in static, non-action visuals. The use of non-button elements in your designs, accentuated by micro-movements such as scrolling or hovering, seems poised to continue to create macro effects in 2026.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Real Images and Human-like Touches
&lt;/h2&gt;

&lt;p&gt;People quickly spot a fake. It's very difficult to convince a user who visits your website for the first time and doesn't trust you. &lt;strong&gt;First impressions&lt;/strong&gt; matter.&lt;/p&gt;

&lt;p&gt;Real photographs, actual product screenshots, and brand-specific illustrations will continue to be among the elements we want to see in &lt;strong&gt;trust-focused&lt;/strong&gt; designs in 2026.&lt;/p&gt;

&lt;p&gt;In addition to flawless work done by AI, vivid, real-life visuals, accompanied by deliberate imperfections, hand-drawn details, or designed products that convey the message, "A human made this site!", will continue to feel warmer and more welcoming.&lt;/p&gt;

&lt;p&gt;The human touch is evident not only in the visuals but also in your &lt;strong&gt;content and text&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In 2026, you'll need more &lt;strong&gt;human-like touches&lt;/strong&gt; that will make your design stand out among the thousands of similar websites rapidly generated by AI.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Accessibility - No Longer an Option, But a Legal and Ethical Obligation
&lt;/h2&gt;

&lt;p&gt;Accessibility, once considered a nice-to-do thing in recent years, is now becoming a &lt;strong&gt;necessity&lt;/strong&gt; in 2026 and beyond. Global regulations like the European Accessibility Act require all digital products to comply with WCAG standards.&lt;/p&gt;

&lt;p&gt;All design and software improvements you make to ensure end users can fully perform their tasks in your products, regardless of their temporary or permanent disabilities, should be viewed as ethical and commercial requirements, not as a requirement to comply with these standards.&lt;/p&gt;

&lt;p&gt;The foundation of accessibility in design is to use semantic HTML for screen readers, provide full keyboard control of all interactive elements, and clearly communicate the roles of complex components to the development team.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Intentional Friction
&lt;/h2&gt;

&lt;p&gt;Steve Krug, the father of UX design, started the trend of designing everything at a hyper-usable level with his book "Don't Make Me Think." As web designers, we've embraced this idea so much that all we care about is getting the user to their destination in the shortest possible scenario and as quickly as possible. This has required so many understandability measures that, after a while, it's starting to feel like fooling the user.&lt;/p&gt;

&lt;p&gt;In recent years, designers have started looking for ways to make things a little more challenging, rather than just getting the user to the result.&lt;/p&gt;

&lt;p&gt;When the end user visits your website, tries to understand exactly what it is at first glance, struggles a bit, and, after a little effort, becomes familiar with how your world works, they'll be more inclined to consider themselves a part of it.&lt;/p&gt;

&lt;p&gt;This has nothing to do with anti-usability. This philosophy is called Intentional Friction.&lt;/p&gt;

&lt;p&gt;This isn't a flaw; it's the pinnacle of error prevention. It's a step to prevent errors from occurring on autopilot and respects the user's ability to understand complex systems. Examples include reviewing the order summary or manually typing the project name when deleting a project on GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Where Does Artificial Intelligence Fit In?
&lt;/h2&gt;

&lt;p&gt;Artificial intelligence will be an infrastructure in 2026, not a trend.&lt;/p&gt;

&lt;p&gt;As designers, we should leverage AI not to paint us a picture, but to make workflows more intelligent. In my opinion, this is the best use case for AI.&lt;/p&gt;

&lt;p&gt;AI can learn user behavior and adapt the interface accordingly. Real-time A/B testing can save us time by conducting a real-time content review. The ability to actively use AI in any area that allows you to accelerate your progress will take you a step further in your career.&lt;/p&gt;

&lt;p&gt;Since your users are always human, &lt;strong&gt;don't be too eager&lt;/strong&gt; to incorporate AI-generated visuals into your design. Unless you're creating and selling a ready-made theme, you should &lt;strong&gt;avoid&lt;/strong&gt; AI-generated visuals, random bento grids, and randomly generated content.&lt;/p&gt;

&lt;p&gt;You should definitely incorporate AI into your work for new content, new ideas, personal and professional development, and insights that will take your design a step further. But just as you don't design your website for designers to like, the same applies to AI. Humans, not robots, will experience your website. &lt;strong&gt;AI-assisted&lt;/strong&gt; , not AI-generated, designs with a human touch are the trend I most expect seeing in 2026.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In the end, it's all fundamentally about respect for the user and their time. In 2026, our success as designers and developers will be measured not by how "cool" we are, but by how "efficient" and "reliable" a world we build for our users.&lt;/p&gt;

&lt;p&gt;Thank you for your time.&lt;/p&gt;

</description>
      <category>applicationdevelopme</category>
      <category>bestpractices</category>
      <category>performance</category>
      <category>optimization</category>
    </item>
    <item>
      <title>Repository Pattern in ASP.NET Core</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Sun, 02 Nov 2025 07:48:14 +0000</pubDate>
      <link>https://forem.com/abp_io/repository-pattern-in-aspnet-core-86a</link>
      <guid>https://forem.com/abp_io/repository-pattern-in-aspnet-core-86a</guid>
      <description>&lt;h1&gt;
  
  
  Repository Pattern in the ASP.NET Core
&lt;/h1&gt;

&lt;p&gt;If you’ve built a .NET app with a database, you’ve likely used Entity Framework, Dapper, or ADO.NET. They’re useful tools; still, when they live inside your business logic or controllers, the code can become harder to keep tidy and to test.&lt;/p&gt;

&lt;p&gt;That’s where the &lt;strong&gt;Repository Pattern&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;At its core, the Repository Pattern acts as a &lt;strong&gt;middle layer between your domain and data access logic&lt;/strong&gt;. It abstracts the way you store and retrieve data, giving your application a clean separation of concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Separation of Concerns:&lt;/strong&gt; Business logic doesn’t depend on the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier Testing:&lt;/strong&gt; You can replace the repository with a fake or mock during unit tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility:&lt;/strong&gt; You can switch data sources (e.g., from SQL to MongoDB) without touching business logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see how this works with a simple example.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Simple Example with Product Repository
&lt;/h2&gt;

&lt;p&gt;Imagine we’re building a small e-commerce app. We’ll start by defining a repository interface for managing products.&lt;/p&gt;

&lt;p&gt;You can find the complete sample code in this GitHub repository:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/m-aliozkaya/RepositoryPattern" rel="noopener noreferrer"&gt;https://github.com/m-aliozkaya/RepositoryPattern&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain model and context
&lt;/h3&gt;

&lt;p&gt;We start with a single entity and a matching &lt;code&gt;DbContext&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Product.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System.ComponentModel.DataAnnotations;

namespace RepositoryPattern.Web.Models;

public class Product
{
    public int Id { get; set; }

    [Required, StringLength(64)]
    public string Name { get; set; } = string.Empty;

    [Range(0, double.MaxValue)]
    public decimal Price { get; set; }

    [StringLength(256)]
    public string? Description { get; set; }

    public int Stock { get; set; }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;"AppDbContext.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;
using RepositoryPattern.Web.Models;

namespace RepositoryPattern.Web.Data;

public class AppDbContext(DbContextOptions&amp;lt;AppDbContext&amp;gt; options) : DbContext(options)
{
    public DbSet&amp;lt;Product&amp;gt; Products =&amp;gt; Set&amp;lt;Product&amp;gt;();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generic repository contract and base class
&lt;/h3&gt;

&lt;p&gt;All entities share the same CRUD needs, so we define a generic interface and an EF Core implementation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Repositories/IRepository.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System.Linq.Expressions;

namespace RepositoryPattern.Web.Repositories;

public interface IRepository&amp;lt;TEntity&amp;gt; where TEntity : class
{
    Task&amp;lt;TEntity?&amp;gt; GetByIdAsync(int id, CancellationToken cancellationToken = default);
    Task&amp;lt;List&amp;lt;TEntity&amp;gt;&amp;gt; GetAllAsync(CancellationToken cancellationToken = default);
    Task&amp;lt;List&amp;lt;TEntity&amp;gt;&amp;gt; GetListAsync(Expression&amp;lt;Func&amp;lt;TEntity, bool&amp;gt;&amp;gt; predicate, CancellationToken cancellationToken = default);
    Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);
    Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default);
    Task DeleteAsync(int id, CancellationToken cancellationToken = default);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Repositories/EfRepository.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;
using RepositoryPattern.Web.Data;

namespace RepositoryPattern.Web.Repositories;

public class EfRepository&amp;lt;TEntity&amp;gt;(AppDbContext context) : IRepository&amp;lt;TEntity&amp;gt;
    where TEntity : class
{
    protected readonly AppDbContext Context = context;

    public virtual async Task&amp;lt;TEntity?&amp;gt; GetByIdAsync(int id, CancellationToken cancellationToken = default)
        =&amp;gt; await Context.Set&amp;lt;TEntity&amp;gt;().FindAsync([id], cancellationToken);

    public virtual async Task&amp;lt;List&amp;lt;TEntity&amp;gt;&amp;gt; GetAllAsync(CancellationToken cancellationToken = default)
        =&amp;gt; await Context.Set&amp;lt;TEntity&amp;gt;().AsNoTracking().ToListAsync(cancellationToken);

    public virtual async Task&amp;lt;List&amp;lt;TEntity&amp;gt;&amp;gt; GetListAsync(
        System.Linq.Expressions.Expression&amp;lt;Func&amp;lt;TEntity, bool&amp;gt;&amp;gt; predicate,
        CancellationToken cancellationToken = default)
        =&amp;gt; await Context.Set&amp;lt;TEntity&amp;gt;()
            .AsNoTracking()
            .Where(predicate)
            .ToListAsync(cancellationToken);

    public virtual async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default)
    {
        await Context.Set&amp;lt;TEntity&amp;gt;().AddAsync(entity, cancellationToken);
        await Context.SaveChangesAsync(cancellationToken);
    }

    public virtual async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
    {
        Context.Set&amp;lt;TEntity&amp;gt;().Update(entity);
        await Context.SaveChangesAsync(cancellationToken);
    }

    public virtual async Task DeleteAsync(int id, CancellationToken cancellationToken = default)
    {
        var entity = await GetByIdAsync(id, cancellationToken);
        if (entity is null)
        {
            return;
        }

        Context.Set&amp;lt;TEntity&amp;gt;().Remove(entity);
        await Context.SaveChangesAsync(cancellationToken);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reads use &lt;code&gt;AsNoTracking()&lt;/code&gt; to avoid tracking overhead, while write methods call &lt;code&gt;SaveChangesAsync&lt;/code&gt; to keep the sample straightforward.&lt;/p&gt;

&lt;h3&gt;
  
  
  Product-specific repository
&lt;/h3&gt;

&lt;p&gt;Products need one extra query: list the items that are almost out of stock. We extend the generic repository with a dedicated interface and implementation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Repositories/IProductRepository.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using RepositoryPattern.Web.Models;

namespace RepositoryPattern.Web.Repositories;

public interface IProductRepository : IRepository&amp;lt;Product&amp;gt;
{
    Task&amp;lt;List&amp;lt;Product&amp;gt;&amp;gt; GetLowStockProductsAsync(int threshold, CancellationToken cancellationToken = default);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Repositories/ProductRepository.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.EntityFrameworkCore;
using RepositoryPattern.Web.Data;
using RepositoryPattern.Web.Models;

namespace RepositoryPattern.Web.Repositories;

public class ProductRepository(AppDbContext context) : EfRepository&amp;lt;Product&amp;gt;(context), IProductRepository
{
    public Task&amp;lt;List&amp;lt;Product&amp;gt;&amp;gt; GetLowStockProductsAsync(int threshold, CancellationToken cancellationToken = default) =&amp;gt;
        Context.Products
            .AsNoTracking()
            .Where(product =&amp;gt; product.Stock &amp;lt;= threshold)
            .OrderBy(product =&amp;gt; product.Stock)
            .ToListAsync(cancellationToken);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🧩 A Note on Unit of Work
&lt;/h3&gt;

&lt;p&gt;The Repository Pattern is often used together with the &lt;strong&gt;Unit of Work&lt;/strong&gt; pattern to manage transactions efficiently.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 *If you want to dive deeper into the Unit of Work pattern, check out our separate blog post dedicated to that topic. &lt;a href="https://abp.io/community/articles/lv4v2tyf" rel="noopener noreferrer"&gt;https://abp.io/community/articles/lv4v2tyf&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Service layer and controller
&lt;/h3&gt;

&lt;p&gt;Controllers depend on a service, and the service depends on the repository. That keeps HTTP logic and data logic separate.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Services/ProductService.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using RepositoryPattern.Web.Models;
using RepositoryPattern.Web.Repositories;

namespace RepositoryPattern.Web.Services;

public class ProductService(IProductRepository productRepository)
{
    private readonly IProductRepository _productRepository = productRepository;

    public Task&amp;lt;List&amp;lt;Product&amp;gt;&amp;gt; GetProductsAsync(CancellationToken cancellationToken = default) =&amp;gt;
        _productRepository.GetAllAsync(cancellationToken);

    public Task&amp;lt;List&amp;lt;Product&amp;gt;&amp;gt; GetLowStockAsync(int threshold, CancellationToken cancellationToken = default) =&amp;gt;
        _productRepository.GetLowStockProductsAsync(threshold, cancellationToken);

    public Task&amp;lt;Product?&amp;gt; GetByIdAsync(int id, CancellationToken cancellationToken = default) =&amp;gt;
        _productRepository.GetByIdAsync(id, cancellationToken);

    public Task CreateAsync(Product product, CancellationToken cancellationToken = default) =&amp;gt;
        _productRepository.AddAsync(product, cancellationToken);

    public Task UpdateAsync(Product product, CancellationToken cancellationToken = default) =&amp;gt;
        _productRepository.UpdateAsync(product, cancellationToken);

    public Task DeleteAsync(int id, CancellationToken cancellationToken = default) =&amp;gt;
        _productRepository.DeleteAsync(id, cancellationToken);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Controllers/ProductsController.cs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.AspNetCore.Mvc;
using RepositoryPattern.Web.Models;
using RepositoryPattern.Web.Services;

namespace RepositoryPattern.Web.Controllers;

public class ProductsController(ProductService productService) : Controller
{
    private readonly ProductService _productService = productService;

    public async Task&amp;lt;IActionResult&amp;gt; Index(CancellationToken cancellationToken)
    {
        const int lowStockThreshold = 5;
        var products = await _productService.GetProductsAsync(cancellationToken);
        var lowStock = await _productService.GetLowStockAsync(lowStockThreshold, cancellationToken);

        return View(new ProductListViewModel(products, lowStock, lowStockThreshold));
    }

    // remaining CRUD actions call through ProductService in the same way
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The controller never reaches for &lt;code&gt;AppDbContext&lt;/code&gt;. Every operation travels through the service, which keeps tests simple and makes future refactors easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependency registration and seeding
&lt;/h3&gt;

&lt;p&gt;The last step is wiring everything up in &lt;code&gt;Program.cs&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddDbContext&amp;lt;AppDbContext&amp;gt;(options =&amp;gt;
    options.UseInMemoryDatabase("ProductsDb"));
builder.Services.AddScoped(typeof(IRepository&amp;lt;&amp;gt;), typeof(EfRepository&amp;lt;&amp;gt;));
builder.Services.AddScoped&amp;lt;IProductRepository, ProductRepository&amp;gt;();
builder.Services.AddScoped&amp;lt;ProductService&amp;gt;();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sample also seeds three products so the list page shows data on first run.&lt;/p&gt;

&lt;p&gt;Run the site with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet run --project RepositoryPattern.Web

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How ABP approaches the same idea
&lt;/h2&gt;

&lt;p&gt;ABP includes generic repositories by default (&lt;code&gt;IRepository&amp;lt;TEntity, TKey&amp;gt;&lt;/code&gt;), so you often skip writing the implementation layer shown above. You inject the interface into an application service, call methods like &lt;code&gt;InsertAsync&lt;/code&gt; or &lt;code&gt;CountAsync&lt;/code&gt;, and ABP’s Unit of Work handles the transaction. When you need custom queries, you can still derive from &lt;code&gt;EfCoreRepository&amp;lt;TEntity, TKey&amp;gt;&lt;/code&gt; and add them.&lt;/p&gt;

&lt;p&gt;For more details, check out the official ABP documentation on repositories: &lt;a href="https://abp.io/docs/latest/framework/architecture/domain-driven-design/repositories" rel="noopener noreferrer"&gt;https://abp.io/docs/latest/framework/architecture/domain-driven-design/repositories&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Closing note
&lt;/h3&gt;

&lt;p&gt;This setup keeps data access tidy without being heavy. Start with the generic repository, add small extensions per entity, pass everything through services, and register the dependencies once. Whether you hand-code it or let ABP supply the repository, the structure stays the same and your controllers remain clean.&lt;/p&gt;

</description>
      <category>efcore</category>
      <category>entityframeworkcore</category>
      <category>aspnetcore</category>
      <category>repository</category>
    </item>
    <item>
      <title>Return Code vs Exceptions: Which One is Better?</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Fri, 31 Oct 2025 12:40:39 +0000</pubDate>
      <link>https://forem.com/abp_io/return-code-vs-exceptions-which-one-is-better-3pee</link>
      <guid>https://forem.com/abp_io/return-code-vs-exceptions-which-one-is-better-3pee</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Return Code vs Exceptions: Which One is Better?&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Alright, so this debate pops up every few months on dev subreddits and forums&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Should you use return codes or exceptions for error handling?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And honestly, there’s no %100 right answer here! Both have pros/cons, and depending on the language or context, one might make more sense than the other. Let’s see...&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Return Codes --- Said to be "Old School Way" ---
&lt;/h2&gt;

&lt;p&gt;Return codes (like &lt;code&gt;0&lt;/code&gt; for success, &lt;code&gt;-1&lt;/code&gt; for failure, etc.) are the OG method. You mostly see them everywhere in C and C++. They’re super explicit, the function literally &lt;em&gt;returns&lt;/em&gt; the result of the operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  ➕ Advantages of returning codes:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You &lt;em&gt;always&lt;/em&gt; know when something went wrong&lt;/li&gt;
&lt;li&gt;No hidden control flow — what you see is what you get&lt;/li&gt;
&lt;li&gt;Usually faster (no stack unwinding, no exception overhead)&lt;/li&gt;
&lt;li&gt;Easy to use in systems programming, embedded stuff, or performance-critical code&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ➖ Disadvantages of returning codes:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It’s easy to forget to check the return value (and boom, silent failure 😬)&lt;/li&gt;
&lt;li&gt;Makes code noisy... Everry function call followed by &lt;code&gt;if (result != SUCCESS)&lt;/code&gt; gets annoying&lt;/li&gt;
&lt;li&gt;No stack trace or context unless you manually build one&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try
{
    await SendEmailAsync();
}
catch (Exception e)
{
    Log.Exception(e.ToString());
    return -1;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks fine… until you forget one of those &lt;code&gt;if&lt;/code&gt; conditions somewhere.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Exceptions --- The Fancy &amp;amp; Modern Way ---
&lt;/h2&gt;

&lt;p&gt;Exceptions came in later, mostly with higher-level languages like Java, C#, and Python. The idea is that you &lt;em&gt;throw&lt;/em&gt; an error and handle it &lt;em&gt;somewhere else&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ➕ Advantages of throwing exceptions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Cleaner code... You can focus on the happy path and handle errors separately&lt;/li&gt;
&lt;li&gt;Can carry detailed info (stack traces, messages, inner exceptions...)&lt;/li&gt;
&lt;li&gt;Easier to handle complex error propagation&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ➖ Disadvantages of throwing exceptions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Hidden control flow — you don’t always see what might throw&lt;/li&gt;
&lt;li&gt;Performance hit (esp. in tight loops or low-level systems)&lt;/li&gt;
&lt;li&gt;Overused in some codebases (“everything throws everything”)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try
{
    await SendEmailAsync();
}
catch (Exception e)
{
    Log.Exception(e.ToString());
    throw e;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Way cleaner, but if &lt;code&gt;SendEmailAsync()&lt;/code&gt; is deep in your call stack and it fails, it can be tricky to know exactly what went wrong unless you log properly.&lt;/p&gt;




&lt;h3&gt;
  
  
  And Which One’s Better? ⚖️
&lt;/h3&gt;

&lt;p&gt;Depends on what you’re building.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Low-level systems, drivers, real-time stuff 👉 Return codes.&lt;/strong&gt; Performance and control matter more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application-level, business logic, or high-level APIs 👉 Exceptions.&lt;/strong&gt; Cleaner and easier to maintain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And honestly, mixing both sometimes makes sense. For example, you can use return codes internally and exceptions at the boundary of your API to surface meaningful errors to the user.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Return codes = simple, explicit, but messy.t Exceptions = clean, powerful, but can bite you. Use what fits your project and your team’s sanity level 😅.&lt;/p&gt;

</description>
      <category>exceptionhandling</category>
      <category>bestpractices</category>
      <category>errors</category>
    </item>
    <item>
      <title>5 Things You Should Keep in Mind When Deploying to a Clustered Environment</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Thu, 30 Oct 2025 07:38:37 +0000</pubDate>
      <link>https://forem.com/abp_io/5-things-you-should-keep-in-mind-when-deploying-to-a-clustered-environment-53o5</link>
      <guid>https://forem.com/abp_io/5-things-you-should-keep-in-mind-when-deploying-to-a-clustered-environment-53o5</guid>
      <description>&lt;h1&gt;
  
  
  5 Things You Should Keep in Mind When Deploying to a Clustered Environment
&lt;/h1&gt;

&lt;p&gt;Let’s be honest — moving from a single server to a cluster sounds simple on paper.&lt;br&gt;&lt;br&gt;
You just add a few more machines, right?&lt;br&gt;&lt;br&gt;
In practice, it’s the moment when small architectural mistakes start to grow legs.&lt;br&gt;&lt;br&gt;
Below are a few things that experienced engineers usually double-check before pressing that “Deploy” button.&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣ Managing State the Right Way
&lt;/h2&gt;

&lt;p&gt;Each request in a cluster might hit a different machine.&lt;br&gt;&lt;br&gt;
If your application keeps user sessions or cache in memory, that data probably won’t exist on the next node.&lt;br&gt;&lt;br&gt;
That’s why many teams decide to push state out of the app itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh51esjeexe30j8uishyt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh51esjeexe30j8uishyt.png" alt="Stateless vs Stateful" width="590" height="564"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A few real-world tips:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep sessions in &lt;strong&gt;Redis&lt;/strong&gt; or something similar instead of local memory.&lt;/li&gt;
&lt;li&gt;Design endpoints so they don’t rely on earlier requests.&lt;/li&gt;
&lt;li&gt;Don’t assume the same server will handle two requests in a row — it rarely does.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2️⃣ Shared Files and Where to Put Them
&lt;/h2&gt;

&lt;p&gt;Uploading files to local disk? That’s going to hurt in a cluster.&lt;br&gt;&lt;br&gt;
Other nodes can’t reach those files, and you’ll spend hours wondering why images disappear.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F05betqclfq1u2hssi09p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F05betqclfq1u2hssi09p.png" alt="Shared Storage" width="650" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better habits:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Push uploads to &lt;strong&gt;S3&lt;/strong&gt; , &lt;strong&gt;Azure Blob&lt;/strong&gt; , or &lt;strong&gt;Google Cloud Storage&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Send logs to a shared location instead of writing to local files.&lt;/li&gt;
&lt;li&gt;Keep environment configs in a central place so each node starts with the same settings.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3️⃣ Database Connections Aren’t Free
&lt;/h2&gt;

&lt;p&gt;Every node opens its own database connections.&lt;br&gt;&lt;br&gt;
Ten nodes with twenty connections each — that’s already two hundred open sessions.&lt;br&gt;&lt;br&gt;
The database might not love that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo7atyrc01dceik57emrr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo7atyrc01dceik57emrr.png" alt="Database Connections" width="664" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What helps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put a cap on your connection pools.&lt;/li&gt;
&lt;li&gt;Avoid keeping transactions open for too long.&lt;/li&gt;
&lt;li&gt;Tune indexes and queries before scaling horizontally.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4️⃣ Logging and Observability Matter More Than You Think
&lt;/h2&gt;

&lt;p&gt;When something breaks in a distributed system, it’s never obvious which server was responsible.&lt;br&gt;&lt;br&gt;
That’s why observability isn’t optional anymore.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzo5jr02i3cuke1f2toox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzo5jr02i3cuke1f2toox.png" alt="Observability" width="600" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider this:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stream logs to &lt;strong&gt;ELK&lt;/strong&gt; , &lt;strong&gt;Datadog&lt;/strong&gt; , or &lt;strong&gt;Grafana Loki&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Add a &lt;strong&gt;trace ID&lt;/strong&gt; to every incoming request and propagate it across services.&lt;/li&gt;
&lt;li&gt;Watch key metrics with &lt;strong&gt;Prometheus&lt;/strong&gt; and visualize them in Grafana dashboards.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5️⃣ Background Jobs and Message Queues
&lt;/h2&gt;

&lt;p&gt;If more than one node runs the same job, you might process the same data twice — or delete something by mistake.&lt;br&gt;&lt;br&gt;
You don’t want that kind of excitement in production.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu7vbgyefvrpwwvtb9vd0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu7vbgyefvrpwwvtb9vd0.png" alt="Background Jobs" width="566" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A few precautions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;strong&gt;distributed lock&lt;/strong&gt; or &lt;strong&gt;leader election&lt;/strong&gt; system.&lt;/li&gt;
&lt;li&gt;Make jobs &lt;strong&gt;idempotent&lt;/strong&gt; , so running them twice doesn’t break data.&lt;/li&gt;
&lt;li&gt;Centralize queue consumers or use a proper task scheduler.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Deploying to a cluster isn’t only about scaling up — it’s about staying stable when you do.&lt;br&gt;&lt;br&gt;
Systems that handle state, logging, and background work correctly tend to age gracefully.&lt;br&gt;&lt;br&gt;
Everything else eventually learns the hard way.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A cluster doesn’t fix design flaws — it magnifies them.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>backgroundworker</category>
      <category>clustered</category>
      <category>statemanagement</category>
      <category>logging</category>
    </item>
    <item>
      <title>Optimize Your .NET App for Production - Complete Checklist (Part 2)</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Wed, 22 Oct 2025 07:19:10 +0000</pubDate>
      <link>https://forem.com/abp_io/optimize-your-net-app-for-production-complete-checklist-part-2-3icg</link>
      <guid>https://forem.com/abp_io/optimize-your-net-app-for-production-complete-checklist-part-2-3icg</guid>
      <description>&lt;p&gt;&lt;em&gt;If you’ve landed directly on this article, note that it’s part-2 of the series. You can read part-1 here: &lt;a href="https://abp.io/community/articles/optimize-your-dotnet-app-for-production-for-any-.net-app-wa24j28e" rel="noopener noreferrer"&gt;Optimize Your .NET App for Production (Part 1)&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6) Telemetry (Logs, Metrics, Traces)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25e8g5jvru44w928w4pp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25e8g5jvru44w928w4pp.png" alt="Telemetry" width="740" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The below code adds &lt;code&gt;OpenTelemetry&lt;/code&gt; to collect app logs, metrics, and traces in .NET.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddOpenTelemetry()
  .UseOtlpExporter()
  .WithMetrics(m =&amp;gt; m.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation())
  .WithTracing(t =&amp;gt; t.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation());

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;UseOtlpExporter()&lt;/code&gt; Tells it where to send telemetry. Usually that’s an OTLP collector (like Grafana , Jaeger, Tempo, Azure Monitor). So you can visualize metrics and traces in dashboards.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WithMetrics()&lt;/code&gt; means it'll collects metrics. These metrics are Request rate (RPS), Request duration (latency), GC pauses, Exceptions, HTTP client timings.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.WithTracing(...)&lt;/code&gt; means it'll collect distributed traces. That's useful when your app calls other APIs or microservices. You can see the full request path from one service to another with timings and bottlenecks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  .NET Diagnostic Tools
&lt;/h3&gt;

&lt;p&gt;When your app is on-air, you should know about the below tools. You know in airplanes there's &lt;em&gt;black box recorder&lt;/em&gt; which is used to understand why the airplane crashed. For .NET below are our &lt;em&gt;black box recorders&lt;/em&gt;. They capture what happened without attaching a debugger.&lt;/p&gt;

&lt;p&gt;| Tool | What It Does | When to Use | | --------------------- | --------------------------------------- | ---------------------------- | | &lt;strong&gt;&lt;code&gt;dotnet-counters&lt;/code&gt;&lt;/strong&gt; | Live metrics like CPU, GC, request rate | Monitor running apps | | &lt;strong&gt;&lt;code&gt;dotnet-trace&lt;/code&gt;&lt;/strong&gt; | CPU sampling &amp;amp; performance traces | Find slow code | | &lt;strong&gt;&lt;code&gt;dotnet-gcdump&lt;/code&gt;&lt;/strong&gt; | GC heap dumps (allocations) | Diagnose memory issues | | &lt;strong&gt;&lt;code&gt;dotnet-dump&lt;/code&gt;&lt;/strong&gt; | Full process dumps | Investigate crashes or hangs | | &lt;strong&gt;&lt;code&gt;dotnet-monitor&lt;/code&gt;&lt;/strong&gt; | HTTP service exposing all the above | Collect telemetry via API |&lt;/p&gt;




&lt;h2&gt;
  
  
  7) Build &amp;amp; Run .NET App in Docker the Right Way
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feta844e51x8axgws17ga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feta844e51x8axgws17ga.png" alt="Docker" width="800" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A multi-stage build is a Docker technique where you use one image for building your app and another smaller image for running it. Why we do multi-stage build, because the .NET SDK image is big but has all the build tools. The .NET Runtime image is small and optimized for production. You copy only the published output from the build stage into the runtime stage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# build
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /app/out -p:PublishTrimmed=true -p:PublishSingleFile=true -p:ReadyToRun=true

# run
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
COPY --from=build /app/out .
ENTRYPOINT ["./YourApp"] # or ["dotnet","YourApp.dll"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll explain what these Docker file commands;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stage1: Build&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build&lt;/code&gt;Uses the .NET SDK image including compilers and tools. The &lt;code&gt;AS build&lt;/code&gt; name lets you reference this stage later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;WORKDIR /src&lt;/code&gt;Sets the working directory inside the container.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;COPY . .&lt;/code&gt;Copies your source code into the container.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;RUN dotnet restore&lt;/code&gt;Restores NuGet packages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;RUN dotnet publish ...&lt;/code&gt;Builds the project in &lt;strong&gt;Release&lt;/strong&gt; mode, optimizes it for production, and outputs it to &lt;code&gt;/app/out&lt;/code&gt;. The flags;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Stage 2: Run&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FROM mcr.microsoft.com/dotnet/aspnet:9.0&lt;/code&gt;Uses a lighter runtime image which no compiler, just the runtime.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WORKDIR /app&lt;/code&gt;Where your app will live inside the container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENV ASPNETCORE_URLS=http://+:8080&lt;/code&gt;Makes the app listen on port 8080 (and all network interfaces).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EXPOSE 8080&lt;/code&gt;Documents the port your container uses (for Docker/K8s networking).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY --from=build /app/out .&lt;/code&gt;Copies the published output from the &lt;strong&gt;build stage&lt;/strong&gt; to this final image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENTRYPOINT ["./YourApp"]&lt;/code&gt;Defines the command that runs when the container starts. If you published as a single file, it’s &lt;code&gt;./YourApp&lt;/code&gt;. f not, use &lt;code&gt;dotnet YourApp.dll&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  8) Security
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8h9g52m9zfa9r9vpae8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8h9g52m9zfa9r9vpae8b.png" alt="Security" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTPS Everywhere Even Behind Proxy
&lt;/h3&gt;

&lt;p&gt;Even if your app runs behind a reverse proxy like Nginx, Cloudflare or a load balancer, always enforce HTTPS. Why? Because internal traffic can still be captured if you don't use SSL and also cookies, HSTS, browser APIs require HTTPS. In .NET, you can easily enforce HTTPS like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.UseHttpsRedirection();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use HSTS in Production
&lt;/h3&gt;

&lt;p&gt;HSTS (HTTP Strict Transport Security) tells browsers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always use HTTPS for this domain — don’t even try HTTP again!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you set, browsers cache this rule, so users can’t accidentally hit the insecure version. You can easily enforce this as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!app.Environment.IsDevelopment())
{
    app.UseHsts();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you use HSTS, it sends browser this HTTP header: &lt;code&gt;Strict-Transport-Security: max-age=31536000; includeSubDomains&lt;/code&gt;. Browser will remember this setting for 1 year (31,536,000 seconds) that this site must only use HTTPS. And &lt;code&gt;includeSubDomains&lt;/code&gt; option applies the rule to all subdomains as well (eg: &lt;code&gt;api.abp.io&lt;/code&gt;, &lt;code&gt;cdn.abp.io&lt;/code&gt;, &lt;code&gt;account.abp.io&lt;/code&gt; etc..)&lt;/p&gt;

&lt;h3&gt;
  
  
  Store Secrets on Environment Variables or Secret Stores
&lt;/h3&gt;

&lt;p&gt;Never store passwords, connection strings, or API keys in your code or Git. Then where should we keep them?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Best/practical way is &lt;strong&gt;Environment variables&lt;/strong&gt;. You can easily sett an environment variable in a Unix-like system as below:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And you can easily access these environment variables from your .NET app like this:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or &lt;strong&gt;Secret stores&lt;/strong&gt; like: Azure Key Vault, AWS Secrets Manager, HashiCorp Vault&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Rate-Limiting to Public Endpoints
&lt;/h3&gt;

&lt;p&gt;Don't forget there'll be not naive guys who will use your app! We've many times faced this issue in the past on our public front-facing websites. So protect your public APIs from abuse, bots, and DDoS. Use rate-limiting!!! Stop brute-force attacks, prevent your resources from exhaustion...&lt;/p&gt;

&lt;p&gt;In .NET, there's a built-in rate-limit feature for .NET (System.Threading.RateLimiting):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddRateLimiter(_ =&amp;gt; _
    .AddFixedWindowLimiter("default", options =&amp;gt;
    {
        options.PermitLimit = 100;
        options.Window = TimeSpan.FromMinutes(1);
    }));

app.UseRateLimiter();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Also there's an open-source rate-limiting library -&amp;gt; &lt;a href="https://github.com/stefanprodan/AspNetCoreRateLimit" rel="noopener noreferrer"&gt;github.com/stefanprodan/AspNetCoreRateLimit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Another one -&amp;gt; &lt;a href="https://www.nuget.org/packages/Polly.RateLimiting" rel="noopener noreferrer"&gt;nuget.org/packages/Polly.RateLimiting&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Secure Cookies
&lt;/h3&gt;

&lt;p&gt;Cookies are often good targets for attacks. You must secure them properly otherwise you can face cookie stealing or CSRF attack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.Strict; // or Lax

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SecurePolicy = Always&lt;/code&gt;&lt;/strong&gt; -&amp;gt; only send cookies over HTTPS&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SameSite=Lax/Strict&lt;/code&gt;&lt;/strong&gt; -&amp;gt; prevent CSRF (Cross-Site Request Forgery)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Strict&lt;/code&gt; = safest&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Lax&lt;/code&gt; = good balance for login sessions&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  9) Startup/Cold Start
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4q33zygkuauhmtmztglk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4q33zygkuauhmtmztglk.png" alt="Cold Start / Startup" width="739" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Keep Tiered JIT On
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;JIT (Just-In-Time) compiler&lt;/strong&gt; converts your app’s Intermediate Language (IL) into native CPU instructions when the code runs. &lt;em&gt;Tiered JIT&lt;/em&gt; means the runtime uses 2 stages of compilation. Actually this setting is enabled by default in modern .NET. So just keep it on.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Tier 0 (Quick JIT):&lt;/strong&gt;Fast, low-optimization compile → gets your app running ASAP. (Used at startup.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tier 1 (Optimized JIT):&lt;/strong&gt;Later, the runtime re-compiles &lt;em&gt;hot&lt;/em&gt; methods (frequently used ones) with deeper optimizations for speed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Use PGO (Profile-Guided Optimization)
&lt;/h3&gt;

&lt;p&gt;PGO lets .NET learn from real usage of your app. It profiles which functions are used most often, then re-optimizes the build for that pattern. You can think of it as the runtime saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I’ve seen what your app actually does... I’ll rearrange and optimize code paths accordingly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In .NET 8+, you don’t have to manually enable PGO (Profile-Guided Optimization). The JIT collects runtime profiling data (e.g. which types are common, branch predictions) and uses it to generate more optimized code later. In .NET 9, PGO has been improved: the JIT uses PGO data for more patterns (like type checks / casts) and makes better decisions.&lt;/p&gt;




&lt;h2&gt;
  
  
  10) Graceful Shutdown
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4zj0o2on0v24h48eski.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4zj0o2on0v24h48eski.png" alt="Shutdown" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we break up with our lover, we often argue and regret it later. When an application breaks up with an operating system, it should be done well 😘 ... When your app stops, maybe you deploy a new version or Kubernetes restarts a pod... the OS sends a signal called &lt;code&gt;SIGTERM&lt;/code&gt; (terminate). A &lt;strong&gt;graceful shutdown&lt;/strong&gt; means handling that signal properly, finishing what’s running, cleaning up, and exiting cleanly (like an adult)!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var app = builder.Build();
var lifetime = app.Services.GetRequiredService&amp;lt;IHostApplicationLifetime&amp;gt;();
lifetime.ApplicationStopping.Register(() =&amp;gt;
{
    // stop accepting, finish in-flight, flush telemetry
});
app.Run();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On K8s, set &lt;code&gt;terminationGracePeriodSeconds&lt;/code&gt; and wire &lt;strong&gt;readiness&lt;/strong&gt; /startup probes.&lt;/p&gt;




&lt;h2&gt;
  
  
  11) Load Test
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5gmy0ywtjrk8dr7xlf7h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5gmy0ywtjrk8dr7xlf7h.png" alt="Load Test" width="742" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes arguing with our lover is good. We can see her/his face before marrying 😀 Use &lt;strong&gt;k6&lt;/strong&gt; or &lt;strong&gt;bombardier&lt;/strong&gt; and test with realistic payloads and prod-like limits. Don't be surprise later when your app is running on prod! These topics should be tested: &lt;code&gt;CPU %&lt;/code&gt; , &lt;code&gt;Time in GC&lt;/code&gt; , &lt;code&gt;LOH Allocations&lt;/code&gt; , &lt;code&gt;ThreadPool Queue Length&lt;/code&gt; and &lt;code&gt;Socket Exhaustion&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  About K6
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A modern load testing tool, using Go and JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;29K stars on GitHub&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GitHub address: &lt;a href="https://github.com/grafana/k6" rel="noopener noreferrer"&gt;https://github.com/grafana/k6&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  About Bombardier
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fast cross-platform HTTP benchmarking tool written in Go.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;7K stars on GitHub&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GitHub address: &lt;a href="https://github.com/codesenberg/bombardier" rel="noopener noreferrer"&gt;https://github.com/codesenberg/bombardier&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://trends.google.com/trends/explore?cat=31&amp;amp;q=bombardier%20%2B%20benchmarking,k6%20%2B%20benchmarking" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvcazr7mjlfevzibad49.png" alt="Bombardier vs K6" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In summary, I listed 11 items for optimizing a .NET application for production; Covering build configuration, hosting setup, runtime behavior, data access, telemetry, containerization, security, startup performance and reliability under load. By applying the checklist from Part 1 and Part 2 of this series, leveraging techniques like trimmed releases, server GC, minimal payloads, pooled &lt;code&gt;DbContexts&lt;/code&gt;, OpenTelemetry, multi-stage Docker builds, HTTPS enforcement, and proper shutdown handling—you’ll improve your app’s durability, scalability and maintainability under real-world traffic and production constraints. Each item is a checkpoint and you’ll be able to deliver a robust, high-performing .NET application ready for live users.&lt;/p&gt;

&lt;p&gt;🎉 Want top-tier .NET performance without the headaches? Try &lt;a href="https://abp.io?utm_source=alper-ebicoglu-performance-article" rel="noopener noreferrer"&gt;ABP Framework&lt;/a&gt; for best-performance and skip all the hustles of .NET app development.&lt;/p&gt;

</description>
      <category>kestrel</category>
      <category>dotnet</category>
      <category>performance</category>
      <category>optimize</category>
    </item>
    <item>
      <title>Optimize Your .NET App for Production - Complete Checklist (Part 1)</title>
      <dc:creator>ABP.IO</dc:creator>
      <pubDate>Wed, 22 Oct 2025 07:00:25 +0000</pubDate>
      <link>https://forem.com/abp_io/optimize-your-net-app-for-production-complete-checklist-part-1-k3o</link>
      <guid>https://forem.com/abp_io/optimize-your-net-app-for-production-complete-checklist-part-1-k3o</guid>
      <description>&lt;h1&gt;
  
  
  Optimize Your .NET App for Production (Complete Checklist)
&lt;/h1&gt;

&lt;p&gt;I see way too many .NET apps go to prod like it’s still “F5 on my laptop.” Here’s the checklist I wish someone shoved me years ago. It’s opinionated, pragmatic, copy-pasteable.&lt;/p&gt;




&lt;h2&gt;
  
  
  1) Publish Command and CSPROJ Settings
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fno21ug7kdvejn63l2j1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fno21ug7kdvejn63l2j1s.png" alt="Publish Command and CSPROJ Setting" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Never go to production with debug build! See the below command which publishes properly a .NET app for production.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet publish -c Release -o out -p:PublishTrimmed=true -p:PublishSingleFile=true -p:ReadyToRun=true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;csproj&lt;/code&gt; for the optimum production publish:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;PropertyGroup&amp;gt;
  &amp;lt;PublishReadyToRun&amp;gt;true&amp;lt;/PublishReadyToRun&amp;gt;
  &amp;lt;PublishTrimmed&amp;gt;true&amp;lt;/PublishTrimmed&amp;gt;
  &amp;lt;InvariantGlobalization&amp;gt;true&amp;lt;/InvariantGlobalization&amp;gt;
  &amp;lt;TieredCompilation&amp;gt;true&amp;lt;/TieredCompilation&amp;gt;
&amp;lt;/PropertyGroup&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PublishTrimmed&lt;/strong&gt; It's trimmimg assemblies. What's that!? It removes unused code from your application and its dependencies, hence it reduces the output files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PublishReadyToRun&lt;/strong&gt; When you normally build a .NET app, your C# code is compiled into &lt;strong&gt;IL&lt;/strong&gt; (Intrmediate Language). When your app runs, the JIT Compiler turns that IL code into native CPU commands. But this takes much time on startup. When you enable &lt;code&gt;PublishReadyToRun&lt;/code&gt;, the build process precompiles your IL into native code and it's called AOT (Ahead Of Time). Hence your app starts faster... But the downside is; the output files are now a bit bigger. Another thing; it'll compile only for a specific OS like Windows and will not run on Linux anymore.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Self-contained&lt;/strong&gt; When you publish your .NET app this way, it ncludes the .NET runtime inside your app files. It will run even on a machine that doesn’t have .NET installed. The output size gets larger, but the runtime version is exactly what you built with.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2) Kestrel Hosting
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyafse2bmwmevyi5nj55p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyafse2bmwmevyi5nj55p.png" alt="Kestrel Hosting" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, ASP.NET Core app listen only &lt;code&gt;localhost&lt;/code&gt;, it means it accepts requests only from inside the machine. When you deploy to Docker or Kubernetes, the container’s internal network needs to expose the app to the outside world. To do this you can set it via environment variable as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ASPNETCORE_URLS=http://0.0.0.0:8080

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also if you’re building an internall API or a containerized microservice which is not multilngual, then add also the below setting. it disables operating system's globalization to reduce image size and dependencies..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean &lt;code&gt;Program.cs&lt;/code&gt; startup! Here's a minimal &lt;code&gt;Program.cs&lt;/code&gt; which includes just the essential middleware and settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);

builder.Logging.ClearProviders();
builder.Logging.AddConsole();

builder.Services.AddResponseCompression();
builder.Services.AddResponseCaching();
builder.Services.AddHealthChecks();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/error");
    app.UseHsts();
}

app.UseResponseCompression();
app.UseResponseCaching();

app.MapHealthChecks("/health");
app.MapGet("/error", () =&amp;gt; Results.Problem(statusCode: 500));

app.Run();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3) Garbage Collection and ThreadPool
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0g5865k50ppvd0qmwdr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0g5865k50ppvd0qmwdr.png" alt="Garbage Collection and ThreadPool" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  GC Memory Cleanup Mode
&lt;/h3&gt;

&lt;p&gt;GC (Garbage Collection) is how .NET automatically frees memory. There are two main modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Workstation GC:&lt;/strong&gt; good for desktop apps (focuses on responsiveness)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server GC:&lt;/strong&gt; good for servers (focuses on throughput)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The below environment variable is telling the .NET runtime to use the &lt;em&gt;Server Garbage Collector (Server GC)&lt;/em&gt; instead of the &lt;em&gt;Workstation GC&lt;/em&gt;. Because our ASP.NET Core app must be optmized for servers not personal computers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COMPlus_gcServer=1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  GC Limit Memory Usage
&lt;/h3&gt;

&lt;p&gt;Use at max 60% of the total available memory for the managed heap (the memory that .NET’s GC controls). So if your container or VM has, let's say 4 GB of RAM, .NET will try to keep the GC heap below 2.4 GB (60% of 4 GB). Especially when you run your app in containers, don’t let the GC assume host memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COMPlus_GCHeapHardLimitPercent=60

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Thread Pool Warm-up
&lt;/h3&gt;

&lt;p&gt;When your .NET app runs, it uses a thread pool. This is for handling background work like HTTP requests, async tasks, I/O things... By default, the thread pool starts small and grows dynamically as load increases. That’s good for desktop apps but for server apps it's too slow! Because during sudden peek of traffic, the app might waste time creating threads instead of handling requests. So below code keeps at least 200 worker threads and 200 I/O completion threads ready to go even if they’re idle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ThreadPool.SetMinThreads(200, 200);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  4) HTTP Performance
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqfjlvefw5irqtslsdtn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqfjlvefw5irqtslsdtn4.png" alt="HTTP Performance" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP Response Compression
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;AddResponseCompression()&lt;/code&gt; enables HTTP response compression. It shrinks your outgoing responses before sending them to the client. Making smaller payloads for faster responses and uses less bandwidth. Default compression method is &lt;code&gt;Gzip&lt;/code&gt;. You can also add &lt;code&gt;Brotli&lt;/code&gt; compression. &lt;code&gt;Brotli&lt;/code&gt; is great for APIs returning JSON or text. If your CPU is already busy, keep the default &lt;code&gt;Gzip&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddResponseCompression(options =&amp;gt;
{
    options.Providers.Add&amp;lt;BrotliCompressionProvider&amp;gt;();
    options.EnableForHttps = true;
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  HTTP Response Caching
&lt;/h3&gt;

&lt;p&gt;Use caching for GET endpoints where data doesn’t change often (e.g., configs, reference data). &lt;code&gt;ETags&lt;/code&gt; and &lt;code&gt;Last-Modified&lt;/code&gt; headers tell browsers or proxies skip downloading data that hasn’t changed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ETag&lt;/strong&gt; = a version token for your resource.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Last-Modified&lt;/strong&gt; = timestamp of last change.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a client sends &lt;code&gt;If-None-Match: "abc123"&lt;/code&gt; and your resource’s &lt;code&gt;ETag&lt;/code&gt; hasn’t changed, .NET automatically returns &lt;code&gt;304 Not Modified&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP/2 or HTTP/3
&lt;/h3&gt;

&lt;p&gt;These newer protocols make web requests faster and smoother. It's good for microservices or frontends making many API calls.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP/2&lt;/strong&gt; : multiplexing (many requests over one TCP connection).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP/3&lt;/strong&gt; : uses QUIC (UDP) for even lower latency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can enable them on your reverse proxy (Nginx, Caddy, Kestrel)... .NET supports both out of the box if your environment allows it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Minimal Payloads with DTOs
&lt;/h3&gt;

&lt;p&gt;The best practise here is; Never send/recieve your entire database entity, use DTOs. In the DTOs include only the fields the client actually needs by doing so you will keep the responses smaller and even safer. Also, prefer &lt;code&gt;System.Text.Json&lt;/code&gt; (now it’s faster than &lt;code&gt;Newtonsoft.Json&lt;/code&gt;) and for very high-traffic APIs, use source generation to remove reflection overhead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//define your entity DTO
[JsonSerializable(typeof(MyDto))]
internal partial class MyJsonContext : JsonSerializerContext { }

//and simply serialize like this    
var json = JsonSerializer.Serialize(dto, MyJsonContext.Default.MyDto)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5) Data Layer (Mostly Where Most Apps Slow Down!)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rl35a1s3pge4do4c934.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rl35a1s3pge4do4c934.png" alt="Data Layer" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reuse &lt;code&gt;DbContext&lt;/code&gt; via Factory (Pooling)
&lt;/h3&gt;

&lt;p&gt;Creating a new &lt;code&gt;DbContext&lt;/code&gt; for every query is expensive! Use &lt;code&gt;IDbContextFactory&amp;lt;TContext&amp;gt;&lt;/code&gt;, it gives you pooled &lt;code&gt;DbContext&lt;/code&gt; instances from a pool that reuses objects instead of creating them from scratch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services.AddDbContextFactory&amp;lt;AppDbContext&amp;gt;(options =&amp;gt;
    options.UseSqlServer(connectionString));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then inject the factory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using var db = _contextFactory.CreateDbContext();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, ensure your database server (SQL Server, PostgreSQL....) has &lt;strong&gt;connection pooling enabled&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  N+1 Query Problem
&lt;/h3&gt;

&lt;p&gt;The N+1 problem occurs when your app runs &lt;strong&gt;one query for the main data&lt;/strong&gt; , then &lt;strong&gt;N more queries for related entities&lt;/strong&gt;. That kills performance!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bad-Practise:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var users = await context.Users.Include(u =&amp;gt; u.Orders).ToListAsync();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Good-Practise:&lt;/strong&gt; Project to DTOs using &lt;code&gt;.Select()&lt;/code&gt; so EF-Core generates a single optimized SQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var users = await context.Users.Select(u =&amp;gt; new UserDto
   {
        Id = u.Id,
        Name = u.Name,
        OrderCount = u.Orders.Count
    }).ToListAsync();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Indexes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Use EF Core logging, SQL Server Profiler, or &lt;code&gt;EXPLAIN&lt;/code&gt; (Postgres/MySQL) to find slow queries. Add missing indexes &lt;strong&gt;only&lt;/strong&gt; where needed. For example &lt;a href="https://blog.sqlauthority.com/2011/01/03/sql-server-2008-missing-index-script-download/" rel="noopener noreferrer"&gt;at this page&lt;/a&gt;, he wrote an SQL query which lists missing index list (also there's another version at &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-db-missing-index-details-transact-sql?view=sql-server-ver17" rel="noopener noreferrer"&gt;Microsoft Docs&lt;/a&gt;). This perf improvement is mostly applied after running the app for a period of time.&lt;/p&gt;




&lt;h3&gt;
  
  
  Migrations
&lt;/h3&gt;

&lt;p&gt;In production run migrations manually, never do it on app startup. That way you can review schema changes, back up data and avoid breaking the live DB.&lt;/p&gt;




&lt;h3&gt;
  
  
  Resilience with Polly
&lt;/h3&gt;

&lt;p&gt;Use &lt;a href="https://www.pollydocs.org/" rel="noopener noreferrer"&gt;Polly&lt;/a&gt; for retries, timeouts and circuit breakers for your DB or HTTP calls. Handles short outages gracefully&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To keep the article short and for the better readability I spitted it into 2 parts 👉 &lt;a href="https://abp.io/community/articles/optimize-your-dotnet-app-for-production-for-any-.net-app-2-78xgncpi" rel="noopener noreferrer"&gt;Continue with the second part here&lt;/a&gt;...&lt;/em&gt;&lt;/p&gt;

</description>
      <category>performance</category>
      <category>optimize</category>
      <category>aspnetcore</category>
      <category>kestrel</category>
    </item>
  </channel>
</rss>
