Sunlix.NET.DDD.BaseTypes 1.0.4

Prefix Reserved
dotnet add package Sunlix.NET.DDD.BaseTypes --version 1.0.4
                    
NuGet\Install-Package Sunlix.NET.DDD.BaseTypes -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sunlix.NET.DDD.BaseTypes" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sunlix.NET.DDD.BaseTypes" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Sunlix.NET.DDD.BaseTypes" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sunlix.NET.DDD.BaseTypes --version 1.0.4
                    
#r "nuget: Sunlix.NET.DDD.BaseTypes, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sunlix.NET.DDD.BaseTypes@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sunlix.NET.DDD.BaseTypes&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Sunlix.NET.DDD.BaseTypes&version=1.0.4
                    
Install as a Cake Tool

Sunlix.NET.DDD.BaseTypes

.NET NuGet NuGet Downloads GitHub license

Sunlix.NET.DDD.BaseTypes is a lightweight and extensible library designed for building robust domain models in C#. It provides a clean foundation for implementing Domain-Driven Design (DDD) patterns by introducing essential base types: Entity, ValueObject, Enumeration, Error, and Unit.

These primitives help developers write more expressive, consistent, and maintainable domain logic while reducing boilerplate code. The library is framework-agnostic, making it suitable for microservices, monoliths, and modular applications.

Why this Library

  • Less boilerplate — concise base types for entities and value objects.
  • Clear equality semantics — identity-based for entities, structural for value objects.
  • Extensible enumerations — strongly typed, with both data and behavior.
  • Consistent error model — represent validation failures and business rule violations without relying on exceptions.
  • ORM-friendly — supports Entity Framework Core, NHibernate; safe for proxies and DB-generated identifiers.
  • Addresses common pitfalls — correct equality, consistent hash codes, proxy-safe type resolution.

Full documentation & FAQ: see the GitHub README.


Installation

dotnet add package Sunlix.NET.DDD.BaseTypes

Usage

Entity<TId>

Entities represent domain concepts that must be individually tracked over time. Each entity has a unique identity that distinguishes it from all others and enables references across the system (e.g. Order, Customer or Account).

Example:

public sealed class Book : Entity<int>
{
    private Book() { }   // for ORM
    public Book(string title) => Title = title;

    public string Title { get; private set; } = string.Empty;

    protected override Type UnproxiedType => typeof(Book);
}

ValueObject

A value object models a descriptive aspect of the domain rather than a distinct, trackable thing. Typical examples include money amounts, dates and ranges, measurements, addresses, and email addresses.

Example:

public sealed class Money : ValueObject
{
    public decimal Amount { get; }
    public string Currency { get; }

    public Money(decimal amount, string currency) =>
        (Amount, Currency) = (amount, currency.ToUpperInvariant());

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Amount;
        yield return Currency;
    }
}

Enumeration<T>

An Enumeration<T> represents a closed set of named values that carry domain meaning and may also include behavior.

Example:

public sealed class TaxCategory : Enumeration<TaxCategory>
{
    private TaxCategory(int value, string name, decimal rate) : base(value, name) =>
        Rate = rate;

    public decimal Rate { get; }
    public decimal ApplyTax(decimal net) => Math.Round(net * (1 + Rate), 2);

    public static readonly TaxCategory Standard = new(0, "Standard", 0.20m);
    public static readonly TaxCategory Reduced  = new(1, "Reduced",  0.10m);
    public static readonly TaxCategory Zero     = new(2, "Zero",     0.00m);
}

Error

An Error is a lightweight value object that encapsulates a code and a descriptive message to represent failures in a structured form.

Example:

public sealed class Error : ValueObject
{
    public string Code { get; }
    public string Message { get; }

    public Error(string code, string message) =>
        (Code, Message) = (code, message);

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Code;
    }
}

Unit

Unit is a lightweight structure that represents the absence of a meaningful result — essentially a semantic alternative to void.

Example:

public readonly record struct Result<T>(T? Value, Error? Error)
{
    public bool IsSuccess => Error is null;
    public static Result<T> Ok(T value) => new(value, null);
    public static Result<T> Fail(Error e) => new(default, e);
}

public Result<Unit> Save(User user) =>
    user.IsValid ? Result<Unit>.Ok(Unit.value)
                 : Result<Unit>.Fail(new Error("invalid_user", "User validation failed"));
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Sunlix.NET.DDD.BaseTypes:

Package Downloads
Sunlix.NET.DDD.ROP

Sunlix.NET.DDD.ROP is a lightweight implementation of the Railway-Oriented Programming pattern for C#. It provides a composable Result type and a set of functional operators for building clear and predictable execution pipelines without exceptions.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 244 8/25/2025
1.0.3 192 8/21/2025
1.0.2 177 8/20/2025
1.0.1 196 8/17/2025
1.0.0 184 8/17/2025

1.0.4 - 2025-08-24
Changed
- Entity<TId>.IsTransient converted from method to property.
- Added support for .NET 6.0 and .NET 8.0.

Fixed
- Fixed XML documentation.