-
Notifications
You must be signed in to change notification settings - Fork 71
Add DI recipe #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add DI recipe #353
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
> This recipe is not a detailed discussion of the pros and cons of Dependency Injection (DI) compared to other patterns. It simply illustrates how to use it within a SAFE Stack application! | ||
|
||
1. Create a class that you wish to inject with a dependency (in this example, we use the built-in `IConfiguration` type that is included in ASP .NET): | ||
|
||
```fsharp | ||
open Microsoft.Extensions.Configuration | ||
|
||
type DatabaseRepository(config:IConfiguration) = | ||
member _.SaveDataToDb (text:string) = | ||
let connectionString = config["SqlConnectionString"] | ||
// Connect to SQL using the above connection string etc. | ||
Ok 1 | ||
``` | ||
|
||
> Instead of functions or modules, DI in .NET and F# only works with classes. | ||
|
||
2. Register your type with ASP .NET during startup within the `application { }` block. | ||
```diff | ||
++ open Microsoft.Extensions.DependencyInjection | ||
|
||
application { | ||
//... | ||
++ service_config (fun services -> services.AddSingleton<DatabaseRepository>()) | ||
``` | ||
|
||
> [This section](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-8.0#lifetime-and-registration-options) of the official ASP .NET Core article explain the distinction between different lifetime registrations, such as Singleton and Transient. | ||
|
||
3. Ensure that your Fable Remoting API can access the `HttpContext` type by using the `fromContext` builder function. | ||
```diff | ||
-- |> Remoting.fromValue createFableRemotingApi | ||
++ |> Remoting.fromContext createFableRemotingApi | ||
``` | ||
|
||
4. Within your Fable Remoting API, use the supplied `context` to retrieve your dependency: | ||
|
||
```diff | ||
++ open Microsoft.AspNetCore.Http | ||
|
||
let createFableRemotingApi | ||
++ (context:HttpContext) = | ||
++ let dbRepository = context.GetService<DatabaseRepository>() | ||
// ... | ||
// Return the constructed API record value... | ||
``` | ||
|
||
> Giraffe provides the `GetService<'T>` extension to allow you to quickly retrieve a dependency from the `HttpContext`. | ||
|
||
This will instruct ASP .NET to get a handle to the `DatabaseRepository` object; ASP .NET will automatically supply the `IConfiguration` object to the constructor. Whether a new `DatabaseRepository` object is constructed on each call depends on the lifetime you have registered it with. | ||
|
||
> You can have your types depend on other types that you create, as long as they are registering into ASP .NET Core's DI container using methods such as `AddSingleton` etc. | ||
|
||
## Further Reading | ||
* [Official documentation on DI in ASP .NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-8.0) | ||
* [Archived example PR to update the SAFE Template Todo App to use DI](https://github.com/SAFE-Stack/SAFE-template/pull/466/files) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.