Skip to content

Commit

Permalink
Add a few more ops
Browse files Browse the repository at this point in the history
  • Loading branch information
brianrourkeboll committed Feb 22, 2023
1 parent fce4e59 commit 21d4afa
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>FSharp.AspNetCore.WebAppBuilder</PackageId>
<Version>0.1.0-alpha.8</Version>
<Version>0.1.0-alpha.9</Version>
<Authors>Brian Rourke Boll</Authors>
<Description>A computation expression for succinctly defining ASP.NET Core web apps, including support for minimal APIs.</Description>
<RepositoryType>git</RepositoryType>
Expand Down
126 changes: 126 additions & 0 deletions FSharp.AspNetCore.WebAppBuilder/WebAppBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,132 @@ type WebAppBuilder internal (args : string array) =
ignore <| builder.Services.AddSingleton<'TService> (implementationInstance=implementationInstance)
builder

/// <summary>
/// Adds a scoped service of the type specified in <paramref name="serviceType"/> to the
/// app's service collection.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="serviceType">The type of the service to add.</param>
/// <example>
/// <code lang="fsharp">
/// let app =
/// webApp {
/// scoped typeof&lt;Service&gt;
/// }
/// </code>
/// </example>
[<CustomOperation("scoped")>]
member _.Scoped (builder : WebApplicationBuilder, serviceType : Type) =
ignore <| builder.Services.AddScoped serviceType
builder

/// <summary>
/// Adds a scoped service of the type specified in <paramref name="serviceType"/> with
/// an implementation of the type specified in <paramref name="implementationType"/> to the
/// app's service collection.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="serviceType">The type of the service to add.</param>
/// <param name="implementationType">The type of the service implementation to add.</param>
/// <example>
/// <code lang="fsharp">
/// let app =
/// webApp {
/// scoped typeof&lt;IService&gt; typeof&lt;Service&gt;
/// }
/// </code>
/// </example>
[<CustomOperation("scoped")>]
member _.Scoped (builder : WebApplicationBuilder, serviceType : Type, implementationType : Type) =
ignore <| builder.Services.AddScoped (serviceType, implementationType)
builder

/// <summary>
/// Adds a scoped service of the type specified in <paramref name="serviceType"/> using
/// an implementation provided by applying the given <paramref name="implementationFactory"/> to the
/// app's service provider.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="serviceType">The type of the service to add.</param>
/// <param name="implementationFactory">A function that produces the service implementation.</param>
/// <example>
/// <code lang="fsharp">
/// let app =
/// webApp {
/// scoped
/// typeof&lt;IService&gt;
/// (fun serviceProvider -> Service (serviceProvider.GetRequiredService&lt;OtherService&gt; ()))
/// }
/// </code>
/// </example>
[<CustomOperation("scoped")>]
member _.Scoped (builder : WebApplicationBuilder, serviceType : Type, implementationFactory : IServiceProvider -> 'TImplementation) =
ignore <| builder.Services.AddScoped (serviceType=serviceType, implementationFactory=(implementationFactory >> box))
builder

/// <summary>
/// Adds a transient service of the type specified in <paramref name="serviceType"/> to the
/// app's service collection.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="serviceType">The type of the service to add.</param>
/// <example>
/// <code lang="fsharp">
/// let app =
/// webApp {
/// transient typeof&lt;Service&gt;
/// }
/// </code>
/// </example>
[<CustomOperation("transient")>]
member _.Transient (builder : WebApplicationBuilder, serviceType : Type) =
ignore <| builder.Services.AddTransient serviceType
builder

/// <summary>
/// Adds a transient service of the type specified in <paramref name="serviceType"/> with
/// an implementation of the type specified in <paramref name="implementationType"/> to the
/// app's service collection.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="serviceType">The type of the service to add.</param>
/// <param name="implementationType">The type of the service implementation to add.</param>
/// <example>
/// <code lang="fsharp">
/// let app =
/// webApp {
/// transient typeof&lt;IService&gt; typeof&lt;Service&gt;
/// }
/// </code>
/// </example>
[<CustomOperation("transient")>]
member _.Transient (builder : WebApplicationBuilder, serviceType : Type, implementationType : Type) =
ignore <| builder.Services.AddTransient (serviceType, implementationType)
builder

/// <summary>
/// Adds a transient service of the type specified in <paramref name="serviceType"/> using
/// an implementation provided by applying the given <paramref name="implementationFactory"/> to the
/// app's service provider.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="serviceType">The type of the service to add.</param>
/// <param name="implementationFactory">A function that produces the service implementation.</param>
/// <example>
/// <code lang="fsharp">
/// let app =
/// webApp {
/// transient
/// typeof&lt;IService&gt;
/// (fun serviceProvider -> Service (serviceProvider.GetRequiredService&lt;OtherService&gt; ()))
/// }
/// </code>
/// </example>
[<CustomOperation("transient")>]
member _.Transient (builder : WebApplicationBuilder, serviceType : Type, implementationFactory : IServiceProvider -> 'TImplementation) =
ignore <| builder.Services.AddTransient (serviceType=serviceType, implementationFactory=(implementationFactory >> box))
builder

/// <summary>
/// Adds a singleton service produced by applying the given <paramref name="configure"/> function
/// to the <see cref="T:Microsoft.AspNetCore.Builder.WebApplicationBuilder"/>'s
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ A few more specialized custom operations are provided to make certain common sce
- `connectionString`, for adding a strongly-typed connection string to the app's dependency injection container.
- `configurationValue`, for adding a strongly-typed configuration value to the apps' dependency injection container.
- `singleton`, for adding a singleton instance of a dependency to the app's dependency injection container.
- `scoped`, for adding a scoped instance of a dependency to the app's dependency injection container.
- `transient`, for adding a transient instance of a dependency to the app's dependency injection container.
- `hostedService`, for adding a hosted background service to the app's dependency injection container.
- `configure`, for configuring options registered via the "options pattern."

See the [API documentation](https://brianrourkeboll.github.io/FSharp.AspNetCore.WebAppBuilder/reference/fsharp-aspnetcore-builder-webappbuilder.html) for the full set of supported operations, with examples.

Expand Down

0 comments on commit 21d4afa

Please sign in to comment.