Lyo.Formatter 1.0.2

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

Lyo.Formatter

SmartFormat.NET-backed templating for user-defined strings: named placeholders, lists, pluralization, and culture-aware formatting. Designed for validation + formatting pipelines (for example IFormatterService with Lyo.Api computed fields and Lyo.Web.Automation step templates).

Examples

Register services

using Lyo.Formatter;
using Microsoft.Extensions.DependencyInjection;

services.AddFormatterService();
// Or: services.AddFormatterService(sp => /* custom SmartFormatter */);

ITemplate workflow

var t = formatter.CreateTemplate("{Title} — {Count}")
    .WithValue("Title", doc.Title)
    .WithValue("Count", doc.Count);

if (!t.TryValidate(out var err))
    throw new InvalidOperationException(err);

if (!t.TryValidateContext(out var ctxErr))
    throw new InvalidOperationException(ctxErr);

var text = t.Format();

When to use this package

  • Turn stored templates ("{User.Name} — {Order.Total:C}") into final text with one or more context objects.
  • Validate templates before persisting them (ValidateTemplate, TryValidateTemplate).
  • Discover placeholders for dependency analysis (GetPlaceholders, GetUnresolvedPlaceholders, AllPlaceholdersResolved).
  • Build context fluently with IContextBuilder (dates, conditional keys, custom formatters).

Registration

Register FormatterService as singleton and expose IFormatterService. Prefer the overload with a factory when you need extra SmartFormat extensions or custom * *SmartSettings**.

Core types

Type Role
IFormatterService Format, validate, inspect placeholders, wrap templates as ITemplate, emit annotated FormatSegments.
FormatterService Default implementation: FormatErrorAction.MaintainTokens so missing data leaves {tokens} in output (enables unresolved-placeholder detection). Case-insensitive placeholder matching.
ITemplate Parse-once style workflow: WithContext, AddContext, TryValidateContext, then Format().
IContextBuilder Fluent dictionary builder passed to Format(template, configure).
FormatterSegment / FormatterSegmentKind Annotated span from FormatSegments: literal text, a resolved replacement, or an unresolved {token} plus the placeholder key and raw template substring.

Formatting overloads

  • Format(template, object? context) — single DTO, anonymous object, or any type SmartFormat can reflect over.
  • Format(template, params object?[] contextItems) — multiple sources; later objects win on duplicate names.
  • Format(template, IReadOnlyDictionary<string, object?>) — explicit name/value map.
  • Format(template, Action<IContextBuilder>) — build the map with Add, AddIf, AddWhen, typed format strings, or custom Func<,> formatters.

Validation and placeholders

  • ValidateTemplate / TryValidateTemplate — parser pass; catches syntax errors before you save a template.
  • TryFormat — swallows exceptions from SmartFormat and returns false (use sparingly; prefer validation + known context).
  • GetPlaceholders — lightweight regex-based names (first segment of each {...}); good for UI hints and entityTypes-style dependency lists.
  • AllPlaceholdersResolved / GetUnresolvedPlaceholders — compare template to formatted output; relies on MaintainTokens so missing keys stay visible as {Name}.
  • FormatSegments — walk the parsed template into ordered FormatterSegment spans (literal / placeholder / unresolved) so UIs can color-link {Name} to its replacement without a second parser.

ITemplate workflow

Use AddContext on the template to layer IContextBuilder steps without allocating a full dictionary at the call site. Format(additionalContext) merges a one-off context (dictionary or object) on top of the accumulated state for a single render.

ITemplate.TryValidateContext succeeds when the accumulated context keys cover every placeholder name (or supply a parent path like Order for {Order.Total}). Bare CLR objects passed via WithContext(object?) do not participate in this check (only the merged dictionary and dictionary-shaped extras do), so call WithValue/AddContext or supply a dictionary when you want the validator to confirm coverage.

SmartFormat behavior

This library does not fork SmartFormat; it configures a SmartFormatter instance. Refer to the SmartFormat documentation for list formatting, plural rules, and built-in extensions. Automation note: Lyo.Web.Automation documents that step templates use single-brace placeholders ({page.url}); legacy {{page.url}} is normalized there. This service accepts standard SmartFormat templates as-is.

Integration points

  • Lyo.Api — optional IFormatterService for ComputedFields on projection/query responses (SmartFormat templates over projected rows).
  • Lyo.Web.Automation — optional IFormatterService to validate automation plans before execution.
  • Lyo.Formatter.Web.Components — WASM-safe live template editor + annotated preview (FormatSegments).

Thread safety

FormatterService is safe for concurrent reads if you do not mutate SmartFormatter or Culture from multiple threads without synchronization. Typical ASP.NET Core registration as a singleton treats Culture as ambient per request by setting it at the start of a request (or avoid mutating Culture on the shared instance and pass culture-aware data in context instead).

Dependencies

Generated from ProjectReference / PackageReference (same model as docs/Lyo.ProjectGraph.html).

  • Lyo.Common — (direct, lyo)
  • Lyo.Exceptions — (direct, lyo)
  • Microsoft.Extensions.DependencyInjection.Abstractions 10.0.5 — (direct, microsoft)
  • SmartFormat.NET 3.6.1 — (direct, third-party)
  • Microsoft.Extensions.Logging.Abstractions 10.0.5 — (transitive, microsoft)
  • System.Memory 4.6.3 — (transitive, microsoft, netstandard2.0)
  • System.Text.Json 10.0.5 — (transitive, microsoft, netstandard2.0)
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 is compatible.  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.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Lyo.Formatter:

Package Downloads
Lyo.Api

Core API library for building RESTful APIs with Entity Framework Core, caching, and mapping support.

Lyo.Web.Automation

Shared browser automation models: element locators, JSON automation plans, session abstraction, and plan runners (engine-neutral).

Lyo.Job.Scheduler

Job scheduling and execution service dispatching jobs via RabbitMQ message queue.

Lyo.Formatter.Web.Components

WASM-safe Blazor components for live SmartFormat template editing and annotated preview using Lyo.Formatter.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 0 8/19/2026
1.0.1 79 8/18/2026
1.0.0 166 8/16/2026