Executable resources: C# + F#

The Razor page only knows logical file paths. The executable examples live in separate C# and F# assemblies but use the same IExample contract.

Shared Fusion connection setup

The examples use this helper to open the tutorial database and configure Fusion for SQLite.

ConnectionFactory.cs
var connection = new SqliteConnection(context.Session.ConnectionString);

try
{
    await connection.OpenAsync(context.CancellationToken);
    return FusionConnection.Create(connection).UseSqlite();
}
catch
{
    await connection.DisposeAsync();
    throw;
}

Plain resource

Examples/CSharp/Models.cs
public sealed record Person(
    [property: DbKey]
    long ID,
    string LastName,
    string FirstMidName,
    DateTime EnrollmentDate,
    string Discriminator);

public sealed record Course(
    [property: DbKey]
    long CourseID,
    string Title,
    int Credits,
    long DepartmentID);

public sealed record Department(
    [property: DbKey]
    long DepartmentID,
    string Name,
    decimal Budget);

public sealed record OfficeAssignment(
    [property: DbKey]
    long InstructorID,
    string Location);

public sealed record CourseAssignment(
    [property: DbKey]
    long InstructorID,
    [property: DbKey]
    long CourseID);

public sealed record DepartmentDto(
    long Id,
    string Name,
    decimal Budget,
    List<CourseDto> Courses);

public sealed record CourseDto(
    long Id,
    string Title,
    int Credits);
Database/Seed.sql
PRAGMA foreign_keys = ON;

CREATE TABLE Person (
    ID INTEGER PRIMARY KEY,
    LastName TEXT NOT NULL,
    FirstMidName TEXT NOT NULL,
    EnrollmentDate TEXT NOT NULL,
    Discriminator TEXT NOT NULL
);

CREATE TABLE Department (
    DepartmentID INTEGER PRIMARY KEY,
    Name TEXT NOT NULL,
    Budget REAL NOT NULL
);

CREATE TABLE Course (
    CourseID INTEGER PRIMARY KEY,
    Title TEXT NOT NULL,
    Credits INTEGER NOT NULL,
    DepartmentID INTEGER NOT NULL,

    FOREIGN KEY (DepartmentID)
        REFERENCES Department(DepartmentID)
);

CREATE TABLE OfficeAssignment (
    InstructorID INTEGER PRIMARY KEY,
    Location TEXT NOT NULL,

    FOREIGN KEY (InstructorID)
        REFERENCES Person(ID)
);

CREATE TABLE CourseAssignment (
    InstructorID INTEGER NOT NULL,
    CourseID INTEGER NOT NULL,

    PRIMARY KEY (InstructorID, CourseID),

    FOREIGN KEY (InstructorID)
        REFERENCES Person(ID),

    FOREIGN KEY (CourseID)
        REFERENCES Course(CourseID)
);

INSERT INTO Person VALUES
(1, 'Abercrombie', 'Kim',     '2005-09-01', 'Instructor'),
(2, 'Fakhouri',    'Fadi',    '2002-08-06', 'Instructor'),
(3, 'Harui',       'Roger',   '2008-07-01', 'Student'),
(4, 'Kapoor',      'Candace', '2011-09-01', 'Student'),
(5, 'Zheng',       'Roger',   '2009-09-01', 'Student');

INSERT INTO Department VALUES
(1, 'English',     350000),
(2, 'Mathematics', 100000),
(3, 'Engineering', 350000),
(4, 'Economics',   100000);

INSERT INTO Course VALUES
(2021, 'Composition',    3, 1),
(2042, 'Literature',     4, 1),

(1045, 'Calculus',       4, 2),
(3141, 'Trigonometry',   4, 2),

(1050, 'Chemistry',      3, 3),

(4022, 'Microeconomics', 3, 4),
(4041, 'Macroeconomics', 3, 4);

INSERT INTO OfficeAssignment VALUES
(1, 'Smith 17'),
(2, 'Gowan 27');

INSERT INTO CourseAssignment VALUES
(1, 1050),
(1, 4022),
(2, 1045),
(2, 4041);

C# examples

Examples/CSharp/ListCourses.cs
await using var fusionConnection = await context.OpenFusionConnectionAsync();

var courses = await fusionConnection.GetRowsAsync<Course>(
    """
    SELECT CourseID, Title, Credits, DepartmentID
    FROM Course
    ORDER BY CourseID;
    """
);

return courses;
Examples/CSharp/Hello.cs
using Demo.Infrastructure;
using Spectre.Console;

namespace Demo.Examples.CSharp;

[Example]
public sealed class Hello : IExample
{
    public async Task<object?> RunAsync(IExampleContext context)
    {
        context.Console.MarkupLine("[bold green]Hello from C#![/]");
        context.Console.MarkupLine("This source file is both [yellow]compiled[/] and [yellow]embedded[/].");

        await Task.Delay(150, context.CancellationToken);

        var values = new[] { 10, 20, 30 };
        context.Console.MarkupLine($"Sum = [cyan]{values.Sum()}[/]");

        return null;
    }
}
Examples/CSharp/Merge.cs
using Demo.Infrastructure;
using Spectre.Console;

namespace Demo.Examples.CSharp;

[Example]
public sealed class Merge : IExample
{
    public Task<object?> RunAsync(IExampleContext context)
    {
        var current = new[] { 1, 2, 3 };
        var incoming = new[] { 2, 3, 4 };
        var merged = current.Union(incoming).OrderBy(x => x).ToArray();

        context.Console.MarkupLine("[bold]Merge demo[/]");
        context.Console.WriteLine($"Current : {string.Join(", ", current)}");
        context.Console.WriteLine($"Incoming: {string.Join(", ", incoming)}");
        context.Console.MarkupLine($"Result  : [green]{string.Join(", ", merged)}[/]");

        return Task.FromResult<object?>(null);
    }
}

F# examples

Examples/FSharp/Model.fs
type Course =
    {
        [<property: Infra.Fusion.DbKey>]
        CourseID: int64
        Title: string
        Credits: int
        DepartmentID: int64
    }

type Department =
    {
        [<property: Infra.Fusion.DbKey>]
        DepartmentID: int64
        Name: string
        Budget: decimal
    }

type CourseDto =
    {
        Id: int64
        Title: string
        Credits: int
    }

type DepartmentDto =
    {
        Id: int64
        Name: string
        Budget: decimal
        Courses: CourseDto list
    }
Examples/FSharp/ListCourses.fs
use! fusionConnection = context.OpenFusionConnectionAsync()

let! courses =
    fusionConnection.GetRowsAsync<Course>(
        """
        SELECT CourseID, Title, Credits, DepartmentID
        FROM Course
        ORDER BY CourseID;
        """)

return box courses
Examples/FSharp/Hello.fs
namespace Demo.Examples.FSharp

open System.Threading.Tasks
open Demo.Infrastructure
open Spectre.Console

[<Example>]
type Hello() =
    interface IExample with
        member _.RunAsync(context: IExampleContext) : Task<obj> =
            (task {
                context.Console.MarkupLine("[bold deepskyblue1]Hello from F#![/]")
                context.Console.MarkupLine("The same [yellow]IExample[/] contract is used from F#.")

                do! Task.Delay(150, context.CancellationToken)

                let values = [ 1 .. 5 ]
                let total = List.sum values
                context.Console.MarkupLine($"Sum = [green]{total}[/]")

                return box values
            } )
Examples/FSharp/Pipeline.fs
namespace Demo.Examples.FSharp

open System.Threading.Tasks
open Demo.Infrastructure
open Spectre.Console

[<Example>]
type Pipeline() =
    interface IExample with
        member _.RunAsync(context: IExampleContext) : Task<obj> =
            (task {
                let result =
                    [ 1 .. 10 ]
                    |> List.filter (fun x -> x % 2 = 0)
                    |> List.map (fun x -> x * x)
                    |> List.sum

                context.Console.MarkupLine("[bold]F# pipeline[/]")
                context.Console.MarkupLine($"Result = [green]{result}[/]")

                return box result
            } )