Sculptor.Core 1.0.16

dotnet add package Sculptor.Core --version 1.0.16
                    
NuGet\Install-Package Sculptor.Core -Version 1.0.16
                    
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="Sculptor.Core" Version="1.0.16" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sculptor.Core" Version="1.0.16" />
                    
Directory.Packages.props
<PackageReference Include="Sculptor.Core" />
                    
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 Sculptor.Core --version 1.0.16
                    
#r "nuget: Sculptor.Core, 1.0.16"
                    
#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.
#addin nuget:?package=Sculptor.Core&version=1.0.16
                    
Install Sculptor.Core as a Cake Addin
#tool nuget:?package=Sculptor.Core&version=1.0.16
                    
Install Sculptor.Core as a Cake Tool

Sculptor

<p align="left"> <a href="https://github.com/cleberMargarida/sculptor/actions/workflows/workflow.yml"> <img src="https://github.com/cleberMargarida/sculptor/actions/workflows/workflow.yml/badge.svg" alt="Build-deploy pipeline"> </a> <a href="https://www.nuget.org/packages/Sculptor.Core"> <img src="https://img.shields.io/nuget/vpre/Sculptor.Core.svg" alt="EasyChain Nuget Version"> </a>
</p>

Overview

A lightweight library designed to facilitate Domain-Driven Design (DDD). Includes classes like AggregateRoot, RichModel, ValueObject, Entity, and Result to help structure domain models. For developers seeking to embrace DDD principles with ease.

It supports multiple .NET versions including .NET Core 2.1, .NET Core 3.1, .NET 5, .NET 6, .NET 7, .NET 8, and .NET 9.

Features

  • Easy-to-use
  • Source generators (No reflection)
  • Support for ServiceProvider anywhere

Getting Started

Installation

To install Sculptor, run the following command in your terminal:

dotnet add package Sculptor.AspNet

Usage

Here is a simple example of how to use Sculptor in your project:

In program file, add the following code:

app.UseSculptor();

The provided code includes a custom middleware that sets the static ServiceProvider property of the ServiceProviderAccessor class to the RequestServices of the current HttpContext. This enables access to the dependency injection container for the current HTTP request within domain classes such as AggregateRoot, Entity, and ValueObject through the Services property.

By this, you can access domain services from anywhere within your domain model, enabling the creation of rich models while ensuring that business rules are maintained within the core business.

Sample

BankAccount Class

This class serves as the Aggregate Root, encapsulating the main business rules and operations for a bank account.

using Sculptor.Core;

public class BankAccount : AggregateRoot
{
    public AccountOwner Owner { get; private set; }
    public Money Balance { get; private set; }

    // empty constructor for serialization and EF core
    private BankAccount() { }

    // Private constructor ensures controlled object creation through the Create method.
    private BankAccount(AccountOwner owner, Money initialBalance)
    {
        Owner = owner;
        Balance = initialBalance;
    }

    // Result Pattern Over Exceptions
    public static Result<BankAccount> Create(AccountOwner owner, Money initialBalance, IServiceProvider serviceProvider)
    {
        // Access the DbContext from the ServiceProvider
        var isExistingAccount = serviceProvider.GetRequiredService<BankAccountDbContext>()
            .BankAccounts.Any(account => account.Owner.Name == owner.Name);

        if (isExistingAccount)
            return Result<BankAccount>.Fail("An existing bank account already exists for this owner.");

        if (owner == null)
            return Result<BankAccount>.Fail("Account owner is required.");

        if (initialBalance == null || initialBalance.Amount < 0)
            return Result<BankAccount>.Fail("Initial balance must be a non-negative value.");

        return Result<BankAccount>.Ok(new BankAccount(owner, initialBalance));
    }

    // Business operation: Deposit money into the account.
    public Result Deposit(Money amount)
    {
        if (amount == null || amount.Amount <= 0)
            return Result.Fail("Deposit amount must be greater than zero.");

        Balance.Add(amount);

        return Result.Ok();
    }

    // Business operation: Withdraw money from the account.
    public Result Withdraw(Money amount)
    {
        if (amount == null || amount.Amount <= 0)
            return Result.Fail("Withdrawal amount must be greater than zero.");

        Balance = Balance.Subtract(amount);

        return Result.Ok();
    }
}
AccountOwner Class

This class is an Entity that represents the account owner, identified by unique attributes such as name and email.

  • Entity Base Class: The AccountOwner inherits from the Entity class, which gives it an identity and provides basic operations like equality comparison and hash code generation.
using Sculptor.Core;

public class AccountOwner : Entity
{
    public string Name { get; private set; }
    public string Email { get; private set; }

    // empty constructor for serialization and EF core
    private BankAccount() { }

    // Private constructor ensures controlled object creation through the Create method.
    private AccountOwner(string name, string email)
    {
        Name = name; // Set the owner's name.
        Email = email; // Set the owner's email.
    }

    public static Result<AccountOwner> Create(string name, string email)
    {
        if (string.IsNullOrWhiteSpace(name))
            return Result<AccountOwner>.Fail("Name is required.");

        if (string.IsNullOrWhiteSpace(email) || !email.Contains("@"))
            return Result<AccountOwner>.Fail("A valid email is required.");

        return Result<AccountOwner>.Ok(new AccountOwner(name, email));
    }
}
Email Class

This class is a Value Object that encapsulates an email address. It demonstrates immutability and validation logic.

  • Partial Class: The Email class is defined as partial to support source generators for automatically generating equality and hash code methods. These features help maintain consistency and ensure that value objects behave correctly in collections or when compared.
using Sculptor.Core;

public partial class Email : ValueObject
{
    public string Value { get; }

    private Email() { }

    public Email(string value)
    {
        Value = value;
    }
}

SourceGeneration

ValueObject must be a partial class to allow source generators to add equality and hash code calculation automatically.

Setting the Service Provider Manually

To set the service provider without relying on built-in mechanisms, like in BackgroundService's, messaging systems, you can manually assign the IServiceProvider to the static ServiceProvider property of ServiceProviderAccessor like this:

ServiceProviderAccessor.ServiceProvider = context.RequestServices;

License

Sculptor is licensed under the MIT License. See the LICENSE file for more details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Sculptor.Core:

Package Downloads
Sculptor.AspNet

Design classes to craft apps with end-to-end DDD.

Sculptor.Orleans

Design classes to craft apps with end-to-end DDD.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.16 111 2/11/2025
1.0.14 106 2/10/2025
1.0.13 106 2/10/2025
1.0.12 105 2/9/2025
1.0.10 99 2/9/2025
1.0.9 102 2/9/2025
1.0.8 92 2/9/2025
1.0.7 87 2/8/2025
1.0.6 121 2/8/2025
1.0.5 104 2/8/2025
1.0.4 115 2/7/2025
1.0.3 118 2/6/2025
1.0.1 133 2/6/2025
0.0.0-gf03c34df73 114 1/4/2025
0.0.0-gcdd8340ce6 70 1/24/2025
0.0.0-g848b39df55 67 1/24/2025
0.0.0-g46dcd4fca6 86 2/6/2025
0.0.0-g3c9ccd9ac4 77 1/21/2025
0.0.0-g393e383aec 73 1/21/2025
0.0.0-g25c8efe668 106 1/4/2025
0.0.0-g22a442af02 81 1/30/2025
0.0.0-g0ee4796a47 76 1/21/2025