CoreKernel.Primitives
1.0.0
See the version list below for details.
dotnet add package CoreKernel.Primitives --version 1.0.0
NuGet\Install-Package CoreKernel.Primitives -Version 1.0.0
<PackageReference Include="CoreKernel.Primitives" Version="1.0.0" />
<PackageVersion Include="CoreKernel.Primitives" Version="1.0.0" />
<PackageReference Include="CoreKernel.Primitives" />
paket add CoreKernel.Primitives --version 1.0.0
#r "nuget: CoreKernel.Primitives, 1.0.0"
#:package CoreKernel.Primitives@1.0.0
#addin nuget:?package=CoreKernel.Primitives&version=1.0.0
#tool nuget:?package=CoreKernel.Primitives&version=1.0.0
CoreKernel.Primitives Library
The CoreKernel.Primitives
library provides foundational building blocks for Domain-Driven Design (DDD) in C#. It includes abstractions for entities, aggregate roots, value objects, and strongly-typed IDs, empowering developers to model business domains with clarity and consistency.
Table of Contents
Overview
The CoreKernel.Primitives
project encapsulates essential DDD concepts as reusable abstractions. It enables consistent modeling of domain entities, immutability of value objects, and explicit handling of domain events. By enforcing these patterns early, it ensures better code maintainability and domain integrity.
Features
Entity Base Class
- Implements identity-based equality comparison.
- Supports generic ID types and immutability by default.
Aggregate Root Base Class
- Extends
Entity<TId>
with domain event tracking. - Provides methods to raise, retrieve, and clear domain events.
- Extends
Domain Event Marker Interface
- A lightweight interface for tagging domain events in the model layer.
Value Object Base Class
- Implements structural equality via atomic values.
- Promotes immutability and encapsulation.
Strongly-Typed ID Base Class
- Prevents accidental type mismatches in identifiers.
- Enforces type-safe ID value representation.
Installation
Add the CoreKernel.Primitives
package to your project:
dotnet add package CoreKernel.Primitives
Usage
Entities
The Entity<TId>
base class provides equality comparison based on the identifier:
public class Product : Entity<Guid>
{
public string Name { get; }
public Product(Guid id, string name) : base(id)
{
Name = name;
}
}
Entities are compared by ID and type:
var product1 = new Product(Guid.NewGuid(), "Widget");
var product2 = new Product(product1.Id, "Widget");
bool areEqual = product1 == product2; // true
Aggregate Roots
Aggregate roots represent the entry point to a domain aggregate. They can raise domain events:
public class Order : AggregateRoot<Guid>
{
public Order(Guid id) : base(id) { }
public void Place()
{
RaiseDomainEvent(new OrderPlaced(Id));
}
}
Working with domain events:
var order = new Order(Guid.NewGuid());
order.Place();
var events = order.GetDomainEvents(); // IReadOnlyCollection<IDomainEvent>
order.ClearDomainEvents(); // After publishing
Domain Events
The IDomainEvent
interface is a simple marker used to denote significant changes in the domain:
public class OrderPlaced : IDomainEvent
{
public Guid OrderId { get; }
public OrderPlaced(Guid orderId)
{
OrderId = orderId;
}
}
This pattern allows domain models to remain decoupled from event publishing concerns.
Value Objects
Create value objects by inheriting from ValueObject
and defining atomic values:
public class Money : ValueObject
{
public decimal Amount { get; }
public string Currency { get; }
public Money(decimal amount, string currency)
{
Amount = amount;
Currency = currency;
}
protected override IEnumerable<object> GetAtomicValues()
{
yield return Amount;
yield return Currency;
}
}
Value objects are compared by structure, not reference:
var price1 = new Money(100, "USD");
var price2 = new Money(100, "USD");
bool areEqual = price1 == price2; // true
Strongly-Typed IDs
Define a custom ID by inheriting from StronglyTypedId<T>
:
public class UserId : StronglyTypedId<Guid>
{
public UserId(Guid value) : base(value) { }
}
Usage:
var id1 = new UserId(Guid.NewGuid());
var id2 = new UserId(id1.Value);
bool areEqual = id1 == id2; // true (if same GUID)
This approach eliminates accidental ID misuse between different entity types.
Best Practices
- Use Value Objects for any data without identity (e.g., Address, Money, Range).
- Derive all domain entities from
Entity<TId>
to ensure consistent equality logic. - Only expose aggregate roots in your repositories to maintain aggregate boundaries.
- Raise domain events inside aggregate methods to maintain invariants.
- Leverage
StronglyTypedId<T>
to prevent bugs caused by mixing ID types. - Avoid setters in entities and value objects to preserve immutability.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on CoreKernel.Primitives:
Package | Downloads |
---|---|
CoreKernel
CoreKernel is a modular .NET library that provides reusable primitives, functional utilities, domain markers, and messaging interfaces for building clean, maintainable, and domain-driven applications. |
|
CoreKernel.Messaging
Interfaces for implementing CQRS and Domain Event patterns in .NET using Commands, Queries, Events, and Handlers. |
GitHub repositories
This package is not used by any popular GitHub repositories.