Skip to content

Commit

Permalink
Fix DI
Browse files Browse the repository at this point in the history
  • Loading branch information
erdembayar committed Dec 22, 2023
1 parent 770b42b commit 1c4c856
Showing 1 changed file with 69 additions and 59 deletions.
128 changes: 69 additions & 59 deletions src/NuGetGallery/App_Start/DefaultDependenciesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<MarkdownService>()
.As<IMarkdownService>()
.InstancePerLifetimeScope();

builder.RegisterType<ImageDomainValidator>()
.As<IImageDomainValidator>()
.InstancePerLifetimeScope();
Expand All @@ -407,7 +407,17 @@ protected override void Load(ContainerBuilder builder)
.As<ICertificateService>()
.InstancePerLifetimeScope();

RegisterTyposquattingService(builder, loggerFactory);
builder.RegisterType<TyposquattingService>()
.AsSelf()
.As<ITyposquattingService>()
.InstancePerLifetimeScope();

builder.RegisterType<TyposquattingCheckListCacheService>()
.AsSelf()
.As<ITyposquattingCheckListCacheService>()
.SingleInstance();

RegisterTyposquattingServiceShim(builder, loggerFactory);

builder.RegisterType<LicenseExpressionSplitter>()
.As<ILicenseExpressionSplitter>()
Expand Down Expand Up @@ -723,31 +733,31 @@ private static void RegisterStatisticsServices(ContainerBuilder builder, IGaller
.Keyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);

builder.Register(c =>
{
var blobConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);
return new CloudBlobClientWrapper(blobConfiguration.ConnectionString, blobConfiguration.ReadAccessGeoRedundant);
})
{
var blobConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.PrimaryStatisticsKey);
return new CloudBlobClientWrapper(blobConfiguration.ConnectionString, blobConfiguration.ReadAccessGeoRedundant);
})
.SingleInstance()
.Keyed<ICloudBlobClient>(BindingKeys.PrimaryStatisticsKey);

builder.Register(c =>
{
var blobConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);
return new CloudBlobClientWrapper(blobConfiguration.ConnectionString, blobConfiguration.ReadAccessGeoRedundant);
})
{
var blobConfiguration = c.ResolveKeyed<IBlobStorageConfiguration>(BindingKeys.AlternateStatisticsKey);
return new CloudBlobClientWrapper(blobConfiguration.ConnectionString, blobConfiguration.ReadAccessGeoRedundant);
})
.SingleInstance()
.Keyed<ICloudBlobClient>(BindingKeys.AlternateStatisticsKey);

var hasSecondaryStatisticsSource = !string.IsNullOrWhiteSpace(configuration.Current.AzureStorage_Statistics_ConnectionString_Alternate);

builder.Register(c =>
{
if (hasSecondaryStatisticsSource && c.Resolve<IFeatureFlagService>().IsAlternateStatisticsSourceEnabled())
{
return c.ResolveKeyed<ICloudBlobClient>(BindingKeys.AlternateStatisticsKey);
}
return c.ResolveKeyed<ICloudBlobClient>(BindingKeys.PrimaryStatisticsKey);
})
if (hasSecondaryStatisticsSource && c.Resolve<IFeatureFlagService>().IsAlternateStatisticsSourceEnabled())
{
return c.ResolveKeyed<ICloudBlobClient>(BindingKeys.AlternateStatisticsKey);
}
return c.ResolveKeyed<ICloudBlobClient>(BindingKeys.PrimaryStatisticsKey);
})
.Keyed<ICloudBlobClient>(BindingKeys.FeatureFlaggedStatisticsKey);

// when running on Windows Azure, we use a back-end job to calculate stats totals and store in the blobs
Expand All @@ -761,17 +771,17 @@ private static void RegisterStatisticsServices(ContainerBuilder builder, IGaller
.SingleInstance();

builder.Register(c =>
{
var cloudBlobClientFactory = c.ResolveKeyed<Func<ICloudBlobClient>>(BindingKeys.FeatureFlaggedStatisticsKey);
var telemetryService = c.Resolve<ITelemetryService>();
var downloadCountServiceLogger = c.Resolve<ILogger<CloudDownloadCountService>>();
var downloadCountService = new CloudDownloadCountService(telemetryService, cloudBlobClientFactory, downloadCountServiceLogger);
{
var cloudBlobClientFactory = c.ResolveKeyed<Func<ICloudBlobClient>>(BindingKeys.FeatureFlaggedStatisticsKey);
var telemetryService = c.Resolve<ITelemetryService>();
var downloadCountServiceLogger = c.Resolve<ILogger<CloudDownloadCountService>>();
var downloadCountService = new CloudDownloadCountService(telemetryService, cloudBlobClientFactory, downloadCountServiceLogger);

var dlCountInterceptor = new DownloadCountObjectMaterializedInterceptor(downloadCountService, telemetryService);
ObjectMaterializedInterception.AddInterceptor(dlCountInterceptor);
var dlCountInterceptor = new DownloadCountObjectMaterializedInterceptor(downloadCountService, telemetryService);
ObjectMaterializedInterception.AddInterceptor(dlCountInterceptor);

return downloadCountService;
})
return downloadCountService;
})
.As<IDownloadCountService>()
.SingleInstance();

Expand Down Expand Up @@ -1466,10 +1476,10 @@ private static void ConfigureForAzureStorage(ContainerBuilder builder, IGalleryC
RegisterStatisticsServices(builder, configuration);

builder.Register(c =>
{
var configurationFactory = c.Resolve<Func<IAppConfiguration>>();
return new TableErrorLog(() => configurationFactory().AzureStorage_Errors_ConnectionString, configurationFactory().AzureStorageReadAccessGeoRedundant);
})
{
var configurationFactory = c.Resolve<Func<IAppConfiguration>>();
return new TableErrorLog(() => configurationFactory().AzureStorage_Errors_ConnectionString, configurationFactory().AzureStorageReadAccessGeoRedundant);
})
.As<ErrorLog>()
.SingleInstance();

Expand Down Expand Up @@ -1514,49 +1524,49 @@ private static void RegisterAuditingServices(ContainerBuilder builder, string st
if (storageType == StorageType.AzureStorage)
{
builder.Register(c =>
{
var configuration = c.Resolve<IAppConfiguration>();
return new CloudBlobClientWrapper(configuration.AzureStorage_Auditing_ConnectionString, configuration.AzureStorageReadAccessGeoRedundant);
})
{
var configuration = c.Resolve<IAppConfiguration>();
return new CloudBlobClientWrapper(configuration.AzureStorage_Auditing_ConnectionString, configuration.AzureStorageReadAccessGeoRedundant);
})
.SingleInstance()
.Keyed<ICloudBlobClient>(BindingKeys.AuditKey);

builder.Register(c =>
{
var blobClientFactory = c.ResolveKeyed<Func<ICloudBlobClient>>(BindingKeys.AuditKey);
return new CloudAuditingService(blobClientFactory, AuditActor.GetAspNetOnBehalfOfAsync);
})
{
var blobClientFactory = c.ResolveKeyed<Func<ICloudBlobClient>>(BindingKeys.AuditKey);
return new CloudAuditingService(blobClientFactory, AuditActor.GetAspNetOnBehalfOfAsync);
})
.SingleInstance()
.AsSelf()
.As<ICloudStorageStatusDependency>();
}

builder.Register(c =>
{
var configuration = c.Resolve<IAppConfiguration>();
IAuditingService defaultAuditingService = null;
switch (storageType)
{
case StorageType.FileSystem:
case StorageType.NotSpecified:
defaultAuditingService = GetAuditingServiceForLocalFileSystem(configuration);
break;

case StorageType.AzureStorage:
defaultAuditingService = c.Resolve<CloudAuditingService>();
break;
}
var configuration = c.Resolve<IAppConfiguration>();
IAuditingService defaultAuditingService = null;
switch (storageType)
{
case StorageType.FileSystem:
case StorageType.NotSpecified:
defaultAuditingService = GetAuditingServiceForLocalFileSystem(configuration);
break;

case StorageType.AzureStorage:
defaultAuditingService = c.Resolve<CloudAuditingService>();
break;
}

var auditingServices = GetAddInServices<IAuditingService>();
var services = new List<IAuditingService>(auditingServices);
var auditingServices = GetAddInServices<IAuditingService>();
var services = new List<IAuditingService>(auditingServices);

if (defaultAuditingService != null)
{
services.Add(defaultAuditingService);
}
if (defaultAuditingService != null)
{
services.Add(defaultAuditingService);
}

return CombineAuditingServices(services);
})
return CombineAuditingServices(services);
})
.AsSelf()
.As<IAuditingService>()
.SingleInstance();
Expand All @@ -1579,7 +1589,7 @@ private static void RegisterCookieComplianceService(ConfigurationService configu
CookieComplianceService.Initialize(service ?? new NullCookieComplianceService(), logger);
}

private static void RegisterTyposquattingService(ContainerBuilder builder, ILoggerFactory loggerFactory)
private static void RegisterTyposquattingServiceShim(ContainerBuilder builder, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger(nameof(ITyposquattingServiceShim));

Expand Down

0 comments on commit 1c4c856

Please sign in to comment.