FractalDataWorks.Commands.Abstractions 0.9.1-alpha.1037

This is a prerelease version of FractalDataWorks.Commands.Abstractions.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package FractalDataWorks.Commands.Abstractions --version 0.9.1-alpha.1037
                    
NuGet\Install-Package FractalDataWorks.Commands.Abstractions -Version 0.9.1-alpha.1037
                    
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="FractalDataWorks.Commands.Abstractions" Version="0.9.1-alpha.1037" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FractalDataWorks.Commands.Abstractions" Version="0.9.1-alpha.1037" />
                    
Directory.Packages.props
<PackageReference Include="FractalDataWorks.Commands.Abstractions" />
                    
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 FractalDataWorks.Commands.Abstractions --version 0.9.1-alpha.1037
                    
#r "nuget: FractalDataWorks.Commands.Abstractions, 0.9.1-alpha.1037"
                    
#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 FractalDataWorks.Commands.Abstractions@0.9.1-alpha.1037
                    
#: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=FractalDataWorks.Commands.Abstractions&version=0.9.1-alpha.1037&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=FractalDataWorks.Commands.Abstractions&version=0.9.1-alpha.1037&prerelease
                    
Install as a Cake Tool

FractalDataWorks.Commands.Abstractions

Comprehensive command infrastructure providing type-safe command definitions, translation capabilities, and execution metadata for universal data operations across different backends.

Overview

FractalDataWorks.Commands.Abstractions defines the command architecture that enables the framework's Universal Data Access pattern. It provides interfaces and base classes for:

  • Command Type Definitions - Static metadata about command capabilities
  • Command Translators - Converting commands between formats (LINQ→SQL, Expression→HTTP)
  • Command Categories - Classification system (Query, Mutation, Bulk operations)
  • Translation Context - Metadata for command translation
  • Command Messages - Structured feedback for command operations

Target Frameworks: .NET Standard 2.0, .NET 10.0 Dependencies: FractalDataWorks.Abstractions, FractalDataWorks.Collections, FluentValidation

Key Concepts

Command vs CommandType

IGenericCommand (runtime instance):

  • Represents a specific command execution
  • Contains CommandId, CreatedAt, CommandType properties
  • Defined in FractalDataWorks.Abstractions
  • Used for actual command execution

IGenericCommandType (static metadata):

  • Represents command type definition (singleton)
  • Contains metadata: Category, SupportedTranslators, batching capabilities
  • Registered via TypeCollections for discovery
  • Used for routing and capability checking

Core Interfaces

IGenericCommand (Runtime Instance)

public interface IGenericCommand
{
    string CommandType { get; }
    IGenericCommandCategory Category { get; }
    IGenericResult<ValidationResult> Validate();
}

public interface IGenericCommand<T> : IGenericCommand
{
    T? Payload { get; init; }
    new IGenericResult<ValidationResult> Validate();
}

Usage: Runtime command instances with payloads:

public class SqlQueryCommand : IGenericCommand<QueryPayload>
{
    public string CommandType => "SqlQuery";
    public IGenericCommandCategory Category => CommandCategories.Query;
    public QueryPayload? Payload { get; init; }

    public IGenericResult<ValidationResult> Validate()
    {
        // FluentValidation logic
    }
}

IGenericCommandType (Type Metadata)

public interface IGenericCommandType : ITypeOption<int, CommandTypeBase>
{
    IGenericCommandCategory CommandCategory { get; }
    IReadOnlyCollection<ITranslatorType> SupportedTranslators { get; }
    bool SupportsBatching { get; }
    bool SupportsPipelining { get; }
    int MaxBatchSize { get; }
}

Usage: Static type definitions in TypeCollections:

[TypeOption(typeof(CommandTypes), "SqlQuery")]
public sealed class SqlQueryCommandType() : CommandTypeBase(1, "SqlQuery")
{
    public override IGenericCommandCategory CommandCategory => CommandCategories.Query;

    public override IReadOnlyCollection<ITranslatorType> SupportedTranslators =>
        new[] { TranslatorTypes.SqlTranslator, TranslatorTypes.LinqTranslator };

    public override bool SupportsBatching => true;
    public override int MaxBatchSize => 100;
}

IGenericCommandTranslator

public interface IGenericCommandTranslator
{
    ITranslatorType TranslatorType { get; }

    IGenericResult<bool> CanTranslate(Expression expression);

    IGenericResult<IGenericCommand> Translate(
        Expression expression,
        ITranslationContext? context = null);

    IGenericResult<IGenericCommand> TranslateCommand(
        IGenericCommand command,
        IDataFormat targetFormat);

    Task<IGenericResult<IGenericCommand>> Optimize(
        IGenericCommand command,
        CancellationToken cancellationToken = default);

    IGenericResult<CommandCostEstimate> EstimateCost(IGenericCommand command);
}

Responsibilities:

  • Translation: Convert LINQ expressions to backend commands
  • Cross-Format: Translate between command formats (SQL↔HTTP↔GraphQL)
  • Optimization: Improve translated commands for performance
  • Cost Estimation: Predict execution cost before running

Example: SQL Translator

public class SqlCommandTranslator : IGenericCommandTranslator
{
    public ITranslatorType TranslatorType => TranslatorTypes.SqlTranslator;

    public IGenericResult<bool> CanTranslate(Expression expression)
    {
        // Check if expression can be translated to SQL
        return GenericResult<bool>.Success(IsSqlCompatible(expression));
    }

    public IGenericResult<IGenericCommand> Translate(
        Expression expression,
        ITranslationContext? context = null)
    {
        try
        {
            var sql = ConvertExpressionToSql(expression, context);
            return GenericResult<IGenericCommand>.Success(
                new SqlQueryCommand { Payload = sql });
        }
        catch (Exception ex)
        {
            return GenericResult<IGenericCommand>.Failure(
                $"Translation failed: {ex.Message}");
        }
    }

    public IGenericResult<CommandCostEstimate> EstimateCost(IGenericCommand command)
    {
        var estimate = new CommandCostEstimate
        {
            EstimatedRows = AnalyzeQuery(command),
            EstimatedCost = CalculateCost(command)
        };
        return GenericResult<CommandCostEstimate>.Success(estimate);
    }
}

TypeCollections

CommandTypes Collection

[TypeCollection(typeof(CommandTypeBase), typeof(IGenericCommandType), typeof(CommandTypes))]
public abstract partial class CommandTypes : TypeCollectionBase<CommandTypeBase, IGenericCommandType>
{
    // Source generator populates with discovered command types
}

// Usage
var queryType = CommandTypes.ById(1);  // Get SqlQueryCommandType
var allTypes = CommandTypes.All();      // All registered command types

CommandCategories Collection

[TypeCollection(typeof(CommandCategoryBase), typeof(IGenericCommandCategory), typeof(CommandCategories))]
public abstract partial class CommandCategories :
    TypeCollectionBase<CommandCategoryBase, IGenericCommandCategory>
{
}

// Built-in categories
public static IGenericCommandCategory Query => CommandCategories.Query;
public static IGenericCommandCategory Mutation => CommandCategories.Mutation;
public static IGenericCommandCategory Bulk => CommandCategories.Bulk;

Categories Define:

  • Query: Read-only operations (SELECT, GET)
  • Mutation: State-changing operations (INSERT, UPDATE, DELETE, POST, PUT)
  • Bulk: Batch operations with multiple commands

TranslatorTypes Collection

[TypeCollection(typeof(TranslatorTypeBase), typeof(ITranslatorType), typeof(TranslatorTypes))]
public abstract partial class TranslatorTypes :
    TypeCollectionBase<TranslatorTypeBase, ITranslatorType>
{
}

// Usage
var sqlTranslator = TranslatorTypes.SqlTranslator;
var linqTranslator = TranslatorTypes.LinqTranslator;

Translation Context

ITranslationContext

public interface ITranslationContext
{
    IDataSchema? SourceSchema { get; }
    IDataSchema? TargetSchema { get; }
    IDataFormat SourceFormat { get; }
    IDataFormat TargetFormat { get; }
    IDictionary<string, object> Metadata { get; }
}

Usage: Provides metadata during translation

var context = new TranslationContext
{
    SourceSchema = databaseSchema,
    TargetSchema = apiSchema,
    SourceFormat = DataFormats.Linq,
    TargetFormat = DataFormats.Sql,
    Metadata = new Dictionary<string, object>
    {
        ["Timeout"] = 30,
        ["MaxRows"] = 1000
    }
};

var result = translator.Translate(expression, context);

Command Execution Flow

Translation Capabilities

public sealed class TranslationCapabilities
{
    public bool SupportsJoins { get; init; }
    public bool SupportsAggregates { get; init; }
    public bool SupportsSubqueries { get; init; }
    public bool SupportsPaging { get; init; }
    public bool SupportsFiltering { get; init; }
    public bool SupportsSorting { get; init; }
    public int MaxBatchSize { get; init; }
    public IReadOnlyList<string> SupportedFunctions { get; init; }
}

Usage: Declare translator capabilities

public class SqlTranslatorType : TranslatorTypeBase
{
    public override TranslationCapabilities Capabilities => new()
    {
        SupportsJoins = true,
        SupportsAggregates = true,
        SupportsSubqueries = true,
        SupportsPaging = true,
        MaxBatchSize = 100,
        SupportedFunctions = new[] { "COUNT", "SUM", "AVG", "MAX", "MIN" }
    };
}

Command Cost Estimation

public sealed class CommandCostEstimate
{
    public long EstimatedRows { get; init; }
    public decimal EstimatedCost { get; init; }
    public TimeSpan EstimatedDuration { get; init; }
    public int ComplexityScore { get; init; }
    public IDictionary<string, object> AdditionalMetrics { get; init; }
}

Usage: Estimate before execution

var estimate = translator.EstimateCost(command);
if (estimate.Value.EstimatedRows > 1_000_000)
{
    // Warn user or optimize query
}

Command Messages

CommandMessage

public sealed class CommandMessage : IDataMessage
{
    public string Message { get; init; }
    public string? Code { get; init; }
    public string? Source { get; init; }
    public MessageSeverity Severity { get; init; }
}

Specialized Messages

TranslationFailedMessage:

public sealed class TranslationFailedMessage : CommandMessage
{
    public Expression? FailedExpression { get; init; }
    public string TranslatorType { get; init; }
    public Exception? Exception { get; init; }
}

UnsupportedCommandMessage:

public sealed class UnsupportedCommandMessage : CommandMessage
{
    public string CommandType { get; init; }
    public string TranslatorType { get; init; }
    public IReadOnlyList<string> SupportedCommands { get; init; }
}

CommandNullMessage:

public sealed class CommandNullMessage : CommandMessage
{
    public CommandNullMessage()
    {
        Message = "Command cannot be null";
        Severity = MessageSeverity.Error;
        Code = "CMD_NULL";
    }
}

Command Logging

CommandLog

public sealed class CommandLog
{
    public Guid CommandId { get; init; }
    public string CommandType { get; init; }
    public DateTime StartTime { get; init; }
    public DateTime? EndTime { get; init; }
    public TimeSpan? Duration { get; init; }
    public bool IsSuccess { get; init; }
    public IReadOnlyList<IGenericMessage> Messages { get; init; }
    public IDictionary<string, object> Metadata { get; init; }
}

Usage: Track command execution

var log = new CommandLog
{
    CommandId = command.CommandId,
    CommandType = command.CommandType,
    StartTime = DateTime.UtcNow,
    Metadata = new Dictionary<string, object>
    {
        ["User"] = userId,
        ["Query"] = sqlCommand.Payload
    }
};

// After execution
log = log with
{
    EndTime = DateTime.UtcNow,
    Duration = executionTime,
    IsSuccess = result.IsSuccess,
    Messages = result.Messages.ToList()
};

Command Execution Patterns

Translation Pipeline

public async Task<IGenericResult<T>> ExecuteQueryAsync<T>(Expression expression)
{
    // 1. Find translator
    var translator = FindTranslator(expression);
    if (translator == null)
        return GenericResult<T>.Failure(new TranslatorNotFoundMessage());

    // 2. Validate translation capability
    var canTranslate = translator.CanTranslate(expression);
    if (!canTranslate.Value)
        return GenericResult<T>.Failure(new UnsupportedCommandMessage());

    // 3. Translate expression
    var commandResult = translator.Translate(expression, context);
    if (commandResult.IsFailure)
        return GenericResult<T>.Failure(new TranslationFailedMessage());

    // 4. Optimize command
    var optimized = await translator.Optimize(commandResult.Value);
    var command = optimized.IsSuccess ? optimized.Value : commandResult.Value;

    // 5. Execute command
    return await service.Execute<T>(command, cancellationToken);
}

Batching Support

public async Task<IGenericResult> ExecuteBatchAsync(IEnumerable<IGenericCommand> commands)
{
    var commandList = commands.ToList();

    // Check batching support
    var commandType = CommandTypes.Name(commandList[0].CommandType);
    if (!commandType.SupportsBatching)
        return GenericResult.Failure("Command type does not support batching");

    // Validate batch size
    if (commandList.Count > commandType.MaxBatchSize)
        return GenericResult.Failure($"Batch size exceeds maximum of {commandType.MaxBatchSize}");

    // Execute batch
    var batchCommand = CreateBatchCommand(commandList);
    return await service.Execute(batchCommand, cancellationToken);
}

Best Practices

  1. Use Railway-Oriented Programming: Always return IGenericResult from translators
  2. Validate Before Translation: Check CanTranslate() before calling Translate()
  3. Optimize Translated Commands: Use Optimize() for better performance
  4. Estimate Costs: Check EstimateCost() for expensive operations
  5. Batch When Possible: Use batching for multiple similar commands
  6. Provide Context: Include translation context for better results
  7. Log Commands: Use CommandLog for debugging and auditing
  8. Handle Failures Gracefully: Use structured messages for clear feedback

Dependencies

  • FractalDataWorks.Abstractions: Core interfaces (IGenericCommand, IGenericService)
  • FractalDataWorks.Collections: TypeCollections infrastructure
  • FractalDataWorks.Results: Railway-Oriented Programming support
  • FractalDataWorks.Data.Abstractions: Data format and schema interfaces
  • FluentValidation: Command validation support

Summary

FractalDataWorks.Commands.Abstractions provides a comprehensive command infrastructure that enables Universal Data Access through type-safe command definitions, pluggable translators, and execution metadata. The combination of TypeCollections-based discovery, Railway-Oriented error handling, and translation capabilities makes it possible to write backend-agnostic data operations that work across SQL, REST, GraphQL, and other data sources.

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.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on FractalDataWorks.Commands.Abstractions:

Package Downloads
FractalDataWorks.Commands.Data.Abstractions

Development tools and utilities for the FractalDataWorks ecosystem. Build:

FractalDataWorks.Services.Connections.Abstractions

Development tools and utilities for the FractalDataWorks ecosystem. Build:

FractalDataWorks.Services.Connections

Development tools and utilities for the FractalDataWorks ecosystem. Build:

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.9.5-alpha.1008 0 11/25/2025
0.9.5-alpha.1007 0 11/25/2025
0.9.5-alpha.1006 0 11/25/2025
0.9.1-alpha.1037 0 11/25/2025
0.9.1-alpha.1036 0 11/25/2025
0.9.1-alpha.1035 0 11/25/2025
0.9.1-alpha.1033 0 11/25/2025
0.9.0-alpha.1011.ged0a6c6e98 356 11/18/2025
0.9.0-alpha.1010.gecd88aac50 350 11/18/2025
0.9.0-alpha.1009.g7f6817e985 349 11/18/2025
0.9.0-alpha.1006.gf287016c0c 366 11/18/2025
0.8.0-alpha.1011 351 11/18/2025
0.7.0-alpha.1022 144 11/3/2025
0.7.0-alpha.1021 151 11/3/2025
0.7.0-alpha.1008 114 11/2/2025
0.7.0-alpha.1006 145 10/30/2025
0.7.0-alpha.1005 145 10/30/2025
0.7.0-alpha.1004 141 10/30/2025
0.7.0-alpha.1001 135 10/29/2025
0.6.0-alpha.1006 135 10/29/2025
0.6.0-alpha.1005 137 10/28/2025
0.6.0-alpha.1004 131 10/28/2025