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.

GenericFactory, get current requested type, like it was possible in StructureMap

See original GitHub issue

Hi, I know it’s not the best solution, but we have exactly that use case:

_.For(typeof(IFoo<>))
.Use(x =>
        {
            var ParamType = x.BuildStack.Current.RequestedType
                             .GetGenericArguments()[0];
            return BuildUsingFooFactory(ParamType);
        });
    }

Is there a way to get the information of the current requested type, like it was possible with StructureMap?

This question is quite similar, but unfortunately it has no answer: https://stackoverflow.com/questions/68832353/how-can-i-get-the-requested-type-in-a-usectx-factory-func-in-lamar-5 Br

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jeremydmillercommented, Feb 9, 2022

@rizi Sorry for being slow here, yes, just throw new NotSupportedException() in those methods. If I get around to it, I can finally formalize this pattern a bit. It comes up a couple times a year.

1reaction
jeremydmillercommented, Jan 11, 2022

You can use a custom Instance type for the parent, open type, then override the CloseType() method to return a subtype of LambaInstance that then calls through to your factory.

So something like:

public class MyBuilderInstance<T> : LambdaInstance<IFoo<T>>
{
    // and call through to the base constructor with s => BuildUsingFooFactory(typeof(T));
}

// and a custom instance like:

public class OpenFooInstance : Instance
{
  // override the CloseType() method to return the right MyBuilderInstance<T> where T would be the single template type
  // the service type should be typeof(IFoo<>)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Registering Method on Generic Factory with StructureMap
Current.RequestedType .GetGenericArguments()[0]; return BuildUsingFooFactory(ParamType); }); } private object BuildUsingFooFactory(Type ...
Read more >
Generic Types
At the first request for that interface, StructureMap will run through its "missing family policies", one of which is to try to find...
Read more >
Dependency Injection of Open Generics via factory #41050
TypedImplementationFactory { get; } + /// <summary> + /// Initializes a new instance of ... The type parameter is the requested serviceType.
Read more >
structuremap ObjectFactory.GetAllInstances<IHandle<TEvent ...
Coding example for the question structuremap ObjectFactory. ... them to the correct handler type and it get's a bit tricky.... a possible solution...
Read more >
ploeh blog danish software design
Only in Part 4 do you get extensive coverage of various DI ... can reuse them as much as you'd like, but only...
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