Skip to content

Func<> with arguments #666

@Griboedoff

Description

@Griboedoff

net6, DryIoc 5.4.3

Hi!

I can't find the way to reuse depencency A when using creation Func for B.

My use case is this:

  • A is other service client that has some heavy initialization logic in constructor and it will be ok to register A as singletone.
  • B is client wrapper with string arg in constructor that specify user for A.

currently DryIoc calls A constructor every time getB called.

Is there a way to reuse A instance in creating B?

P.S. I saw docs for Func wrapper but asResolutionCall does not do the job.

using DryIoc;

var dc = new Container(
    r => r
        .WithMicrosoftDependencyInjectionRules()
        .WithDefaultIfAlreadyRegistered(IfAlreadyRegistered.Throw)
        .WithConcreteTypeDynamicRegistrations()
        .WithIgnoringReuseForFuncWithArgs());
dc.Register<A>(reuse: Reuse.Singleton);
dc.Register<B>();
var getB = dc.Resolve<Func<string, B>>();
getB("1");
getB("2");

class A
{
    public A()
        => Console.WriteLine("A created");
}

class B
{
    public B(A a, string s) {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions