https://github.com/seclerp/dependx
Library that provides F# DSL for dependency injection registration for your favorite IoC provider
https://github.com/seclerp/dependx
autofac dependency-injection fsharp ioc
Last synced: 8 months ago
JSON representation
Library that provides F# DSL for dependency injection registration for your favorite IoC provider
- Host: GitHub
- URL: https://github.com/seclerp/dependx
- Owner: seclerp
- License: mit
- Created: 2020-01-08T02:47:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-22T16:20:28.000Z (over 6 years ago)
- Last Synced: 2025-05-24T05:37:29.483Z (about 1 year ago)
- Topics: autofac, dependency-injection, fsharp, ioc
- Language: F#
- Homepage:
- Size: 41 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DependX
Library that provides F# DSL for dependency injection registration for your favorite IoC provider
---
:warning: **Project in active development now and currently only supports Autofac container.**
DependX allows you to change this:
```fsharp
let deps =
let containerBuilder = ContainerBuilder()
containerBuilder.RegisterType()
.AsSelf()
.SingleInstance()
.Named("Some name")
.WithParameter("abc", 123)
.WithParameter("abc2", fun _ -> 123) |> ignore
containerBuilder.RegisterType()
.As() |> ignore
containerBuilder
```
Into this:
```fsharp
let deps () = dependencies [
configure selfContract {
lifetime singleton
named "Some name"
param "abc" (fromInstance 123)
param "abc2" (fromFactory <| fun () -> 123)
}
contract
]
```
Also there are some helper functions to nicely get started into your .NET Core or ASP.NET Core environment
Core library built on top of **.NET Startard 2.0**
ASP.NET Core extensions made for **ASP.NET Core 2.2+**
Autofac libraries currently built on top of **Autofac 4.9.4** and **Autofac.Extensions.DependencyInjection 5.0.1**
### Getting started with ASP.NET Core and Autofac
Before we start, let's assume that there are some dependencies:
```fsharp
// ExampleServices.fs
module SomeProject.ExampleServices
type ICustomLogger =
abstract LogInfo : string -> unit
type IMessageService = abstract SayHello : unit -> unit
type CustomLogger() =
interface ICustomLogger with
member _.LogInfo(str) = printfn "%s" str
type MessageService(logger: ICustomLogger) =
interface IHelloService with
member _.Say(message) = logger.LogInfo message
type StartupService(messageService: IMessageService) =
interface IHostedService with
member _.StartAsync(token: CancellationToken) =
messageService.Say("Hello")
Task.CompletedTask
member _.StopAsync(_: CancellationToken) =
Task.CompletedTask
```
Here we defined dummy logger, `IMessageService` that uses that logger and `IHostedService` that on start uses our `IMessageService` for writing some stuff.
Then, lets define our dependencies module:
```fsharp
// Dependencies.fs
module SomeProject.Dependencies
open DependX
open DependX.Autofac
open DependX.AspNetCore
open ExampleServices
let register = dependencies [
// ...
]
```
`open DependX` - for core types and logic
`open DependX.Autofac` - for autofac-specific interpreter
`open DependX.AspNetCore` - for ASP.NET Core related stuff, in our case - `hostedService`
`let register = dependencies [...]` - this is our initial dependency composition. We will declare dependencies inside that block.
Lets add our newly created dependencies:
```fsharp
// Dependencies.fs
// ...
let register = dependencies [
configure contract {
lifetime singleton
}
contract
hostedService
]
```
In that case we registered `CustomLogger` as singleton and `SomeService` as transient (by default). We also added StartupService as IHostedService just like regular `.AddHostedService` in ASP.NET Core DI.
That's it. We only need to provide information how dependency need to be registered, **there are no direct usage on Autofac related stuff like ContainerBuilder**.
After that we need to use our register function in `Startup.fs` class:
```fsharp
// Startup.fs
// ...
open Dependencies
// ...
type Startup(...) =
member this.ConfigureContainer(builder: ContainerBuilder) =
register builder |> ignore
```
And (if you don't added it yet) configure Autofac in `Program.fs`:
```fsharp
// ...
open DependX.AspNetCore.Autofac
// ...
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(AutofacServiceProviderFactory()) // ASP.NET Core 3+ related stuff
.ConfigureWebHostDefaults(fun webBuilder ->
webBuilder.UseStartup() |> ignore
webBuilder |> configureAutofacDefaults
)
```
After project start you will see
```
Hello
```
in the console window. More information about what functions you could use for registratino can be found in **[documentation](DOCUMENTATION.md)**
### Solution structure
#### Core
- `DependX.Core` - core library with main logic for creating abstract dependencies
#### Autofac
- `DependX.Autofac` - provides interpret functions that produces Autofac IoC container from abstract dependencies
#### ASP.NET Core extensions
- `DependX.AspNetCore` - provides helper functions to support injection of ASP.NET Core related services (like IHostedService)
- `DependX.AspNetCore.Autofac` - provides helper functions to configure and use Autofac in ASP.NET Core
#### Tests
- `DependX.Tests` - contains unit tests for core logic
### Documentation
You could found full documentation **[here](DOCUMENTATION.md)**
### Contributing
Feel free to submit issues and PR's. If you have any questions contact me via **[email](mailto:andrewrublyov99@gmail.com)** or **[telegram](https://t.me/FreeParticle)**.