lsbV.ValueObject
1.0.3
dotnet add package lsbV.ValueObject --version 1.0.3
NuGet\Install-Package lsbV.ValueObject -Version 1.0.3
<PackageReference Include="lsbV.ValueObject" Version="1.0.3" />
<PackageVersion Include="lsbV.ValueObject" Version="1.0.3" />
<PackageReference Include="lsbV.ValueObject" />
paket add lsbV.ValueObject --version 1.0.3
#r "nuget: lsbV.ValueObject, 1.0.3"
#:package lsbV.ValueObject@1.0.3
#addin nuget:?package=lsbV.ValueObject&version=1.0.3
#tool nuget:?package=lsbV.ValueObject&version=1.0.3
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
Aspattern - 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
- Getting Started Guide - Comprehensive introduction and common patterns
- TryParse Emitter - Parser generation for safe type conversion
- Operators Emitter - Operator overloading and conversions
- Type Extensions - The
Aspattern for type conversions - Entity Framework Core - EF Core integration and configuration
- MongoDB Integration - MongoDB serialization support
- Configuration - Customizing generator behavior
For Contributors
- Architecture Overview - How the source generator works
- Emitter Development - Creating new emitters
- Testing Guide - Contributing tests
π― 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:
- Discovers all
recorddeclarations implementingIValueObject<T> - Analyzes the underlying value type
T - Generates optimized methods tailored to each type
- 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 | Versions 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. |
-
.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.