question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Questions about Func<> factories

See original GitHub issue

Hi @ipjohnson !

I have a few question regarding factories if you don’t mind answering them. These questions are about use cases where I inject a scope locator and it works absolutely fine. I was just wondering if there was a way to avoid taking a dependency on the full container. I know Grace has tons of features but they’re sometimes hard to discover! 😉

So I know that you can inject:

  • Func<T> for a factory resolving T in current scope.
  • Func<Scoped<T>> for creating a new (disposable) scope and resolving T inside of it.
  • Func<string, Scoped<T>> same as previous one, but you can give the scope a name.

Question 1: Can you get a factory to resolve a keyed T, the key being dynamically provided when calling the factory? (as opposed to: when importing the factory)

An example use case is having an interface IExporter and many implementation keyed by format (e.g. “xls”, “xlsx”, “cvs”) and then being to dynamically create from code an IExporter based on a specific format.

Question 2: Can you do the same as Q1 combined with Scoped? I.e. creating a new scope, and resolving a keyed T from code through a Func<>.

Question 3: Is it possible to import factories in one scope (e.g. global scope) and then use them to resolve T in a different scope provided dynamically?

Hypothetically I would import Func<IExportScopeLocator, T> in a global singleton, then use it like factory(scope) to get a T from a local scope.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ipjohnsoncommented, Aug 11, 2023

I did see the new APIs, very exciting. My plan is to add the interfaces as we get closer to .net 8 release date (late Nov).

1reaction
jods4commented, Aug 10, 2023

BTW, speaking of keyed injections:

@Lex45x @ipjohnson I’m sure you’ve seen .net 8 introduces built-in support for keyed services? https://weblogs.asp.net/ricardoperes/net-8-dependency-injection-changes-keyed-services

I suppose you’ll have to implement new interfaces to integrate Grace with the new built-in API such as AddKeyedSingleton().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Doubts regarding the use of factory function in java
I have some questions regarding this: why do we need a factory function instead of creating the class directly? What is the use...
Read more >
10 Function factories - Advanced R - Hadley Wickham
Section 10.4 uses function factories to tackle three challenges from statistics: understanding the Box-Cox transform, solving maximum likelihood problems, and ...
Read more >
JavaScript Question: What is a Factory Function? - YouTube
What is a factory function ? In this tutorial we are going to talk about what they are and when you might want...
Read more >
What are factory functions in JavaScript
Factory Functions differ from regular functions as they always return an object, which will contain any value, method, etc.
Read more >
Delegate Factories — Autofac 7.0.0 documentation
The first step in setting up a delegate factory is to create a delegate that will be used to dynamically resolve values from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found