Domain models, business rules, aggregates, value objects, and domain services for Nera applications
$ dotnet add package Nera.Lib.DomainA 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.
Install the package via NuGet Package Manager:
dotnet add package Nera.Lib.Domain
Or via Package Manager Console:
Install-Package Nera.Lib.Domain
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));
}
}
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;
}
}
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));
}
}
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;
}
}
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
}
IEntity<TKey>
├── BaseEntity<TKey>
│ ├── AuditableEntity<TKey>
│ └── Entity<TKey>
└── AggregateRoot<TKey>
// 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));
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure your entities
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
}
}
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions, please:
Nextera Systems - Initial work and maintenance
Part of the Nera Libraries ecosystem for building robust, scalable applications.