Skip to content

Commit 21d4afa

Browse files
Add a few more ops
1 parent fce4e59 commit 21d4afa

3 files changed

Lines changed: 130 additions & 1 deletion

File tree

FSharp.AspNetCore.WebAppBuilder/FSharp.AspNetCore.WebAppBuilder.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<PackageId>FSharp.AspNetCore.WebAppBuilder</PackageId>
7-
<Version>0.1.0-alpha.8</Version>
7+
<Version>0.1.0-alpha.9</Version>
88
<Authors>Brian Rourke Boll</Authors>
99
<Description>A computation expression for succinctly defining ASP.NET Core web apps, including support for minimal APIs.</Description>
1010
<RepositoryType>git</RepositoryType>

FSharp.AspNetCore.WebAppBuilder/WebAppBuilder.fs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,132 @@ type WebAppBuilder internal (args : string array) =
312312
ignore <| builder.Services.AddSingleton<'TService> (implementationInstance=implementationInstance)
313313
builder
314314

315+
/// <summary>
316+
/// Adds a scoped service of the type specified in <paramref name="serviceType"/> to the
317+
/// app's service collection.
318+
/// </summary>
319+
/// <param name="builder">The web application builder.</param>
320+
/// <param name="serviceType">The type of the service to add.</param>
321+
/// <example>
322+
/// <code lang="fsharp">
323+
/// let app =
324+
/// webApp {
325+
/// scoped typeof&lt;Service&gt;
326+
/// }
327+
/// </code>
328+
/// </example>
329+
[<CustomOperation("scoped")>]
330+
member _.Scoped (builder : WebApplicationBuilder, serviceType : Type) =
331+
ignore <| builder.Services.AddScoped serviceType
332+
builder
333+
334+
/// <summary>
335+
/// Adds a scoped service of the type specified in <paramref name="serviceType"/> with
336+
/// an implementation of the type specified in <paramref name="implementationType"/> to the
337+
/// app's service collection.
338+
/// </summary>
339+
/// <param name="builder">The web application builder.</param>
340+
/// <param name="serviceType">The type of the service to add.</param>
341+
/// <param name="implementationType">The type of the service implementation to add.</param>
342+
/// <example>
343+
/// <code lang="fsharp">
344+
/// let app =
345+
/// webApp {
346+
/// scoped typeof&lt;IService&gt; typeof&lt;Service&gt;
347+
/// }
348+
/// </code>
349+
/// </example>
350+
[<CustomOperation("scoped")>]
351+
member _.Scoped (builder : WebApplicationBuilder, serviceType : Type, implementationType : Type) =
352+
ignore <| builder.Services.AddScoped (serviceType, implementationType)
353+
builder
354+
355+
/// <summary>
356+
/// Adds a scoped service of the type specified in <paramref name="serviceType"/> using
357+
/// an implementation provided by applying the given <paramref name="implementationFactory"/> to the
358+
/// app's service provider.
359+
/// </summary>
360+
/// <param name="builder">The web application builder.</param>
361+
/// <param name="serviceType">The type of the service to add.</param>
362+
/// <param name="implementationFactory">A function that produces the service implementation.</param>
363+
/// <example>
364+
/// <code lang="fsharp">
365+
/// let app =
366+
/// webApp {
367+
/// scoped
368+
/// typeof&lt;IService&gt;
369+
/// (fun serviceProvider -> Service (serviceProvider.GetRequiredService&lt;OtherService&gt; ()))
370+
/// }
371+
/// </code>
372+
/// </example>
373+
[<CustomOperation("scoped")>]
374+
member _.Scoped (builder : WebApplicationBuilder, serviceType : Type, implementationFactory : IServiceProvider -> 'TImplementation) =
375+
ignore <| builder.Services.AddScoped (serviceType=serviceType, implementationFactory=(implementationFactory >> box))
376+
builder
377+
378+
/// <summary>
379+
/// Adds a transient service of the type specified in <paramref name="serviceType"/> to the
380+
/// app's service collection.
381+
/// </summary>
382+
/// <param name="builder">The web application builder.</param>
383+
/// <param name="serviceType">The type of the service to add.</param>
384+
/// <example>
385+
/// <code lang="fsharp">
386+
/// let app =
387+
/// webApp {
388+
/// transient typeof&lt;Service&gt;
389+
/// }
390+
/// </code>
391+
/// </example>
392+
[<CustomOperation("transient")>]
393+
member _.Transient (builder : WebApplicationBuilder, serviceType : Type) =
394+
ignore <| builder.Services.AddTransient serviceType
395+
builder
396+
397+
/// <summary>
398+
/// Adds a transient service of the type specified in <paramref name="serviceType"/> with
399+
/// an implementation of the type specified in <paramref name="implementationType"/> to the
400+
/// app's service collection.
401+
/// </summary>
402+
/// <param name="builder">The web application builder.</param>
403+
/// <param name="serviceType">The type of the service to add.</param>
404+
/// <param name="implementationType">The type of the service implementation to add.</param>
405+
/// <example>
406+
/// <code lang="fsharp">
407+
/// let app =
408+
/// webApp {
409+
/// transient typeof&lt;IService&gt; typeof&lt;Service&gt;
410+
/// }
411+
/// </code>
412+
/// </example>
413+
[<CustomOperation("transient")>]
414+
member _.Transient (builder : WebApplicationBuilder, serviceType : Type, implementationType : Type) =
415+
ignore <| builder.Services.AddTransient (serviceType, implementationType)
416+
builder
417+
418+
/// <summary>
419+
/// Adds a transient service of the type specified in <paramref name="serviceType"/> using
420+
/// an implementation provided by applying the given <paramref name="implementationFactory"/> to the
421+
/// app's service provider.
422+
/// </summary>
423+
/// <param name="builder">The web application builder.</param>
424+
/// <param name="serviceType">The type of the service to add.</param>
425+
/// <param name="implementationFactory">A function that produces the service implementation.</param>
426+
/// <example>
427+
/// <code lang="fsharp">
428+
/// let app =
429+
/// webApp {
430+
/// transient
431+
/// typeof&lt;IService&gt;
432+
/// (fun serviceProvider -> Service (serviceProvider.GetRequiredService&lt;OtherService&gt; ()))
433+
/// }
434+
/// </code>
435+
/// </example>
436+
[<CustomOperation("transient")>]
437+
member _.Transient (builder : WebApplicationBuilder, serviceType : Type, implementationFactory : IServiceProvider -> 'TImplementation) =
438+
ignore <| builder.Services.AddTransient (serviceType=serviceType, implementationFactory=(implementationFactory >> box))
439+
builder
440+
315441
/// <summary>
316442
/// Adds a singleton service produced by applying the given <paramref name="configure"/> function
317443
/// to the <see cref="T:Microsoft.AspNetCore.Builder.WebApplicationBuilder"/>'s

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ A few more specialized custom operations are provided to make certain common sce
156156
- `connectionString`, for adding a strongly-typed connection string to the app's dependency injection container.
157157
- `configurationValue`, for adding a strongly-typed configuration value to the apps' dependency injection container.
158158
- `singleton`, for adding a singleton instance of a dependency to the app's dependency injection container.
159+
- `scoped`, for adding a scoped instance of a dependency to the app's dependency injection container.
160+
- `transient`, for adding a transient instance of a dependency to the app's dependency injection container.
159161
- `hostedService`, for adding a hosted background service to the app's dependency injection container.
162+
- `configure`, for configuring options registered via the "options pattern."
160163

161164
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.
162165

0 commit comments

Comments
 (0)