Skip to content

Commit

Permalink
Remove extra instance
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Apr 21, 2024
1 parent fb80c44 commit 1b5c7ee
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Shiny.Core/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,19 @@ public static IServiceCollection AddShinyService(this IServiceCollection service
.Where(x => x != typeof(IDisposable))
.ToList();

services.AddSingleton(implementationType);

if (interfaces.Any(x => x == typeof(INotifyPropertyChanged)) || interfaces.Any(x => x == typeof(IShinyComponentStartup)))
{
services.AddSingleton(implementationType, services =>
services.AddSingleton(implementationType, sp =>
{
var logger = services.GetRequiredService<ILoggerFactory>().CreateLogger("ShinyStartup");
var instance = ActivatorUtilities.CreateInstance(services, implementationType);
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("ShinyStartup");
var instance = ActivatorUtilities.CreateInstance(sp, implementationType);
var fn = implementationType.FullName;

if (instance is INotifyPropertyChanged npc)
{
logger.LogInformation("Startup Binding for " + fn);

services
sp
.GetRequiredService<IObjectStoreBinder>()
.Bind(npc);
}
Expand All @@ -113,6 +111,10 @@ public static IServiceCollection AddShinyService(this IServiceCollection service
interfaces.Remove(typeof(INotifyPropertyChanged));
interfaces.Remove(typeof(IShinyComponentStartup));
}
else
{
services.AddSingleton(implementationType);
}
foreach (var iface in interfaces)
{
services.AddSingleton(iface, sp => sp.GetRequiredService(implementationType));
Expand Down

0 comments on commit 1b5c7ee

Please sign in to comment.