From 21d4afa843ef5c1ff8e1d8bb022e2ce9a4dde452 Mon Sep 17 00:00:00 2001 From: Brian Rourke Boll Date: Wed, 22 Feb 2023 10:52:02 -0500 Subject: [PATCH] Add a few more ops --- .../FSharp.AspNetCore.WebAppBuilder.fsproj | 2 +- .../WebAppBuilder.fs | 126 ++++++++++++++++++ README.md | 3 + 3 files changed, 130 insertions(+), 1 deletion(-) diff --git a/FSharp.AspNetCore.WebAppBuilder/FSharp.AspNetCore.WebAppBuilder.fsproj b/FSharp.AspNetCore.WebAppBuilder/FSharp.AspNetCore.WebAppBuilder.fsproj index 655e6e0..f94908a 100644 --- a/FSharp.AspNetCore.WebAppBuilder/FSharp.AspNetCore.WebAppBuilder.fsproj +++ b/FSharp.AspNetCore.WebAppBuilder/FSharp.AspNetCore.WebAppBuilder.fsproj @@ -4,7 +4,7 @@ net6.0;net7.0 true FSharp.AspNetCore.WebAppBuilder - 0.1.0-alpha.8 + 0.1.0-alpha.9 Brian Rourke Boll A computation expression for succinctly defining ASP.NET Core web apps, including support for minimal APIs. git diff --git a/FSharp.AspNetCore.WebAppBuilder/WebAppBuilder.fs b/FSharp.AspNetCore.WebAppBuilder/WebAppBuilder.fs index c4943fd..18380ba 100644 --- a/FSharp.AspNetCore.WebAppBuilder/WebAppBuilder.fs +++ b/FSharp.AspNetCore.WebAppBuilder/WebAppBuilder.fs @@ -312,6 +312,132 @@ type WebAppBuilder internal (args : string array) = ignore <| builder.Services.AddSingleton<'TService> (implementationInstance=implementationInstance) builder + /// + /// Adds a scoped service of the type specified in to the + /// app's service collection. + /// + /// The web application builder. + /// The type of the service to add. + /// + /// + /// let app = + /// webApp { + /// scoped typeof<Service> + /// } + /// + /// + [] + member _.Scoped (builder : WebApplicationBuilder, serviceType : Type) = + ignore <| builder.Services.AddScoped serviceType + builder + + /// + /// Adds a scoped service of the type specified in with + /// an implementation of the type specified in to the + /// app's service collection. + /// + /// The web application builder. + /// The type of the service to add. + /// The type of the service implementation to add. + /// + /// + /// let app = + /// webApp { + /// scoped typeof<IService> typeof<Service> + /// } + /// + /// + [] + member _.Scoped (builder : WebApplicationBuilder, serviceType : Type, implementationType : Type) = + ignore <| builder.Services.AddScoped (serviceType, implementationType) + builder + + /// + /// Adds a scoped service of the type specified in using + /// an implementation provided by applying the given to the + /// app's service provider. + /// + /// The web application builder. + /// The type of the service to add. + /// A function that produces the service implementation. + /// + /// + /// let app = + /// webApp { + /// scoped + /// typeof<IService> + /// (fun serviceProvider -> Service (serviceProvider.GetRequiredService<OtherService> ())) + /// } + /// + /// + [] + member _.Scoped (builder : WebApplicationBuilder, serviceType : Type, implementationFactory : IServiceProvider -> 'TImplementation) = + ignore <| builder.Services.AddScoped (serviceType=serviceType, implementationFactory=(implementationFactory >> box)) + builder + + /// + /// Adds a transient service of the type specified in to the + /// app's service collection. + /// + /// The web application builder. + /// The type of the service to add. + /// + /// + /// let app = + /// webApp { + /// transient typeof<Service> + /// } + /// + /// + [] + member _.Transient (builder : WebApplicationBuilder, serviceType : Type) = + ignore <| builder.Services.AddTransient serviceType + builder + + /// + /// Adds a transient service of the type specified in with + /// an implementation of the type specified in to the + /// app's service collection. + /// + /// The web application builder. + /// The type of the service to add. + /// The type of the service implementation to add. + /// + /// + /// let app = + /// webApp { + /// transient typeof<IService> typeof<Service> + /// } + /// + /// + [] + member _.Transient (builder : WebApplicationBuilder, serviceType : Type, implementationType : Type) = + ignore <| builder.Services.AddTransient (serviceType, implementationType) + builder + + /// + /// Adds a transient service of the type specified in using + /// an implementation provided by applying the given to the + /// app's service provider. + /// + /// The web application builder. + /// The type of the service to add. + /// A function that produces the service implementation. + /// + /// + /// let app = + /// webApp { + /// transient + /// typeof<IService> + /// (fun serviceProvider -> Service (serviceProvider.GetRequiredService<OtherService> ())) + /// } + /// + /// + [] + member _.Transient (builder : WebApplicationBuilder, serviceType : Type, implementationFactory : IServiceProvider -> 'TImplementation) = + ignore <| builder.Services.AddTransient (serviceType=serviceType, implementationFactory=(implementationFactory >> box)) + builder + /// /// Adds a singleton service produced by applying the given function /// to the 's diff --git a/README.md b/README.md index 2961821..6de580a 100644 --- a/README.md +++ b/README.md @@ -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.