Nera.Lib.Domain
1.0.5
dotnet add package Nera.Lib.Domain --version 1.0.5
NuGet\Install-Package Nera.Lib.Domain -Version 1.0.5
<PackageReference Include="Nera.Lib.Domain" Version="1.0.5" />
<PackageVersion Include="Nera.Lib.Domain" Version="1.0.5" />
<PackageReference Include="Nera.Lib.Domain" />
paket add Nera.Lib.Domain --version 1.0.5
#r "nuget: Nera.Lib.Domain, 1.0.5"
#:package Nera.Lib.Domain@1.0.5
#addin nuget:?package=Nera.Lib.Domain&version=1.0.5
#tool nuget:?package=Nera.Lib.Domain&version=1.0.5
Nera.Lib.Domain
A comprehensive .NET 9 library providing domain-driven design (DDD) building blocks for Nera applications. This library includes essential domain modeling constructs such as entities, value objects, aggregates, domain events, and CQRS patterns.
Features
🏗️ Domain Building Blocks
- Entities: Base classes for domain entities with built-in auditing and tracking
- Value Objects: Immutable objects distinguished by their state
- Aggregates: Aggregate root implementation with domain event support
- Domain Events: Event-driven architecture support with MediatR integration
🔄 CQRS Support
- Command Handlers: Base classes for command processing
- Query Handlers: Base classes for query processing
- Behaviors: Logging and validation pipeline behaviors
- Domain Event Handlers: Reactive event processing
📦 Repository Patterns
- Generic Repository: Standard CRUD operations
- Read/Write Separation: Separate read and write repositories
- Specification Pattern: Query specification support
- Unit of Work: Transaction management
🎯 Additional Features
- Auditable Entities: Automatic tracking of creation and modification
- Multi-tenant Support: Built-in tenant isolation
- Pagination: Standardized pagination utilities
- Filters & Sorting: Query filtering and ordering capabilities
- Outbox Pattern: Reliable event publishing
Installation
Install the package via NuGet Package Manager:
dotnet add package Nera.Lib.Domain
Or via Package Manager Console:
Install-Package Nera.Lib.Domain
Quick Start
1. Creating an Entity
using Nera.Lib.Domain.Entities;
public class Product : BaseEntity<Guid>
{
public string Name { get; private set; }
public decimal Price { get; private set; }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
public void UpdatePrice(decimal newPrice)
{
if (newPrice <= 0)
throw new ArgumentException("Price must be positive");
Price = newPrice;
// Domain event will be automatically tracked
AddDomainEvent(new ProductPriceChangedEvent(Id, newPrice));
}
}
2. Creating a Value Object
using Nera.Lib.Domain.ValueObjects;
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> GetEqualityComponents()
{
yield return Amount;
yield return Currency;
}
}
3. Creating an Aggregate Root
using Nera.Lib.Domain.Aggregates;
public class Order : AggregateRoot<Guid>
{
private readonly List<OrderItem> _items = new();
public string OrderNumber { get; private set; }
public OrderStatus Status { get; private set; }
public IReadOnlyList<OrderItem> Items => _items.AsReadOnly();
public Order(string orderNumber)
{
OrderNumber = orderNumber;
Status = OrderStatus.Draft;
AddDomainEvent(new OrderCreatedEvent(Id, orderNumber));
}
public void AddItem(Product product, int quantity)
{
var item = new OrderItem(product.Id, quantity, product.Price);
_items.Add(item);
AddDomainEvent(new OrderItemAddedEvent(Id, item.ProductId, quantity));
}
}
4. Using CQRS
using Nera.Lib.Domain.CQRS;
// Command
public record CreateProductCommand(string Name, decimal Price) : ICommand<Guid>;
// Command Handler
public class CreateProductHandler : BaseCommandHandler<CreateProductCommand, Guid>
{
private readonly IRepository<Product> _repository;
public CreateProductHandler(IRepository<Product> repository)
{
_repository = repository;
}
protected override async Task<Guid> ExecuteAsync(
CreateProductCommand request,
CancellationToken cancellationToken)
{
var product = new Product(request.Name, request.Price);
await _repository.AddAsync(product, cancellationToken);
return product.Id;
}
}
5. Repository Usage
using Nera.Lib.Domain.Repositories;
public interface IProductRepository : IRepository<Product>
{
Task<IEnumerable<Product>> GetByPriceRangeAsync(decimal min, decimal max);
}
public class ProductRepository : IProductRepository
{
// Implementation using your preferred ORM
}
Architecture Components
Entity Hierarchy
IEntity<TKey>
├── BaseEntity<TKey>
│ ├── AuditableEntity<TKey>
│ └── Entity<TKey>
└── AggregateRoot<TKey>
Event System
- Domain Events: Business events that occur within aggregates
- Event Publisher: Publishes events to registered handlers
- Event Handlers: Process domain events reactively
Repository Pattern
- IReadRepository: Read-only operations
- IWriteRepository: Write operations
- IRepository: Combined read/write interface
- ISpecification: Query specifications
Configuration
Dependency Injection
// Program.cs or Startup.cs
services.AddScoped<IRepository<Product>, ProductRepository>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
// Register MediatR for CQRS
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
Entity Framework Integration
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure your entities
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
}
}
Best Practices
- Domain Events: Use domain events for cross-aggregate communication
- Value Objects: Prefer value objects for concepts without identity
- Aggregates: Keep aggregates small and focused
- Repository: Use specifications for complex queries
- CQRS: Separate read and write operations for better performance
Dependencies
- .NET 9.0: Target framework
- MediatR.Contracts: For CQRS pattern implementation
- Nera.Lib.Core: Core utilities and extensions
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support and questions, please:
- Create an issue in the repository
- Contact the Nextera Systems team
- Review the documentation and examples
Authors
Nextera Systems - Initial work and maintenance
Part of the Nera Libraries ecosystem for building robust, scalable applications.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net9.0
- MediatR.Contracts (>= 2.0.1)
- Nera.Lib.Core (>= 1.0.6)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Nera.Lib.Domain:
Package | Downloads |
---|---|
Nera.Lib.Web
Web models, business rules, aggregates, value objects, and domain services for Nera applications |
|
Nera.Lib.Infrastructure
Domain models, business rules, aggregates, value objects, and domain services for Nera applications |
|
Nera.Lib.Caching
Caching utilities and services for the NERA system, with built-in support for Redis and seamless integration with .NET 9.0. |
GitHub repositories
This package is not used by any popular GitHub repositories.