Skip to content

Commit

Permalink
Use Factory methods in DependencyInjection (dotnet#5290)
Browse files Browse the repository at this point in the history
* Use Factory methods in DependencyInjection

Using factory methods and directly invoking the constructors instead of letting DI use reflection to find the constructor.

In local testing this appeared to shave off ~7ms from startup of a dotnet new maui app.

* Introduce BannedApiAnalyzers

- Add TryAddSingleton`2 to the banned API list
  • Loading branch information
eerhardt authored Mar 15, 2022
1 parent 93092a0 commit e7812b0
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Import Condition="'$(SampleProject)' == 'true' or '$(CI)' != 'true' " Project="eng\Versions.dev.targets" />
<Import Condition="'$(SampleProject)' != 'true' and '$(CI)' == 'true'" Project="eng\Git.Build.targets" />
<Import Condition="'$(SampleProject)' != 'true' and '$(CI)' == 'true' " Project="eng\Versions.targets" />
<Import Condition="'$(SampleProject)' != 'true' and '$(IsTestProject)' != 'true'" Project="eng\BannedApis.targets" />
<Import Project="eng\AndroidX.targets" />
<Import Project="eng\Microsoft.Extensions.targets" />

Expand Down
10 changes: 10 additions & 0 deletions eng/BannedApis.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="$(MicrosoftCodeAnalysisBannedApiAnalyzersVersion)" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<!-- Configure analyzer to forbid certain API calls -->
<AdditionalFiles Include="$(MSBuildThisFileDirectory)BannedSymbols.txt" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions eng/BannedSymbols.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
M:Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddSingleton`2(Microsoft.Extensions.DependencyInjection.IServiceCollection);Use a Factory method to create the service instead
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<MicrosoftJSInteropPackageVersion>6.0.2</MicrosoftJSInteropPackageVersion>
<MicrosoftWindowsDesktopAppRuntimewinx64Version>6.0.2</MicrosoftWindowsDesktopAppRuntimewinx64Version>
<!-- Other packages -->
<MicrosoftCodeAnalysisBannedApiAnalyzersVersion>3.3.3</MicrosoftCodeAnalysisBannedApiAnalyzersVersion>
<MicrosoftMauiGraphicsVersion>6.0.200-preview.14.1092</MicrosoftMauiGraphicsVersion>
<SystemNumericsVectorsVersion>4.5.0</SystemNumericsVectorsVersion>
<_MicrosoftWebWebView2Version>1.0.1020.30</_MicrosoftWebWebView2Version>
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public static partial class AppHostBuilderExtensions
public static MauiAppBuilder UseMauiApp<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TApp>(this MauiAppBuilder builder)
where TApp : class, IApplication
{
#pragma warning disable RS0030 // Do not used banned APIs - don't want to use a factory method here
builder.Services.TryAddSingleton<IApplication, TApp>();
#pragma warning restore RS0030
builder.SetupDefaults();
return builder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Hosting/HandlerMauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static MauiAppBuilder ConfigureMauiHandlers(this MauiAppBuilder builder,

public static IServiceCollection ConfigureMauiHandlers(this IServiceCollection services, Action<IMauiHandlersCollection>? configureDelegate)
{
services.TryAddSingleton<IMauiHandlersFactory, MauiHandlersFactory>();
services.TryAddSingleton<IMauiHandlersFactory>(sp => new MauiHandlersFactory(sp.GetServices<HandlerRegistration>()));
if (configureDelegate != null)
{
services.AddSingleton<HandlerRegistration>(new HandlerRegistration(configureDelegate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static MauiAppBuilder ConfigureImageSources(this MauiAppBuilder builder,
}

builder.Services.TryAddSingleton<IImageSourceServiceProvider>(svcs => new ImageSourceServiceProvider(svcs.GetRequiredService<IImageSourceServiceCollection>(), svcs));
builder.Services.TryAddSingleton<IImageSourceServiceCollection, ImageSourceServiceBuilder>();
builder.Services.TryAddSingleton<IImageSourceServiceCollection>(svcs => new ImageSourceServiceBuilder(svcs.GetServices<ImageSourceRegistration>()));

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static partial class MauiAppHostBuilderExtensions
{
public static MauiAppBuilder ConfigureLifecycleEvents(this MauiAppBuilder builder, Action<ILifecycleBuilder>? configureDelegate)
{
builder.Services.TryAddSingleton<ILifecycleEventService, LifecycleEventService>();
builder.Services.TryAddSingleton<ILifecycleEventService>(sp => new LifecycleEventService(sp.GetServices<LifecycleEventRegistration>()));
if (configureDelegate != null)
{
builder.Services.AddSingleton<LifecycleEventRegistration>(new LifecycleEventRegistration(configureDelegate));
Expand Down

0 comments on commit e7812b0

Please sign in to comment.