lsbV.ValueObject 1.0.3

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

ValueObject

A powerful C# source generator that automates the creation of utility methods for value objects. Transform simple value object declarations into fully-featured types with operators, type conversions, parsing capabilities, and database integration support.

✨ Features

  • TryParse Methods: Automatically generate safe parsing methods for minimal APIs and CLI applications
  • Operator Overloading: Implicit/explicit conversions and comparison operators
  • Type Casting Extensions: Convenient type conversion extensions with the As pattern
  • Entity Framework Core Integration: Automatic value converters and model builder extensions
  • MongoDB Support: Built-in serializers and class maps for MongoDB BSON serialization
  • Zero Configuration: Smart defaults with optional per-assembly customization

πŸš€ Quick Start

1. Install the Package

dotnet add package ValueObject

2. Define Your Value Object

using ValueObject.Core;

public partial record UserId(Guid Value) : IValueObject<Guid>;

public partial record Email(string Value) : IValueObject<string>;

public partial record Age(int Value) : IValueObject<int>;

3. Use the Generated Methods

// TryParse - safe parsing
if (Age.TryParse("25", out var age))
{
    Console.WriteLine($"Age: {age.Value}"); // Output: Age: 25
}

// Operators - natural comparisons
var userId1 = new UserId(Guid.Parse("..."));
var userId2 = userId1;
if (userId1 == userId2) { /* ... */ }

// Implicit conversion to underlying type
Guid id = userId1; // implicitly converts UserId to Guid

// Type extensions - convenient casting
var users = userIds.Select(id => id.As().Guid); // using StringAs extensions

4. Use in ASP.NET Core Minimal APIs

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Value objects are automatically parsed from route parameters
app.MapGet("/users/{userId}/details", (UserId userId) => 
{
    return Results.Ok($"Getting user: {userId.Value}");
});

app.Run();

πŸ“š Documentation

For Contributors

🎯 Why Value Objects?

Value objects provide:

  • Type Safety: Compile-time guarantees that values are correct types
  • Domain Clarity: Self-documenting code that expresses intent
  • Reusability: Share validation logic across your application
  • Maintainability: Changes centralized in value object definitions

πŸ”„ How It Works

ValueObject is a .NET incremental source generator that:

  1. Discovers all record declarations implementing IValueObject<T>
  2. Analyzes the underlying value type T
  3. Generates optimized methods tailored to each type
  4. Emits zero-cost abstractions at compile time

No runtime overhead, no reflectionβ€”just generated code.

πŸ’‘ Example: E-Commerce Domain

// Define your domain value objects
public partial record ProductId(Guid Value) : IValueObject<Guid>;
public partial record Price(decimal Value) : IValueObject<decimal>;
public partial record Quantity(int Value) : IValueObject<int>;
public partial record Email(string Value) : IValueObject<string>;

// Automatically get:
// βœ“ TryParse methods for parsing from strings
// βœ“ Operators for natural syntax (price > 100)
// βœ“ EF Core value converters for database mapping
// βœ“ MongoDB serializers for document storage
// βœ“ Type extensions for safe casting

// In your API
app.MapPost("/orders/{productId}/{quantity}", 
    (ProductId productId, Quantity quantity, Price pricePerUnit) =>
{
    if (pricePerUnit > 1000) // operator comparison
        return Results.BadRequest();
    
    var total = quantity.Value * pricePerUnit.Value; // easy arithmetic
    return Results.Ok(new { productId, total });
});

πŸ› οΈ Configuration

Control generation with assembly-level settings:

// Program.cs or AssemblyInfo.cs
[assembly: ValueObjectSettings(
    generateMongoDbSerializer: true,
    generateEfCoreValueConverter: true
)]

πŸ“– Supported Types

ValueObject supports all common .NET types:

  • Primitives: int, long, float, double, decimal, bool, byte
  • Strings: string
  • Types: Guid, DateTime, DateOnly, TimeOnly
  • MongoDB: MongoDB.Bson.ObjectId
  • Custom: Any type with TryParse(string, IFormatProvider, out T)

🀝 Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

πŸ“„ License

See LICENSE.txt for details.


Made with ❀️ for developers who care about type safety and clean code

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.  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. 
.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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 124 2/9/2026
1.0.2 103 2/9/2026
1.0.1 105 2/9/2026
1.0.0 122 2/9/2026