Path: redb-route/redb.Route/Xslt/IXsltEngineFactory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Xslt/IXsltEngineFactory.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using redb.Route.Abstractions;
namespace redb.Route.Xslt;
///
/// Compiles stylesheets into instances — the point at which a different
/// XSLT processor is substituted for the built-in one.
///
///
///
/// has always been an interface, and its documentation has always said a
/// Saxon-backed engine could be added later in an adapter package. It could not: every construction
/// site named directly, so there was nothing to substitute.
/// An interface with no seam behind it is documentation that lies.
///
///
/// Both methods are here rather than in because the engine is a compiled
/// stylesheet, and compiling is exactly what a replacement processor does differently. A route that
/// allows a per-message stylesheet needs to compile again at runtime, which is why the processor
/// holds the factory and not just the engine.
///
///
/// The default stays the BCL, XSLT 1.0, no dependencies — the same behaviour a route gets today.
/// XSLT 2.0/3.0 needs a processor we cannot ship: every .NET
/// implementation is commercial. What we can give is the contract, so someone who has bought one
/// registers it in their own project, under their own licence.
///
///
public interface IXsltEngineFactory
{
///
/// Compiles a stylesheet from a file path or URL, which also becomes the base URI for
/// xsl:import and xsl:include.
///
IXsltEngine FromFile(string stylesheetPath);
///
/// Compiles a self-contained stylesheet document. There is no base URI, so
/// xsl:import/xsl:include have nothing to resolve against.
///
IXsltEngine FromContent(string stylesheetXml);
}
///
/// The built-in factory: BCL , XSLT 1.0, no
/// external dependencies. Used whenever a route has registered nothing else.
///
public sealed class XslCompiledTransformEngineFactory : IXsltEngineFactory
{
/// The shared instance. The factory holds no state; the compiled stylesheets do.
public static readonly XslCompiledTransformEngineFactory Instance = new();
///
public IXsltEngine FromFile(string stylesheetPath) => XslCompiledTransformEngine.FromFile(stylesheetPath);
///
public IXsltEngine FromContent(string stylesheetXml) => XslCompiledTransformEngine.FromContent(stylesheetXml);
}
///
/// Registration and resolution of the XSLT engine factory, following the same order as every other
/// pluggable service here: what the context was told, then the DI container, then the default.
///
public static class XsltEngineRegistration
{
///
/// Registers an XSLT engine factory on a context built without DI:
/// context.UseXsltEngine(new SaxonXsltEngineFactory()).
///
public static IRouteContext UseXsltEngine(this IRouteContext context, IXsltEngineFactory factory)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(factory);
context.AddService(typeof(IXsltEngineFactory), factory);
return context;
}
///
/// DI registration: services.AddXsltEngine(new SaxonXsltEngineFactory()).
///
public static IServiceCollection AddXsltEngine(this IServiceCollection services, IXsltEngineFactory factory)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(factory);
services.TryAddSingleton(factory);
return services;
}
///
/// Resolves the factory a route should compile its stylesheets with.
///
///
/// A null context is not an error: definitions are built in tests and tools without one, and
/// the answer there is the built-in engine, which is also the answer when nothing is registered.
///
public static IXsltEngineFactory GetXsltEngineFactory(this IRouteContext? context)
=> context?.GetService()
?? context?.GetServiceProvider()?.GetService(typeof(IXsltEngineFactory)) as IXsltEngineFactory
?? XslCompiledTransformEngineFactory.Instance;
}