F#

Use Fusion idiomatically from F#

Fusion materializes F# records through their complete constructor and preserves explicit SQL, typed parameters, and projections across the language boundary.

Records and immutable collections

The model uses F# records, DbKey attributes, and an immutable CourseDto list for the aggregate DTO.

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
        InstructorID: int64
    }

type CourseAssignment =
    {
        [<property: Infra.Fusion.DbKey>]
        InstructorID: int64
        [<property: Infra.Fusion.DbKey>]
        CourseID: int64
    }

type Person =
    {
        [<property: Infra.Fusion.DbKey>]
        ID: int64
        LastName: string
        FirstMidName: string
        EnrollmentDate: System.DateTime
        Discriminator: string
    }

type OfficeAssignment =
    {
        [<property: Infra.Fusion.DbKey>]
        InstructorID: int64
        Location: string
    }

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

type DepartmentDto =
    {
        Id: int64
        Name: string
        Budget: decimal
        InstructorId: int64
        Courses: CourseDto list
    }

Typed rows

GetRowsAsync<Course> materializes each database row as an F# record.

ListCourses.fs
use! fusionConnection = context.OpenFusionConnectionAsync()

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

return box courses

Scalar value and named parameters

A typed Parameters value is passed to the same provider-independent query API.

ScalarAndParameters.fs
use! fusionConnection = context.OpenFusionConnectionAsync()

let parameters = Parameters()
parameters.Add("minimumCredits", 4)

let! courseCount =
    fusionConnection.GetValueAsync<int64>(
        """
        SELECT COUNT(*)
        FROM Course
        WHERE Credits >= @minimumCredits;
        """,
        parameters,
        context.CancellationToken)

return box courseCount

Custom projection

FusionDataRecord can project ordered columns directly into an anonymous F# record.

ProjectionCourses.fs
use! fusionConnection = context.OpenFusionConnectionAsync()

let parameters = Parameters()
parameters.Add("departmentId", 1L)

let! courses =
    fusionConnection.GetProjectionsAsync(
        """
        SELECT Title, Credits
        FROM Course
        WHERE DepartmentID = @departmentId
        ORDER BY Title;
        """,
        (fun row ->
            {|
                Title = row.ReadValue<string>()
                Credits = row.ReadValue<int64>()
            |}),
        parameters,
        context.CancellationToken)

return box courses

DataNode tree

A branching From/Join shape rebuilds instructor roots with assigned courses, optional offices, and managed departments without generating or parsing the query.

InstructorDataNodes.fs
use! fusionConnection = context.OpenFusionConnectionAsync()

let sql =
    """
        SELECT
            P.ID, P.LastName, P.FirstMidName, P.EnrollmentDate, P.Discriminator,
            CA.InstructorID, CA.CourseID,
            C.CourseID, C.Title, C.Credits, C.DepartmentID,
            D.DepartmentID, D.Name, D.Budget, D.InstructorID,
            O.InstructorID, O.Location,
            D1.DepartmentID, D1.Name, D1.Budget, D1.InstructorID,
            C2.CourseID, C2.Title, C2.Credits, C2.DepartmentID
        FROM Person P
            LEFT JOIN CourseAssignment CA ON P.ID = CA.InstructorID
                LEFT JOIN Course C ON CA.CourseID = C.CourseID
                    LEFT JOIN Department D ON C.DepartmentID = D.DepartmentID
            LEFT JOIN OfficeAssignment O ON P.ID = O.InstructorID
            LEFT JOIN Department D1 ON P.ID = D1.InstructorID
                LEFT JOIN Course C2 ON D1.DepartmentID = C2.DepartmentID
        WHERE P.Discriminator = 'Instructor'
        ORDER BY P.ID, CA.CourseID, D1.DepartmentID, C2.CourseID;
    """

// The shape mirrors the record blocks returned by the LEFT JOINs above.
// It interprets the result; it does not generate or execute the joins.
let shape =
    From<Person>(
        Join<CourseAssignment>(
            Join<Course>(
                Join<Department>())),
        Join<OfficeAssignment>(),
        Join<Department>(
            Join<Course>()))

let! instructors =
    fusionConnection.GetDataNodesAsync(
        sql,
        shape,
        null,
        context.CancellationToken)

return box instructors