Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

Scope management enhancement #109

@oleneveu

Description

@oleneveu

Feature description

Hello,

I am in the process of evaluating the technical solutions for a new and relatively large Blazor/WASM project.
We are going to use MVVM, and on this topic I think that MvvmBlazor could help us save a lot of time and energy.

However the fact that all services within a view are resolved to a scope limited to that view could be an issue for us.
I understand that this behavior might be the best one in the general case, and that there is a workaround using the RootServiceProvider.
But this is an option that we might have to rely on quite a lot, and that would likely make our code much less clear.

I would like to suggest a change that would introduce some kind of "hook" so that the services resolution could be customized:

In the MvvmComponentBase class, the ScopeFactory would be declared as IMvvmServiceScopeFactory instead of IServiceScopeFactory.

public interface IMvvmServiceScopeFactory : IServiceScopeFactory { }

The default implementation for that interface would be:

internal class MvvmServiceScopeFactory : IMvvmServiceScopeFactory
{
    private readonly IServiceScopeFactory serviceScopeFactory;

    public MvvmServiceScopeFactory(IServiceScopeFactory serviceScopeFactory)
    {
        this.serviceScopeFactory = serviceScopeFactory;
    }

    public IServiceScope CreateScope() => serviceScopeFactory.CreateScope();
}

It would be registered in AddMvvm()

serviceCollection.AddSingleton<IMvvmServiceScopeFactory, MvvmServiceScopeFactory>();

The current behavior would be unchanged.

I have actually already tested that solution, and implemented a custom IMvvmServiceScopeFactory in a test project.
That implementation relies on an attribute on the classes and constructors parameters (ViewScopeAttribute) to decide on which scope they should be resolved.

I can send a pull request if you think that this feature would benefit to the project.
There should be many alternative solutions to achieve a similar result, we can also discuss them if you are not convinced by this one.

Regards,

Code sample

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: featureNew feature or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions