Tizzani.ValueObject.EntityFrameworkCore 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tizzani.ValueObject.EntityFrameworkCore --version 0.2.0                
NuGet\Install-Package Tizzani.ValueObject.EntityFrameworkCore -Version 0.2.0                
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="Tizzani.ValueObject.EntityFrameworkCore" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tizzani.ValueObject.EntityFrameworkCore --version 0.2.0                
#r "nuget: Tizzani.ValueObject.EntityFrameworkCore, 0.2.0"                
#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.
// Install Tizzani.ValueObject.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=Tizzani.ValueObject.EntityFrameworkCore&version=0.2.0

// Install Tizzani.ValueObject.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=Tizzani.ValueObject.EntityFrameworkCore&version=0.2.0                

Tizzani.ValueObject

A simple value object framework easily configurable for use with Entity Framework Core.

GitHub Workflow Status Nuget

Sample Usage

BlogTitle.cs
public sealed record BlogTitle : ValueObject<string>
{
    public BlogTitle(string value) : base(value) { }
    
    public override void Validate(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("Blog title is required.");
          
        if (value.Length > 50)
            throw new ArgumentException("Blog title cannot exceed 50 characters.");
    }
}
Blog.cs
public sealed class Blog
{
    public int Id { get; set; }
    public BlogTitle Title { get; set; } = null!;
}
BlogContext.cs
public sealed class BlogContext : DbContext
{
    public DbSet<Blog>() Blogs => Set<Blog>();
    
    protected override void OnModelCreating(ModelBuilder builder)
    {
        modelBuilder.MapValueObjects();
        
        // for additional configuration (e.g., max length):
        modelBuilder.Entity<Blog>().HasValueObject(x => x.Title).HasMaxLength(50);
    }
}

Json Serialization

In some cases it might be useful to serialize/deserialize to/from the underlying value type. For these cases, you can include ValueObjectConverter in your json serialization options:

Without custom converter:
var blog = new Blog(1, new BlogTitle("My Sick Blog"));

// output: {"Id":1,"Title":{"Value":"My Sick Blog"}}
Console.WriteLine(JsonSerializer.Serialze(blog));
With custom converter:
var blog = new Blog(1, new BlogTitle("My Sick Blog"));

var options = new JsonSerializerOptions();
options.Converters.Add(new ValueObjectConverter());

// output: {"Id":1,"Title":"My Sick Blog"}
Console.WriteLine(JsonSerializer.Serialize(blog, options));
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.3.0 338 11/10/2022
0.2.0 392 10/23/2022