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

[API Proposal]: ActivatorUtilities.CreateFactory<T> with params span #101889

Open
alrz opened this issue May 5, 2024 · 1 comment
Open

[API Proposal]: ActivatorUtilities.CreateFactory<T> with params span #101889

alrz opened this issue May 5, 2024 · 1 comment
Assignees
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner

Comments

@alrz
Copy link

alrz commented May 5, 2024

Background and motivation

The arguments passed to CreateFactory and ObjectFactory are usually of a fixed number so it may be worthwhile to consider params span for both instead of array.

API Proposal

I haven't seen an addition like this so far so I'm not sure about the naming convention.

namespace Microsoft.Extensions.DependencyInjection
{
  public delegate T ObjectFactory2<out T>(IServiceProvider serviceProvider, params ReadOnlySpan<object?> arguments); 

  public static class ActivatorUtilities
  {
    public static ObjectFactory2<T> CreateFactory2<T>(params ReadOnlySpan<Type> argumentTypes)
  }
}

API Usage

var factory = ActivatorUtilities.CreateFactory<T>(typeof(SomeType));
var result = factory(serviceProvider, new SomeType());

Alternative Designs

An alternative is to consider making the return type a generic delegate over argument types.

public static ObjectFactory<TArg1, T> CreateFactory<TArg1, T>();
public static ObjectFactory<TArg1, TArg2, T> CreateFactory<TArg1, TArg2, T>();
// etc
public delegate T ObjectFactory<in TArg1, out T>(IServiceProvider serviceProvider, TArg1? arg1);
public delegate T ObjectFactory<in TArg1, in TArg2, out T>(IServiceProvider serviceProvider, TArg1? arg1, TArg2? arg2);
// etc

Risks

No response

@alrz alrz added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 5, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants