Tutorials Microservices with .NET

Implementing User Microservice Domain Layer — Complete Guide

Implementing User Microservice Domain Layer — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Microservices with .NET on Toolliyo Academy.

On this page

Microservices with .NET · Lesson 13 of 131

Implementing User Microservice Domain Layer

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Foundations · ~6 min · Module 2: Building User Microservice

What is this?

The Domain layer holds User entity, value objects (Email), domain rules, and repository interfaces — no EF, no HTTP.

Why should you care?

Business rules like “email must be unique” belong here so Application and tests share one truth.

See it live — copy this example

Create a Web API project (dotnet new webapi), paste the code, then run dotnet run.

namespace User.Domain.Entities;

public class UserProfile
{
    public Guid Id { get; private set; }
    public string Name { get; private set; } = "";
    public Email Email { get; private set; } = null!;

    public static UserProfile Create(string name, string email)
    {
        if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Name required");
        return new UserProfile { Id = Guid.NewGuid(), Name = name.Trim(), Email = Email.Create(email) };
    }
}

public interface IUserRepository
{
    Task<UserProfile?> GetByIdAsync(Guid id, CancellationToken ct = default);
    Task AddAsync(UserProfile user, CancellationToken ct = default);
}

Run Example »

Edit the code and click Run — like W3Schools Try it Yourself.

Code
Result

What happened?

  • Create factory method validates invariants.
  • IUserRepository is implemented in Infrastructure — Domain only declares the contract.

Try it yourself

  1. Add UserProfile and Email value object.
  2. Add IUserRepository interface.
  3. Write one unit test for UserProfile.Create with invalid email.
  4. Change a string or route in the example and save — watch Swagger or the RabbitMQ Management UI update.
  5. Break the code on purpose (remove a semicolon), read the error message, then fix it.

Remember

Domain = entities + rules + interfaces. No EF or ASP.NET references. Factory methods enforce invariants.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Identify Business Domains: Use Domain-Driven Design (DDD) to identify?
Short answer: business capabilities that can be made into separate services. Real-world example (ShopNest) After payment succeeds, ShopNest publishes OrderPaid . Inventory and Notification services react independently—no…
Mid PDF Detailed
URI Versioning: Include the version in the URL, such as /api/v1/users.?
Short answer: Example: GET /api/v1/orders/{id} vs. GET /api/v2/orders/{id} GET /api/v1/orders/{id} vs. GET /api/v2/orders/{id} Example code GET /api/v1/orders/{id} vs. GET /api/v2/orders/{id} GET /api/v1/orders/{id} vs.…
Mid PDF Detailed
Shard by Business Domain: Each service can own its own database, and the data?
Short answer: can be partitioned by business domain. For example, the Order Service might have its own database, and the Customer Service has another one. Say this in the interview Define — one clear sentence (the short…
Mid PDF Detailed
Test Full User Journeys: Simulate real user workflows that span multiple?
Short answer: Test Full User Journeys: Simulate real user workflows that span multiple? is a common interview topic in Microservices. Give a clear definition, then one concrete example. Real-world example (ShopNest) Shop…
Senior PDF Detailed
Distributed Transactions: Using Two-Phase Commit (2PC) or Sagas to ensure?
Short answer: that a transaction either commits or rolls back across multiple services. Real-world example (ShopNest) After payment succeeds, ShopNest publishes OrderPaid . Inventory and Notification services react indep…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Microservices with .NET
Course syllabus

Microservices with .NET Tutorial

Module 1: Foundations and Fundamentals
Module 2: Building User Microservice
Module 3: ShopNest Services and Integration
Module 4: RabbitMQ and Messaging
Module 5: Saga and Distributed Transactions
Module 6: API Gateway
Module 7: gRPC, CQRS, and GraphQL
Module 8: Resiliency and Fault Tolerance
Module 9: DevOps and Cloud-Native
Module 10: Git and GitHub
Module 11: CI/CD Pipelines
Module 12: Observability and Testing
Module 13: Advanced Topics
Module 14: Real-World Enterprise Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details