Functional dependency injection with ASP.NET? #1188
Unanswered
EivindAntonsen
asked this question in
Q&A
Replies: 2 comments 5 replies
-
@EivindAntonsen If you create your runtime with an internal class based environment the integration is relatively straight forward. // some runtime
public readonly struct Runtime :
HasActivitySource<Runtime>,
HasConsole<Runtime>,
HasFile<Runtime>,
HasTextRead<Runtime>,
HasTime<Runtime>,
HasEnvironment<Runtime>,
HasDirectory<Runtime>
{
readonly RuntimeEnv env; // this holds your service instances and can be constructed via DI and passed into the runtime So then you create the RuntimeEnv via DI and pass that to the runtime and you are done. var env = serviceProvider.GetRequiredService<RuntimeEnv>();
var rt = new Runtime(env); // or via a constructor function Runtime.New(env)
... Now its integrated. The only requirement is that the env needs to be a class or record. public record RuntimeEnv(
ActivityEnv Activity,
CancellationTokenSource Source,
CancellationToken Token,
Encoding Encoding); At this stage IMHO I dont think it needs to be any more complicated than that. Just wire it into the service collection, // wire it in however you want
services.AddScoped(sp => new RuntimeEnv(...)); |
Beta Was this translation helpful? Give feedback.
4 replies
-
All standard library traits.
Check out the Sys and SysX projects.
…On Sun, 19 Mar. 2023, 6:44 pm Eivind Antonsen, ***@***.***> wrote:
@bmazzarol <https://github.com/bmazzarol> Thanks for your response! This
looks helpful. These traits you've defined there, are they found in the
library or are they something you crafted from scratch?
—
Reply to this email directly, view it on GitHub
<#1188 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAV7ZLSDM54ZO45A5D75JIDW43PQJANCNFSM6AAAAAAUVLLUJI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there,
I've been learning more and more about this library, and now I am a bit stuck on bridging functional dependency injection with what's available to use in ASP.NET with web api type projects etc.
So I've just started using Runtimes and trait interfaces. This discussion here showcases the type of runtime usage I'm curious about how to fit into an ASP.Net type project (if at all possible - I'm getting the impression that is not a match meant to be!). You can only register reference types as services using the servicebuilder in an asp.net web api project, and the Runtime as seen in this example is of course a struct. How decoupled from ASP.Net will I have to go to use mostly functional principles?
In short - a regular project with controllers, service layer, data access - where does functional dependency injection fit in?
Beta Was this translation helpful? Give feedback.
All reactions