Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Lazy Initialization for RegisterPerRequest #20

Open
1 task done
orankhu opened this issue Mar 29, 2022 · 1 comment
Open
1 task done

Support Lazy Initialization for RegisterPerRequest #20

orankhu opened this issue Mar 29, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@orankhu
Copy link

orankhu commented Mar 29, 2022

Feature request

Type

  • - Enhancement - completely new feature

Is your feature request related to a problem?

A controller or a service can have many underlying services to handle different requests. For example, ControllerA can have MyService and MyService can have ServiceA, ServiceB and so on.
image

The problem with this is all services are not used for some requests. an RequestForA request can only use ServiceA, or RequestForB can only use ServiceB. However, all services get initialized.

public class MyService : IMyService
{
    private readonly IServiceA _serviceA;
    private readonly IServiceB _serviceB;

    public MyService(IServiceA serviceA, IServiceB serviceB)
    {
        _serviceA = serviceA;
        _serviceB = serviceB;
    }

    public void DoWorkA()
    {
        _serviceA.DoWork();
    }

    public void DoWorkB()
    {
        _serviceB.DoWork();
    }
}

Describe the solution you'd like

One solution is to Singleton, so all initialization overhead get mitigated. However, doing so forces us to pass down request-only properties into method where we could store in request-only object and inject into services.

Or we can have a lazy initialization like this

[RegisterPerRequest(supportLazyInitialization: true)]
public class ServiceA : IServiceA
{
    public void DoWork(int value) 
    {
        // do something for A
    }
}
public class MyService : IMyService
{
    private readonly Lazy<IServiceA> _serviceA;
    private readonly Lazy<IServiceB> _serviceB;

    public MyService(Lazy<IServiceA> serviceA, Lazy<IServiceB> serviceB)
    {
        _serviceA = serviceA;
        _serviceB = serviceB;
    }

    public void DoWork(int value)
    {
        if (value < 42)
        {
            _serviceA.Value.DoWork();
        }
        else
  {
            _serviceB.Value.DoWork();
        }
    }
}
// Registering a type mapping for the lazy proxy.
builder
    .Register(c =>
    {
        var context = c.Resolve<IComponentContext>();
        return LazyProxyBuilder.CreateInstance(
            () => context.ResolveNamed<IMyService>(registrationName));
    })
    .As<IMyService>();

Additional context

@orankhu orankhu added the enhancement New feature or request label Mar 29, 2022
@szaboopeeter
Copy link
Member

As long as we're taking about controllers - controller actions can mark their parameters as [FromServices] (details link). This does not solve dependencies of "general" (non-controller) classes, nor the very niche case mentioned in your referred article, where we have different dependencies based on branches.

I wonder, do you have an example to show where this feature would actually provide benefits - and this is actually the right way to achieve that (as opposed to refactoring)? I'm NOT against the idea, just trying to understand use-cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants