Merge branch 'v10/dev' into v10/contrib

# Conflicts:
#	src/Umbraco.Core/Actions/ActionAssignDomain.cs
#	src/Umbraco.Core/Actions/ActionCopy.cs
#	src/Umbraco.Core/Actions/ActionCreateBlueprintFromContent.cs
#	src/Umbraco.Core/Actions/ActionDelete.cs
#	src/Umbraco.Core/Actions/ActionMove.cs
#	src/Umbraco.Core/Actions/ActionNew.cs
#	src/Umbraco.Core/Actions/ActionNotify.cs
#	src/Umbraco.Core/Actions/ActionProtect.cs
#	src/Umbraco.Core/Actions/ActionRestore.cs
#	src/Umbraco.Core/Actions/ActionRights.cs
#	src/Umbraco.Core/Actions/ActionRollback.cs
#	src/Umbraco.Core/Actions/ActionSort.cs
#	src/Umbraco.Core/Actions/ActionToPublish.cs
#	src/Umbraco.Core/Actions/ActionUnpublish.cs
#	src/Umbraco.Core/Actions/ActionUpdate.cs
#	src/Umbraco.Core/Constants-Telemetry.cs
#	src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs
#	src/Umbraco.Core/DependencyInjection/UmbracoBuilder.CollectionBuilders.cs
#	src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Collections.cs
#	src/Umbraco.Core/Models/Trees/ActionMenuItem.cs
#	src/Umbraco.Core/Models/Trees/CreateChildEntity.cs
#	src/Umbraco.Core/Models/Trees/ExportMember.cs
#	src/Umbraco.Core/Models/Trees/MenuItem.cs
#	src/Umbraco.Core/Models/Trees/RefreshNode.cs
#	src/Umbraco.Core/Persistence/Repositories/IRedirectUrlRepository.cs
#	src/Umbraco.Core/PropertyEditors/NestedContentConfiguration.cs
#	src/Umbraco.Core/Routing/ContentFinderByRedirectUrl.cs
#	src/Umbraco.Core/Services/IFileService.cs
#	src/Umbraco.Core/Services/IRedirectUrlService.cs
#	src/Umbraco.Core/Services/RedirectUrlService.cs
#	src/Umbraco.Core/Trees/MenuItemList.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs
#	src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RedirectUrlRepository.cs
#	src/Umbraco.Infrastructure/Telemetry/Providers/SystemInformationTelemetryProvider.cs
#	src/Umbraco.Web.BackOffice/Controllers/CodeFileController.cs
#	src/Umbraco.Web.BackOffice/Controllers/ContentController.cs
#	src/Umbraco.Web.BackOffice/Controllers/DictionaryController.cs
#	src/Umbraco.Web.BackOffice/Controllers/EntityController.cs
#	src/Umbraco.Web.BackOffice/Controllers/PreviewController.cs
#	src/Umbraco.Web.BackOffice/Trees/ContentBlueprintTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/ContentTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs
#	src/Umbraco.Web.BackOffice/Trees/ContentTypeTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/DataTypeTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/DictionaryTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/FileSystemTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/MacrosTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/MediaTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/MediaTypeTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/MemberGroupTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/MemberTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/MemberTypeAndGroupTreeControllerBase.cs
#	src/Umbraco.Web.BackOffice/Trees/RelationTypeTreeController.cs
#	src/Umbraco.Web.BackOffice/Trees/TemplatesTreeController.cs
#	src/Umbraco.Web.UI.Client/src/less/navs.less
#	templates/UmbracoProject/.gitignore
#	tests/Umbraco.Tests.Integration/Umbraco.Core/Telemetry/TelemetryServiceTests.cs
#	tests/Umbraco.Tests.UnitTests/Umbraco.Core/Services/UserDataServiceTests.cs
#	tests/Umbraco.Tests.UnitTests/Umbraco.Core/Telemetry/SystemInformationTelemetryProviderTests.cs
This commit is contained in:
Sebastiaan Janssen
2022-07-11 14:07:08 +02:00
3849 changed files with 312969 additions and 300428 deletions

View File

@@ -1,19 +1,16 @@
using System;
namespace Umbraco.Cms.Core.DependencyInjection;
namespace Umbraco.Cms.Core.DependencyInjection
/// <summary>
/// Provides access to a request scoped service provider when available for cases where
/// IHttpContextAccessor is not available. e.g. No reference to AspNetCore.Http in core.
/// </summary>
public interface IScopedServiceProvider
{
/// <summary>
/// Provides access to a request scoped service provider when available for cases where
/// IHttpContextAccessor is not available. e.g. No reference to AspNetCore.Http in core.
/// Gets a request scoped service provider when available.
/// </summary>
public interface IScopedServiceProvider
{
/// <summary>
/// Gets a request scoped service provider when available.
/// </summary>
/// <remarks>
/// Can be null.
/// </remarks>
IServiceProvider? ServiceProvider { get; }
}
/// <remarks>
/// Can be null.
/// </remarks>
IServiceProvider? ServiceProvider { get; }
}

View File

@@ -1,4 +1,3 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -7,33 +6,38 @@ using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Logging;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
public interface IUmbracoBuilder
{
public interface IUmbracoBuilder
{
IServiceCollection Services { get; }
IConfiguration Config { get; }
TypeLoader TypeLoader { get; }
IServiceCollection Services { get; }
/// <summary>
/// A Logger factory created specifically for the <see cref="IUmbracoBuilder"/>. This is NOT the same
/// instance that will be resolved from DI. Use only if required during configuration.
/// </summary>
ILoggerFactory BuilderLoggerFactory { get; }
IConfiguration Config { get; }
/// <summary>
/// A hosting environment created specifically for the <see cref="IUmbracoBuilder"/>. This is NOT the same
/// instance that will be resolved from DI. Use only if required during configuration.
/// </summary>
/// <remarks>
/// This may be null.
/// </remarks>
[Obsolete("This property will be removed in a future version, please find an alternative approach.")]
IHostingEnvironment? BuilderHostingEnvironment { get; }
TypeLoader TypeLoader { get; }
IProfiler Profiler { get; }
AppCaches AppCaches { get; }
TBuilder? WithCollectionBuilder<TBuilder>() where TBuilder : ICollectionBuilder;
void Build();
}
/// <summary>
/// A Logger factory created specifically for the <see cref="IUmbracoBuilder" />. This is NOT the same
/// instance that will be resolved from DI. Use only if required during configuration.
/// </summary>
ILoggerFactory BuilderLoggerFactory { get; }
/// <summary>
/// A hosting environment created specifically for the <see cref="IUmbracoBuilder" />. This is NOT the same
/// instance that will be resolved from DI. Use only if required during configuration.
/// </summary>
/// <remarks>
/// This may be null.
/// </remarks>
[Obsolete("This property will be removed in a future version, please find an alternative approach.")]
IHostingEnvironment? BuilderHostingEnvironment { get; }
IProfiler Profiler { get; }
AppCaches AppCaches { get; }
TBuilder WithCollectionBuilder<TBuilder>()
where TBuilder : ICollectionBuilder;
void Build();
}

View File

@@ -1,138 +1,141 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Umbraco.Cms.Core.Composing;
namespace Umbraco.Extensions
namespace Umbraco.Extensions;
public static class ServiceCollectionExtensions
{
public static class ServiceCollectionExtensions
/// <summary>
/// Adds a service of type <typeparamref name="TService" /> with an implementation type of
/// <typeparamref name="TImplementing" /> to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService" />.
/// </remarks>
public static void AddUnique<TService, TImplementing>(
this IServiceCollection services)
where TService : class
where TImplementing : class, TService =>
AddUnique<TService, TImplementing>(services, ServiceLifetime.Singleton);
/// <summary>
/// Adds a service of type <typeparamref name="TService" /> with an implementation type of
/// <typeparamref name="TImplementing" /> to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService" />.
/// </remarks>
public static void AddUnique<TService, TImplementing>(
this IServiceCollection services,
ServiceLifetime lifetime)
where TService : class
where TImplementing : class, TService
{
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique<TService, TImplementing>(
this IServiceCollection services)
where TService : class
where TImplementing : class, TService
{
AddUnique<TService, TImplementing>(services, ServiceLifetime.Singleton);
}
services.RemoveAll<TService>();
services.Add(ServiceDescriptor.Describe(typeof(TService), typeof(TImplementing), lifetime));
}
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique<TService, TImplementing>(
this IServiceCollection services,
ServiceLifetime lifetime)
where TService : class
where TImplementing : class, TService
{
services.RemoveAll<TService>();
services.Add(ServiceDescriptor.Describe(typeof(TService), typeof(TImplementing), lifetime));
}
/// <summary>
/// Adds services of types <typeparamref name="TService1" /> &amp; <typeparamref name="TService2" /> with a shared
/// implementation type of <typeparamref name="TImplementing" /> to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the types <typeparamref name="TService1" /> &amp;
/// <typeparamref name="TService2" />.
/// </remarks>
public static void AddMultipleUnique<TService1, TService2, TImplementing>(
this IServiceCollection services)
where TService1 : class
where TService2 : class
where TImplementing : class, TService1, TService2
=> services.AddMultipleUnique<TService1, TService2, TImplementing>(ServiceLifetime.Singleton);
/// <summary>
/// Adds services of types <typeparamref name="TService1"/> &amp; <typeparamref name="TService2"/> with a shared implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the types <typeparamref name="TService1"/> &amp; <typeparamref name="TService2"/>.
/// </remarks>
public static void AddMultipleUnique<TService1, TService2, TImplementing>(
this IServiceCollection services)
where TService1 : class
where TService2 : class
where TImplementing : class, TService1, TService2
=> services.AddMultipleUnique<TService1, TService2, TImplementing>(ServiceLifetime.Singleton);
/// <summary>
/// Adds services of types <typeparamref name="TService1"/> &amp; <typeparamref name="TService2"/> with a shared implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the types <typeparamref name="TService1"/> &amp; <typeparamref name="TService2"/>.
/// </remarks>
public static void AddMultipleUnique<TService1, TService2, TImplementing>(
this IServiceCollection services,
ServiceLifetime lifetime)
where TService1 : class
where TService2 : class
where TImplementing : class, TService1, TService2
{
services.AddUnique<TService1, TImplementing>(lifetime);
services.AddUnique<TService2>(factory => (TImplementing)factory.GetRequiredService<TService1>(), lifetime);
}
/// <summary>
/// Adds services of types <typeparamref name="TService1"/> &amp; <typeparamref name="TService2"/> with a shared implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the types <typeparamref name="TService1"/> &amp; <typeparamref name="TService2"/>.
/// </remarks>
public static void AddMultipleUnique<TService1, TService2, TImplementing>(
this IServiceCollection services,
ServiceLifetime lifetime)
where TService1 : class
where TService2 : class
where TImplementing : class, TService1, TService2
{
services.AddUnique<TService1, TImplementing>(lifetime);
services.AddUnique<TService2>(factory => (TImplementing)factory.GetRequiredService<TService1>(), lifetime);
}
// TODO(V11): Remove this function.
[Obsolete("This method is functionally equivalent to AddSingleton<TImplementing>() please use that instead.")]
public static void AddUnique<TImplementing>(this IServiceCollection services)
where TImplementing : class
{
services.RemoveAll<TImplementing>();
services.AddSingleton<TImplementing>();
}
// TODO(V11): Remove this function.
[Obsolete("This method is functionally equivalent to AddSingleton<TImplementing>() please use that instead.")]
public static void AddUnique<TImplementing>(this IServiceCollection services)
where TImplementing : class
{
services.RemoveAll<TImplementing>();
services.AddSingleton<TImplementing>();
}
/// <summary>
/// Adds a service of type <typeparamref name="TService" /> with an implementation factory method to the specified
/// <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService" />.
/// </remarks>
public static void AddUnique<TService>(
this IServiceCollection services,
Func<IServiceProvider, TService> factory)
where TService : class
=> services.AddUnique(factory, ServiceLifetime.Singleton);
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation factory method to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique<TService>(
this IServiceCollection services,
Func<IServiceProvider, TService> factory)
where TService : class
=> services.AddUnique(factory, ServiceLifetime.Singleton);
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation factory method to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique<TService>(
this IServiceCollection services,
Func<IServiceProvider, TService> factory,
ServiceLifetime lifetime)
where TService : class
{
services.RemoveAll<TService>();
services.Add(ServiceDescriptor.Describe(typeof(TService), factory, lifetime));
}
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation factory method to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique<TService>(
this IServiceCollection services,
Func<IServiceProvider, TService> factory,
ServiceLifetime lifetime)
where TService : class
{
services.RemoveAll<TService>();
services.Add(ServiceDescriptor.Describe(typeof(TService), factory, lifetime));
}
/// <summary>
/// Adds a singleton service of the type specified by <paramref name="serviceType" /> to the specified
/// <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type specified by <paramref name="serviceType" />.
/// </remarks>
public static void AddUnique(this IServiceCollection services, Type serviceType, object instance)
{
services.RemoveAll(serviceType);
services.AddSingleton(serviceType, instance);
}
/// <summary>
/// Adds a singleton service of the type specified by <paramref name="serviceType"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type specified by <paramref name="serviceType"/>.
/// </remarks>
public static void AddUnique(this IServiceCollection services, Type serviceType, object instance)
{
services.RemoveAll(serviceType);
services.AddSingleton(serviceType, instance);
}
/// <summary>
/// Adds a singleton service of type <typeparamref name="TService" /> to the specified
/// <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type type <typeparamref name="TService" />.
/// </remarks>
public static void AddUnique<TService>(this IServiceCollection services, TService instance)
where TService : class
{
services.RemoveAll<TService>();
services.AddSingleton(instance);
}
/// <summary>
/// Adds a singleton service of type <typeparamref name="TService"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique<TService>(this IServiceCollection services, TService instance)
where TService : class
{
services.RemoveAll<TService>();
services.AddSingleton(instance);
}
internal static IServiceCollection AddLazySupport(this IServiceCollection services)
{
services.Replace(ServiceDescriptor.Transient(typeof(Lazy<>), typeof(LazyResolve<>)));
return services;
}
internal static IServiceCollection AddLazySupport(this IServiceCollection services)
{
services.Replace(ServiceDescriptor.Transient(typeof(Lazy<>), typeof(LazyResolve<>)));
return services;
}
}

View File

@@ -1,57 +1,55 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Models.PublishedContent;
namespace Umbraco.Extensions
namespace Umbraco.Extensions;
/// <summary>
/// Provides extension methods to the <see cref="IFactory" /> class.
/// </summary>
public static class ServiceProviderExtensions
{
/// <summary>
/// Provides extension methods to the <see cref="IFactory"/> class.
/// Creates an instance with arguments.
/// </summary>
public static class ServiceProviderExtensions
/// <typeparam name="T">The type of the instance.</typeparam>
/// <param name="serviceProvider">The factory.</param>
/// <param name="args">Arguments.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>
/// <para>Throws an exception if the factory failed to get an instance of the specified type.</para>
/// <para>The arguments are used as dependencies by the factory.</para>
/// </remarks>
public static T CreateInstance<T>(this IServiceProvider serviceProvider, params object[] args)
where T : class
=> (T)serviceProvider.CreateInstance(typeof(T), args);
/// <summary>
/// Creates an instance of a service, with arguments.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider" /></param>
/// <param name="type">The type of the instance.</param>
/// <param name="args">Named arguments.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>
/// <para>The instance type does not need to be registered into the factory.</para>
/// <para>
/// The arguments are used as dependencies by the factory. Other dependencies
/// are retrieved from the factory.
/// </para>
/// </remarks>
public static object CreateInstance(this IServiceProvider serviceProvider, Type type, params object[] args)
=> ActivatorUtilities.CreateInstance(serviceProvider, type, args);
[EditorBrowsable(EditorBrowsableState.Never)]
public static PublishedModelFactory CreateDefaultPublishedModelFactory(this IServiceProvider factory)
{
/// <summary>
/// Creates an instance with arguments.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <param name="serviceProvider">The factory.</param>
/// <param name="args">Arguments.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>
/// <para>Throws an exception if the factory failed to get an instance of the specified type.</para>
/// <para>The arguments are used as dependencies by the factory.</para>
/// </remarks>
public static T CreateInstance<T>(this IServiceProvider serviceProvider, params object[] args)
where T : class
=> (T)serviceProvider.CreateInstance(typeof(T), args);
/// <summary>
/// Creates an instance of a service, with arguments.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/></param>
/// <param name="type">The type of the instance.</param>
/// <param name="args">Named arguments.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>
/// <para>The instance type does not need to be registered into the factory.</para>
/// <para>The arguments are used as dependencies by the factory. Other dependencies
/// are retrieved from the factory.</para>
/// </remarks>
public static object CreateInstance(this IServiceProvider serviceProvider, Type type, params object[] args)
=> ActivatorUtilities.CreateInstance(serviceProvider, type, args);
[EditorBrowsable(EditorBrowsableState.Never)]
public static PublishedModelFactory CreateDefaultPublishedModelFactory(this IServiceProvider factory)
{
TypeLoader typeLoader = factory.GetRequiredService<TypeLoader>();
IPublishedValueFallback publishedValueFallback = factory.GetRequiredService<IPublishedValueFallback>();
IEnumerable<Type> types = typeLoader
.GetTypes<PublishedElementModel>() // element models
.Concat(typeLoader.GetTypes<PublishedContentModel>()); // content models
return new PublishedModelFactory(types, publishedValueFallback);
}
TypeLoader typeLoader = factory.GetRequiredService<TypeLoader>();
IPublishedValueFallback publishedValueFallback = factory.GetRequiredService<IPublishedValueFallback>();
IEnumerable<Type> types = typeLoader
.GetTypes<PublishedElementModel>() // element models
.Concat(typeLoader.GetTypes<PublishedContentModel>()); // content models
return new PublishedModelFactory(types, publishedValueFallback);
}
}

View File

@@ -1,25 +1,25 @@
using System;
using System.ComponentModel;
namespace Umbraco.Cms.Web.Common.DependencyInjection
namespace Umbraco.Cms.Web.Common.DependencyInjection;
/// <summary>
/// Service locator for internal (umbraco cms) only purposes. Should only be used if no other ways exist.
/// </summary>
/// <remarks>
/// It is created with only two goals in mind
/// 1) Continue to have the same extension methods on IPublishedContent and IPublishedElement as in V8. To make
/// migration easier.
/// 2) To have a tool to avoid breaking changes in minor and patch versions. All methods using this should in theory be
/// obsolete.
/// Keep in mind, every time this is used, the code becomes basically untestable.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class StaticServiceProvider
{
/// <summary>
/// Service locator for internal (umbraco cms) only purposes. Should only be used if no other ways exist.
/// The service locator.
/// </summary>
/// <remarks>
/// It is created with only two goals in mind
/// 1) Continue to have the same extension methods on IPublishedContent and IPublishedElement as in V8. To make migration easier.
/// 2) To have a tool to avoid breaking changes in minor and patch versions. All methods using this should in theory be obsolete.
///
/// Keep in mind, every time this is used, the code becomes basically untestable.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class StaticServiceProvider
{
/// <summary>
/// The service locator.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceProvider Instance { get; set; } = null!; // This is set doing startup and will always exists after that
}
public static IServiceProvider Instance { get; set; } =
null!; // This is set doing startup and will always exists after that
}

View File

@@ -1,4 +1,3 @@
using System;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Dashboards;
using Umbraco.Cms.Core.Media;
@@ -6,111 +5,110 @@ using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Sections;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
/// <summary>
/// Contains extensions methods for <see cref="IUmbracoBuilder" /> used for registering content apps.
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Contains extensions methods for <see cref="IUmbracoBuilder"/> used for registering content apps.
/// Register a component.
/// </summary>
public static partial class UmbracoBuilderExtensions
/// <typeparam name="T">The type of the component.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddComponent<T>(this IUmbracoBuilder builder)
where T : class, IComponent
{
/// <summary>
/// Register a component.
/// </summary>
/// <typeparam name="T">The type of the component.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddComponent<T>(this IUmbracoBuilder builder)
where T : class, IComponent
{
builder.Components()?.Append<T>();
return builder;
}
builder.Components().Append<T>();
return builder;
}
/// <summary>
/// Register a content app.
/// </summary>
/// <typeparam name="T">The type of the content app.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddContentApp<T>(this IUmbracoBuilder builder)
where T : class, IContentAppFactory
{
builder.ContentApps()?.Append<T>();
return builder;
}
/// <summary>
/// Register a content app.
/// </summary>
/// <typeparam name="T">The type of the content app.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddContentApp<T>(this IUmbracoBuilder builder)
where T : class, IContentAppFactory
{
builder.ContentApps().Append<T>();
return builder;
}
/// <summary>
/// Register a content finder.
/// </summary>
/// <typeparam name="T">The type of the content finder.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddContentFinder<T>(this IUmbracoBuilder builder)
where T : class, IContentFinder
{
builder.ContentFinders()?.Append<T>();
return builder;
}
/// <summary>
/// Register a content finder.
/// </summary>
/// <typeparam name="T">The type of the content finder.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddContentFinder<T>(this IUmbracoBuilder builder)
where T : class, IContentFinder
{
builder.ContentFinders().Append<T>();
return builder;
}
/// <summary>
/// Register a dashboard.
/// </summary>
/// <typeparam name="T">The type of the dashboard.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddDashboard<T>(this IUmbracoBuilder builder)
where T : class, IDashboard
{
builder.Dashboards()?.Add<T>();
return builder;
}
/// <summary>
/// Register a dashboard.
/// </summary>
/// <typeparam name="T">The type of the dashboard.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddDashboard<T>(this IUmbracoBuilder builder)
where T : class, IDashboard
{
builder.Dashboards().Add<T>();
return builder;
}
/// <summary>
/// Register a media url provider.
/// </summary>
/// <typeparam name="T">The type of the media url provider.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddMediaUrlProvider<T>(this IUmbracoBuilder builder)
where T : class, IMediaUrlProvider
{
builder.MediaUrlProviders()?.Append<T>();
return builder;
}
/// <summary>
/// Register a media url provider.
/// </summary>
/// <typeparam name="T">The type of the media url provider.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddMediaUrlProvider<T>(this IUmbracoBuilder builder)
where T : class, IMediaUrlProvider
{
builder.MediaUrlProviders().Append<T>();
return builder;
}
/// <summary>
/// Register a embed provider.
/// </summary>
/// <typeparam name="T">The type of the embed provider.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddEmbedProvider<T>(this IUmbracoBuilder builder)
where T : class, IEmbedProvider
{
builder.EmbedProviders()?.Append<T>();
return builder;
}
/// <summary>
/// Register a embed provider.
/// </summary>
/// <typeparam name="T">The type of the embed provider.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddEmbedProvider<T>(this IUmbracoBuilder builder)
where T : class, IEmbedProvider
{
builder.EmbedProviders().Append<T>();
return builder;
}
[Obsolete("Use AddEmbedProvider instead. This will be removed in Umbraco 11")]
public static IUmbracoBuilder AddOEmbedProvider<T>(this IUmbracoBuilder builder)
where T : class, IEmbedProvider => AddEmbedProvider<T>(builder);
[Obsolete("Use AddEmbedProvider instead. This will be removed in Umbraco 11")]
public static IUmbracoBuilder AddOEmbedProvider<T>(this IUmbracoBuilder builder)
where T : class, IEmbedProvider => AddEmbedProvider<T>(builder);
/// <summary>
/// Register a section.
/// </summary>
/// <typeparam name="T">The type of the section.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddSection<T>(this IUmbracoBuilder builder)
where T : class, ISection
{
builder.Sections()?.Append<T>();
return builder;
}
/// <summary>
/// Register a section.
/// </summary>
/// <typeparam name="T">The type of the section.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddSection<T>(this IUmbracoBuilder builder)
where T : class, ISection
{
builder.Sections().Append<T>();
return builder;
}
/// <summary>
/// Register a url provider.
/// </summary>
/// <typeparam name="T">The type of the url provider.</typeparam>
/// <param name="builder">The Builder.</param>
public static IUmbracoBuilder AddUrlProvider<T>(this IUmbracoBuilder builder)
where T : class, IUrlProvider
{
builder.UrlProviders()?.Append<T>();
return builder;
}
/// <summary>
/// Register a url provider.
/// </summary>
/// <typeparam name="T">The type of the url provider.</typeparam>
/// <param name="builder">The Builder.</param>
public static IUmbracoBuilder AddUrlProvider<T>(this IUmbracoBuilder builder)
where T : class, IUrlProvider
{
builder.UrlProviders().Append<T>();
return builder;
}
}

View File

@@ -20,291 +20,282 @@ using Umbraco.Cms.Core.Trees;
using Umbraco.Cms.Core.WebAssets;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder"/>
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder"/>
/// Adds all core collection builders
/// </summary>
public static partial class UmbracoBuilderExtensions
internal static void AddAllCoreCollectionBuilders(this IUmbracoBuilder builder)
{
/// <summary>
/// Adds all core collection builders
/// </summary>
internal static void AddAllCoreCollectionBuilders(this IUmbracoBuilder builder)
{
builder.CacheRefreshers()?.Add(() => builder.TypeLoader.GetCacheRefreshers());
builder.DataEditors()?.Add(() => builder.TypeLoader.GetDataEditors());
builder.Actions()?.Add(() => builder .TypeLoader.GetActions());
builder.CacheRefreshers().Add(() => builder.TypeLoader.GetCacheRefreshers());
builder.DataEditors().Add(() => builder.TypeLoader.GetDataEditors());
builder.Actions().Add(() => builder .TypeLoader.GetActions());
// register known content apps
builder.ContentApps()?
.Append<ListViewContentAppFactory>()
.Append<ContentEditorContentAppFactory>()
.Append<ContentInfoContentAppFactory>()
.Append<ContentTypeDesignContentAppFactory>()
.Append<ContentTypeListViewContentAppFactory>()
.Append<ContentTypePermissionsContentAppFactory>()
.Append<ContentTypeTemplatesContentAppFactory>()
.Append<MemberEditorContentAppFactory>()
.Append<DictionaryContentAppFactory>();
// register known content apps
builder.ContentApps()
.Append<ListViewContentAppFactory>()
.Append<ContentEditorContentAppFactory>()
.Append<ContentInfoContentAppFactory>()
.Append<ContentTypeDesignContentAppFactory>()
.Append<ContentTypeListViewContentAppFactory>()
.Append<ContentTypePermissionsContentAppFactory>()
.Append<ContentTypeTemplatesContentAppFactory>()
.Append<MemberEditorContentAppFactory>()
.Append<DictionaryContentAppFactory>();
// all built-in finders in the correct order,
// devs can then modify this list on application startup
builder.ContentFinders()?
.Append<ContentFinderByPageIdQuery>()
.Append<ContentFinderByUrl>()
.Append<ContentFinderByIdPath>()
/*.Append<ContentFinderByUrlAndTemplate>() // disabled, this is an odd finder */
.Append<ContentFinderByUrlAlias>()
.Append<ContentFinderByRedirectUrl>();
builder.EditorValidators()?.Add(() => builder.TypeLoader.GetTypes<IEditorValidator>());
builder.HealthChecks()?.Add(() => builder.TypeLoader.GetTypes<HealthCheck>());
builder.HealthCheckNotificationMethods()?.Add(() => builder.TypeLoader.GetTypes<IHealthCheckNotificationMethod>());
builder.TourFilters();
builder.UrlProviders()?
.Append<AliasUrlProvider>()
.Append<DefaultUrlProvider>();
builder.MediaUrlProviders()?
.Append<DefaultMediaUrlProvider>();
// register back office sections in the order we want them rendered
builder.Sections()?
.Append<ContentSection>()
.Append<MediaSection>()
.Append<SettingsSection>()
.Append<PackagesSection>()
.Append<UsersSection>()
.Append<MembersSection>()
.Append<FormsSection>()
.Append<TranslationSection>();
builder.Components();
// register core CMS dashboards and 3rd party types - will be ordered by weight attribute & merged with package.manifest dashboards
builder.Dashboards()?
.Add<ContentDashboard>()
.Add<ExamineDashboard>()
.Add<FormsDashboard>()
.Add<HealthCheckDashboard>()
.Add<ManifestDashboard>()
.Add<MediaDashboard>()
.Add<MembersDashboard>()
.Add<ProfilerDashboard>()
.Add<PublishedStatusDashboard>()
.Add<RedirectUrlDashboard>()
.Add<SettingsDashboard>()
.Add(builder.TypeLoader.GetTypes<IDashboard>());
builder.PartialViewSnippets();
builder.PartialViewMacroSnippets();
builder.DataValueReferenceFactories();
builder.PropertyValueConverters()?.Append(builder.TypeLoader.GetTypes<IPropertyValueConverter>());
builder.UrlSegmentProviders()?.Append<DefaultUrlSegmentProvider>();
builder.ManifestValueValidators()?
.Add<RequiredValidator>()
.Add<RegexValidator>()
.Add<DelimitedValueValidator>()
.Add<EmailValidator>()
.Add<IntegerValidator>()
.Add<DecimalValidator>();
builder.ManifestFilters();
builder.MediaUrlGenerators();
// register OEmbed providers - no type scanning - all explicit opt-in of adding types, IEmbedProvider is not IDiscoverable
builder.EmbedProviders()?
.Append<YouTube>()
.Append<Twitter>()
.Append<Vimeo>()
.Append<DailyMotion>()
.Append<Flickr>()
.Append<Slideshare>()
.Append<Kickstarter>()
.Append<GettyImages>()
.Append<Ted>()
.Append<Soundcloud>()
.Append<Issuu>()
.Append<Hulu>()
.Append<Giphy>()
.Append<LottieFiles>();
builder.SearchableTrees()?.Add(() => builder.TypeLoader.GetTypes<ISearchableTree>());
builder.BackOfficeAssets();
}
/// <summary>
/// Gets the actions collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ActionCollectionBuilder? Actions(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ActionCollectionBuilder>();
/// <summary>
/// Gets the content apps collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ContentAppFactoryCollectionBuilder? ContentApps(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ContentAppFactoryCollectionBuilder>();
/// <summary>
/// Gets the content finders collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ContentFinderCollectionBuilder? ContentFinders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ContentFinderCollectionBuilder>();
/// <summary>
/// Gets the editor validators collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static EditorValidatorCollectionBuilder? EditorValidators(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<EditorValidatorCollectionBuilder>();
/// <summary>
/// Gets the health checks collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static HealthCheckCollectionBuilder? HealthChecks(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<HealthCheckCollectionBuilder>();
public static HealthCheckNotificationMethodCollectionBuilder? HealthCheckNotificationMethods(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<HealthCheckNotificationMethodCollectionBuilder>();
/// <summary>
/// Gets the TourFilters collection builder.
/// </summary>
public static TourFilterCollectionBuilder? TourFilters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<TourFilterCollectionBuilder>();
/// <summary>
/// Gets the URL providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static UrlProviderCollectionBuilder? UrlProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<UrlProviderCollectionBuilder>();
/// <summary>
/// Gets the media url providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static MediaUrlProviderCollectionBuilder? MediaUrlProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<MediaUrlProviderCollectionBuilder>();
/// <summary>
/// Gets the backoffice sections/applications collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static SectionCollectionBuilder? Sections(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<SectionCollectionBuilder>();
/// <summary>
/// Gets the components collection builder.
/// </summary>
public static ComponentCollectionBuilder? Components(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ComponentCollectionBuilder>();
/// <summary>
/// Gets the backoffice dashboards collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static DashboardCollectionBuilder? Dashboards(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DashboardCollectionBuilder>();
/// <summary>
/// Gets the partial view snippets collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static PartialViewSnippetCollectionBuilder? PartialViewSnippets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PartialViewSnippetCollectionBuilder>();
/// <summary>
/// Gets the partial view macro snippets collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static PartialViewMacroSnippetCollectionBuilder? PartialViewMacroSnippets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PartialViewMacroSnippetCollectionBuilder>();
/// <summary>
/// Gets the cache refreshers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static CacheRefresherCollectionBuilder? CacheRefreshers(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<CacheRefresherCollectionBuilder>();
/// <summary>
/// Gets the map definitions collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static MapDefinitionCollectionBuilder? MapDefinitions(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>();
/// <summary>
/// Gets the data editor collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static DataEditorCollectionBuilder? DataEditors(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DataEditorCollectionBuilder>();
/// <summary>
/// Gets the data value reference factory collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static DataValueReferenceFactoryCollectionBuilder? DataValueReferenceFactories(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DataValueReferenceFactoryCollectionBuilder>();
/// <summary>
/// Gets the property value converters collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static PropertyValueConverterCollectionBuilder? PropertyValueConverters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>();
/// <summary>
/// Gets the url segment providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static UrlSegmentProviderCollectionBuilder? UrlSegmentProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<UrlSegmentProviderCollectionBuilder>();
/// <summary>
/// Gets the validators collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
internal static ManifestValueValidatorCollectionBuilder? ManifestValueValidators(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ManifestValueValidatorCollectionBuilder>();
/// <summary>
/// Gets the manifest filter collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ManifestFilterCollectionBuilder? ManifestFilters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ManifestFilterCollectionBuilder>();
/// <summary>
/// Gets the content finders collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static MediaUrlGeneratorCollectionBuilder? MediaUrlGenerators(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<MediaUrlGeneratorCollectionBuilder>();
/// <summary>
/// Gets the backoffice OEmbed Providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
[Obsolete("Use EmbedProviders() instead")]
public static EmbedProvidersCollectionBuilder? OEmbedProviders(this IUmbracoBuilder builder)
=> EmbedProviders(builder);
/// <summary>
/// Gets the backoffice Embed Providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static EmbedProvidersCollectionBuilder? EmbedProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<EmbedProvidersCollectionBuilder>();
/// <summary>
/// Gets the back office searchable tree collection builder
/// </summary>
public static SearchableTreeCollectionBuilder? SearchableTrees(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<SearchableTreeCollectionBuilder>();
/// <summary>
/// Gets the back office custom assets collection builder
/// </summary>
public static CustomBackOfficeAssetsCollectionBuilder? BackOfficeAssets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<CustomBackOfficeAssetsCollectionBuilder>();
// all built-in finders in the correct order,
// devs can then modify this list on application startup
builder.ContentFinders()
.Append<ContentFinderByPageIdQuery>()
.Append<ContentFinderByUrl>()
.Append<ContentFinderByIdPath>()
/*.Append<ContentFinderByUrlAndTemplate>() // disabled, this is an odd finder */
.Append<ContentFinderByUrlAlias>()
.Append<ContentFinderByRedirectUrl>();
builder.EditorValidators().Add(() => builder.TypeLoader.GetTypes<IEditorValidator>());
builder.HealthChecks().Add(() => builder.TypeLoader.GetTypes<HealthCheck>());
builder.HealthCheckNotificationMethods().Add(() => builder.TypeLoader.GetTypes<IHealthCheckNotificationMethod>());
builder.TourFilters();
builder.UrlProviders()
.Append<AliasUrlProvider>()
.Append<DefaultUrlProvider>();
builder.MediaUrlProviders()
.Append<DefaultMediaUrlProvider>();
// register back office sections in the order we want them rendered
builder.Sections()
.Append<ContentSection>()
.Append<MediaSection>()
.Append<SettingsSection>()
.Append<PackagesSection>()
.Append<UsersSection>()
.Append<MembersSection>()
.Append<FormsSection>()
.Append<TranslationSection>();
builder.Components();
// register core CMS dashboards and 3rd party types - will be ordered by weight attribute & merged with package.manifest dashboards
builder.Dashboards()
.Add<ContentDashboard>()
.Add<ExamineDashboard>()
.Add<FormsDashboard>()
.Add<HealthCheckDashboard>()
.Add<ManifestDashboard>()
.Add<MediaDashboard>()
.Add<MembersDashboard>()
.Add<ProfilerDashboard>()
.Add<PublishedStatusDashboard>()
.Add<RedirectUrlDashboard>()
.Add<SettingsDashboard>()
.Add(builder.TypeLoader.GetTypes<IDashboard>());
builder.PartialViewSnippets();
builder.PartialViewMacroSnippets();
builder.DataValueReferenceFactories();
builder.PropertyValueConverters().Append(builder.TypeLoader.GetTypes<IPropertyValueConverter>());
builder.UrlSegmentProviders().Append<DefaultUrlSegmentProvider>();
builder.ManifestValueValidators()
.Add<RequiredValidator>()
.Add<RegexValidator>()
.Add<DelimitedValueValidator>()
.Add<EmailValidator>()
.Add<IntegerValidator>()
.Add<DecimalValidator>();
builder.ManifestFilters();
builder.MediaUrlGenerators();
// register OEmbed providers - no type scanning - all explicit opt-in of adding types, IEmbedProvider is not IDiscoverable
builder.EmbedProviders()
.Append<YouTube>()
.Append<Twitter>()
.Append<Vimeo>()
.Append<DailyMotion>()
.Append<Flickr>()
.Append<Slideshare>()
.Append<Kickstarter>()
.Append<GettyImages>()
.Append<Ted>()
.Append<Soundcloud>()
.Append<Issuu>()
.Append<Hulu>()
.Append<Giphy>()
.Append<LottieFiles>();
builder.SearchableTrees().Add(() => builder.TypeLoader.GetTypes<ISearchableTree>());
builder.BackOfficeAssets();
}
/// <summary>
/// Gets the actions collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ActionCollectionBuilder Actions(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ActionCollectionBuilder>();
/// <summary>
/// Gets the content apps collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ContentAppFactoryCollectionBuilder ContentApps(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ContentAppFactoryCollectionBuilder>();
/// <summary>
/// Gets the content finders collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ContentFinderCollectionBuilder ContentFinders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ContentFinderCollectionBuilder>();
/// <summary>
/// Gets the editor validators collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static EditorValidatorCollectionBuilder EditorValidators(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<EditorValidatorCollectionBuilder>();
/// <summary>
/// Gets the health checks collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static HealthCheckCollectionBuilder HealthChecks(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<HealthCheckCollectionBuilder>();
public static HealthCheckNotificationMethodCollectionBuilder HealthCheckNotificationMethods(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<HealthCheckNotificationMethodCollectionBuilder>();
/// <summary>
/// Gets the TourFilters collection builder.
/// </summary>
public static TourFilterCollectionBuilder TourFilters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<TourFilterCollectionBuilder>();
/// <summary>
/// Gets the URL providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static UrlProviderCollectionBuilder UrlProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<UrlProviderCollectionBuilder>();
/// <summary>
/// Gets the media url providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static MediaUrlProviderCollectionBuilder MediaUrlProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<MediaUrlProviderCollectionBuilder>();
/// <summary>
/// Gets the backoffice sections/applications collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static SectionCollectionBuilder Sections(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<SectionCollectionBuilder>();
/// <summary>
/// Gets the components collection builder.
/// </summary>
public static ComponentCollectionBuilder Components(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ComponentCollectionBuilder>();
/// <summary>
/// Gets the backoffice dashboards collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static DashboardCollectionBuilder Dashboards(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DashboardCollectionBuilder>();
/// <summary>
public static PartialViewSnippetCollectionBuilder? PartialViewSnippets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PartialViewSnippetCollectionBuilder>();
public static PartialViewMacroSnippetCollectionBuilder? PartialViewMacroSnippets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PartialViewMacroSnippetCollectionBuilder>();
/// Gets the cache refreshers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static CacheRefresherCollectionBuilder CacheRefreshers(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<CacheRefresherCollectionBuilder>();
/// <summary>
/// Gets the map definitions collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static MapDefinitionCollectionBuilder MapDefinitions(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>();
/// <summary>
/// Gets the data editor collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static DataEditorCollectionBuilder DataEditors(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DataEditorCollectionBuilder>();
/// <summary>
/// Gets the data value reference factory collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static DataValueReferenceFactoryCollectionBuilder DataValueReferenceFactories(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<DataValueReferenceFactoryCollectionBuilder>();
/// <summary>
/// Gets the property value converters collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static PropertyValueConverterCollectionBuilder PropertyValueConverters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>();
/// <summary>
/// Gets the url segment providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static UrlSegmentProviderCollectionBuilder UrlSegmentProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<UrlSegmentProviderCollectionBuilder>();
/// <summary>
/// Gets the validators collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
internal static ManifestValueValidatorCollectionBuilder ManifestValueValidators(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ManifestValueValidatorCollectionBuilder>();
/// <summary>
/// Gets the manifest filter collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ManifestFilterCollectionBuilder ManifestFilters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ManifestFilterCollectionBuilder>();
/// <summary>
/// Gets the content finders collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static MediaUrlGeneratorCollectionBuilder MediaUrlGenerators(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<MediaUrlGeneratorCollectionBuilder>();
/// <summary>
/// Gets the backoffice OEmbed Providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
[Obsolete("Use EmbedProviders() instead")]
public static EmbedProvidersCollectionBuilder OEmbedProviders(this IUmbracoBuilder builder)
=> EmbedProviders(builder);
/// <summary>
/// Gets the backoffice Embed Providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static EmbedProvidersCollectionBuilder EmbedProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<EmbedProvidersCollectionBuilder>();
/// <summary>
/// Gets the back office searchable tree collection builder
/// </summary>
public static SearchableTreeCollectionBuilder SearchableTrees(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<SearchableTreeCollectionBuilder>();
/// <summary>
/// Gets the back office custom assets collection builder
/// </summary>
public static CustomBackOfficeAssetsCollectionBuilder BackOfficeAssets(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<CustomBackOfficeAssetsCollectionBuilder>();
}

View File

@@ -1,26 +1,24 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Composing;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder" />
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder"/>
/// Adds Umbraco composers for plugins
/// </summary>
public static partial class UmbracoBuilderExtensions
public static IUmbracoBuilder AddComposers(this IUmbracoBuilder builder)
{
/// <summary>
/// Adds Umbraco composers for plugins
/// </summary>
public static IUmbracoBuilder AddComposers(this IUmbracoBuilder builder)
{
IEnumerable<Type> composerTypes = builder.TypeLoader.GetTypes<IComposer>();
IEnumerable<Attribute> enableDisable = builder.TypeLoader.GetAssemblyAttributes(typeof(EnableComposerAttribute), typeof(DisableComposerAttribute));
IEnumerable<Type> composerTypes = builder.TypeLoader.GetTypes<IComposer>();
IEnumerable<Attribute> enableDisable =
builder.TypeLoader.GetAssemblyAttributes(typeof(EnableComposerAttribute), typeof(DisableComposerAttribute));
new ComposerGraph(builder, composerTypes, enableDisable, builder.BuilderLoggerFactory.CreateLogger<ComposerGraph>()).Compose();
new ComposerGraph(builder, composerTypes, enableDisable, builder.BuilderLoggerFactory.CreateLogger<ComposerGraph>()).Compose();
return builder;
}
return builder;
}
}

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration;
@@ -9,105 +6,104 @@ using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Configuration.Models.Validation;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder" />
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder"/>
/// </summary>
public static partial class UmbracoBuilderExtensions
private static IUmbracoBuilder AddUmbracoOptions<TOptions>(this IUmbracoBuilder builder, Action<OptionsBuilder<TOptions>>? configure = null)
where TOptions : class
{
private static IUmbracoBuilder AddUmbracoOptions<TOptions>(this IUmbracoBuilder builder, Action<OptionsBuilder<TOptions>>? configure = null)
where TOptions : class
UmbracoOptionsAttribute? umbracoOptionsAttribute = typeof(TOptions).GetCustomAttribute<UmbracoOptionsAttribute>();
if (umbracoOptionsAttribute is null)
{
var umbracoOptionsAttribute = typeof(TOptions).GetCustomAttribute<UmbracoOptionsAttribute>();
if (umbracoOptionsAttribute is null)
throw new ArgumentException($"{typeof(TOptions)} do not have the UmbracoOptionsAttribute.");
}
OptionsBuilder<TOptions>? optionsBuilder = builder.Services.AddOptions<TOptions>()
.Bind(
builder.Config.GetSection(umbracoOptionsAttribute.ConfigurationKey),
o => o.BindNonPublicProperties = umbracoOptionsAttribute.BindNonPublicProperties)
.ValidateDataAnnotations();
configure?.Invoke(optionsBuilder);
return builder;
}
/// <summary>
/// Add Umbraco configuration services and options
/// </summary>
public static IUmbracoBuilder AddConfiguration(this IUmbracoBuilder builder)
{
// Register configuration validators.
builder.Services.AddSingleton<IValidateOptions<ContentSettings>, ContentSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<GlobalSettings>, GlobalSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<HealthChecksSettings>, HealthChecksSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<RequestHandlerSettings>, RequestHandlerSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<UnattendedSettings>, UnattendedSettingsValidator>();
// Register configuration sections.
builder
.AddUmbracoOptions<ModelsBuilderSettings>()
.AddUmbracoOptions<ActiveDirectorySettings>()
.AddUmbracoOptions<ContentSettings>()
.AddUmbracoOptions<CoreDebugSettings>()
.AddUmbracoOptions<ExceptionFilterSettings>()
.AddUmbracoOptions<GlobalSettings>(optionsBuilder => optionsBuilder.PostConfigure(options =>
{
throw new ArgumentException($"{typeof(TOptions)} do not have the UmbracoOptionsAttribute.");
}
var optionsBuilder = builder.Services.AddOptions<TOptions>()
.Bind(
builder.Config.GetSection(umbracoOptionsAttribute.ConfigurationKey),
o => o.BindNonPublicProperties = umbracoOptionsAttribute.BindNonPublicProperties
)
.ValidateDataAnnotations();
configure?.Invoke(optionsBuilder);
return builder;
}
/// <summary>
/// Add Umbraco configuration services and options
/// </summary>
public static IUmbracoBuilder AddConfiguration(this IUmbracoBuilder builder)
{
// Register configuration validators.
builder.Services.AddSingleton<IValidateOptions<ContentSettings>, ContentSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<GlobalSettings>, GlobalSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<HealthChecksSettings>, HealthChecksSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<RequestHandlerSettings>, RequestHandlerSettingsValidator>();
builder.Services.AddSingleton<IValidateOptions<UnattendedSettings>, UnattendedSettingsValidator>();
// Register configuration sections.
builder
.AddUmbracoOptions<ModelsBuilderSettings>()
.AddUmbracoOptions<ConnectionStrings>()
.AddUmbracoOptions<ActiveDirectorySettings>()
.AddUmbracoOptions<ContentSettings>()
.AddUmbracoOptions<CoreDebugSettings>()
.AddUmbracoOptions<ExceptionFilterSettings>()
.AddUmbracoOptions<GlobalSettings>(optionsBuilder => optionsBuilder.PostConfigure(options =>
if (string.IsNullOrEmpty(options.UmbracoMediaPhysicalRootPath))
{
if (string.IsNullOrEmpty(options.UmbracoMediaPhysicalRootPath))
{
options.UmbracoMediaPhysicalRootPath = options.UmbracoMediaPath;
}
}))
.AddUmbracoOptions<HealthChecksSettings>()
.AddUmbracoOptions<HostingSettings>()
.AddUmbracoOptions<ImagingSettings>()
.AddUmbracoOptions<IndexCreatorSettings>()
.AddUmbracoOptions<KeepAliveSettings>()
.AddUmbracoOptions<LoggingSettings>()
.AddUmbracoOptions<MemberPasswordConfigurationSettings>()
.AddUmbracoOptions<NuCacheSettings>()
.AddUmbracoOptions<RequestHandlerSettings>()
.AddUmbracoOptions<RuntimeSettings>()
.AddUmbracoOptions<SecuritySettings>()
.AddUmbracoOptions<TourSettings>()
.AddUmbracoOptions<TypeFinderSettings>()
.AddUmbracoOptions<UserPasswordConfigurationSettings>()
.AddUmbracoOptions<WebRoutingSettings>()
.AddUmbracoOptions<UmbracoPluginSettings>()
.AddUmbracoOptions<UnattendedSettings>()
.AddUmbracoOptions<RichTextEditorSettings>()
.AddUmbracoOptions<BasicAuthSettings>()
.AddUmbracoOptions<RuntimeMinificationSettings>()
.AddUmbracoOptions<LegacyPasswordMigrationSettings>()
.AddUmbracoOptions<PackageMigrationSettings>()
.AddUmbracoOptions<ContentDashboardSettings>()
.AddUmbracoOptions<HelpPageSettings>();
options.UmbracoMediaPhysicalRootPath = options.UmbracoMediaPath;
}
}))
.AddUmbracoOptions<HealthChecksSettings>()
.AddUmbracoOptions<HostingSettings>()
.AddUmbracoOptions<ImagingSettings>()
.AddUmbracoOptions<IndexCreatorSettings>()
.AddUmbracoOptions<KeepAliveSettings>()
.AddUmbracoOptions<LoggingSettings>()
.AddUmbracoOptions<MemberPasswordConfigurationSettings>()
.AddUmbracoOptions<NuCacheSettings>()
.AddUmbracoOptions<RequestHandlerSettings>()
.AddUmbracoOptions<RuntimeSettings>()
.AddUmbracoOptions<SecuritySettings>()
.AddUmbracoOptions<TourSettings>()
.AddUmbracoOptions<TypeFinderSettings>()
.AddUmbracoOptions<UserPasswordConfigurationSettings>()
.AddUmbracoOptions<WebRoutingSettings>()
.AddUmbracoOptions<UmbracoPluginSettings>()
.AddUmbracoOptions<UnattendedSettings>()
.AddUmbracoOptions<RichTextEditorSettings>()
.AddUmbracoOptions<BasicAuthSettings>()
.AddUmbracoOptions<RuntimeMinificationSettings>()
.AddUmbracoOptions<LegacyPasswordMigrationSettings>()
.AddUmbracoOptions<PackageMigrationSettings>()
.AddUmbracoOptions<ContentDashboardSettings>()
.AddUmbracoOptions<HelpPageSettings>()
.AddUmbracoOptions<DataTypesSettings>();
builder.Services.AddSingleton<IConfigureOptions<ConnectionStrings>, ConfigureConnectionStrings>();
// Configure connection string and ensure it's updated when the configuration changes
builder.Services.AddSingleton<IConfigureOptions<ConnectionStrings>, ConfigureConnectionStrings>();
builder.Services.AddSingleton<IOptionsChangeTokenSource<ConnectionStrings>, ConfigurationChangeTokenSource<ConnectionStrings>>();
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.Languages,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.Languages}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.DataTypes,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.DataTypes}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.MediaTypes,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.MediaTypes}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.MemberTypes,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.MemberTypes}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.Languages,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.Languages}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.DataTypes,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.DataTypes}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.MediaTypes,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.MediaTypes}"));
builder.Services.Configure<InstallDefaultDataSettings>(
Constants.Configuration.NamedOptions.InstallDefaultData.MemberTypes,
builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.MemberTypes}"));
builder.Services.Configure<RequestHandlerSettings>(options => options.MergeReplacements(builder.Config));
builder.Services.Configure<RequestHandlerSettings>(options => options.MergeReplacements(builder.Config));
return builder;
}
return builder;
}
}

View File

@@ -5,72 +5,80 @@ using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
/// <summary>
/// Contains extensions methods for <see cref="IUmbracoBuilder" /> used for registering event handlers.
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Registers a notification handler against the Umbraco service collection.
/// </summary>
/// <typeparam name="TNotification">The type of notification.</typeparam>
/// <typeparam name="TNotificationHandler">The type of notificiation handler.</typeparam>
/// <param name="builder">The Umbraco builder.</param>
/// <returns>The <see cref="IUmbracoBuilder" />.</returns>
public static IUmbracoBuilder AddNotificationHandler<TNotification, TNotificationHandler>(
this IUmbracoBuilder builder)
where TNotificationHandler : INotificationHandler<TNotification>
where TNotification : INotification
{
builder.Services.AddNotificationHandler<TNotification, TNotificationHandler>();
return builder;
}
/// <summary>
/// Contains extensions methods for <see cref="IUmbracoBuilder"/> used for registering event handlers.
/// Registers a notification async handler against the Umbraco service collection.
/// </summary>
public static partial class UmbracoBuilderExtensions
/// <typeparam name="TNotification">The type of notification.</typeparam>
/// <typeparam name="TNotificationAsyncHandler">The type of notification async handler.</typeparam>
/// <param name="builder">The Umbraco builder.</param>
/// <returns>The <see cref="IUmbracoBuilder" />.</returns>
public static IUmbracoBuilder AddNotificationAsyncHandler<TNotification, TNotificationAsyncHandler>(
this IUmbracoBuilder builder)
where TNotificationAsyncHandler : INotificationAsyncHandler<TNotification>
where TNotification : INotification
{
/// <summary>
/// Registers a notification handler against the Umbraco service collection.
/// </summary>
/// <typeparam name="TNotification">The type of notification.</typeparam>
/// <typeparam name="TNotificationHandler">The type of notificiation handler.</typeparam>
/// <param name="builder">The Umbraco builder.</param>
/// <returns>The <see cref="IUmbracoBuilder"/>.</returns>
public static IUmbracoBuilder AddNotificationHandler<TNotification, TNotificationHandler>(this IUmbracoBuilder builder)
where TNotificationHandler : INotificationHandler<TNotification>
where TNotification : INotification
builder.Services.AddNotificationAsyncHandler<TNotification, TNotificationAsyncHandler>();
return builder;
}
internal static IServiceCollection AddNotificationHandler<TNotification, TNotificationHandler>(
this IServiceCollection services)
where TNotificationHandler : INotificationHandler<TNotification>
where TNotification : INotification
{
// Register the handler as transient. This ensures that anything can be injected into it.
var descriptor = new UniqueServiceDescriptor(
typeof(INotificationHandler<TNotification>),
typeof(TNotificationHandler),
ServiceLifetime.Transient);
if (!services.Contains(descriptor))
{
builder.Services.AddNotificationHandler<TNotification, TNotificationHandler>();
return builder;
services.Add(descriptor);
}
/// <summary>
/// Registers a notification async handler against the Umbraco service collection.
/// </summary>
/// <typeparam name="TNotification">The type of notification.</typeparam>
/// <typeparam name="TNotificationAsyncHandler">The type of notification async handler.</typeparam>
/// <param name="builder">The Umbraco builder.</param>
/// <returns>The <see cref="IUmbracoBuilder"/>.</returns>
public static IUmbracoBuilder AddNotificationAsyncHandler<TNotification, TNotificationAsyncHandler>(this IUmbracoBuilder builder)
where TNotificationAsyncHandler : INotificationAsyncHandler<TNotification>
where TNotification : INotification
return services;
}
internal static IServiceCollection AddNotificationAsyncHandler<TNotification, TNotificationAsyncHandler>(
this IServiceCollection services)
where TNotificationAsyncHandler : INotificationAsyncHandler<TNotification>
where TNotification : INotification
{
// Register the handler as transient. This ensures that anything can be injected into it.
var descriptor = new ServiceDescriptor(
typeof(INotificationAsyncHandler<TNotification>),
typeof(TNotificationAsyncHandler),
ServiceLifetime.Transient);
if (!services.Contains(descriptor))
{
builder.Services.AddNotificationAsyncHandler<TNotification, TNotificationAsyncHandler>();
return builder;
services.Add(descriptor);
}
internal static IServiceCollection AddNotificationHandler<TNotification, TNotificationHandler>(this IServiceCollection services)
where TNotificationHandler : INotificationHandler<TNotification>
where TNotification : INotification
{
// Register the handler as transient. This ensures that anything can be injected into it.
var descriptor = new UniqueServiceDescriptor(typeof(INotificationHandler<TNotification>), typeof(TNotificationHandler), ServiceLifetime.Transient);
if (!services.Contains(descriptor))
{
services.Add(descriptor);
}
return services;
}
internal static IServiceCollection AddNotificationAsyncHandler<TNotification, TNotificationAsyncHandler>(this IServiceCollection services)
where TNotificationAsyncHandler : INotificationAsyncHandler<TNotification>
where TNotification : INotification
{
// Register the handler as transient. This ensures that anything can be injected into it.
var descriptor = new ServiceDescriptor(typeof(INotificationAsyncHandler<TNotification>), typeof(TNotificationAsyncHandler), ServiceLifetime.Transient);
if (!services.Contains(descriptor))
{
services.Add(descriptor);
}
return services;
}
return services;
}
}

View File

@@ -1,8 +1,6 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -25,7 +23,6 @@ using Umbraco.Cms.Core.Install;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Mail;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Packaging;
@@ -49,7 +46,7 @@ namespace Umbraco.Cms.Core.DependencyInjection
{
public class UmbracoBuilder : IUmbracoBuilder
{
private readonly Dictionary<Type, ICollectionBuilder?> _builders = new Dictionary<Type, ICollectionBuilder?>();
private readonly Dictionary<Type, ICollectionBuilder> _builders = new Dictionary<Type, ICollectionBuilder>();
public IServiceCollection Services { get; }
@@ -102,18 +99,17 @@ namespace Umbraco.Cms.Core.DependencyInjection
/// </summary>
/// <typeparam name="TBuilder">The type of the collection builder.</typeparam>
/// <returns>The collection builder.</returns>
public TBuilder? WithCollectionBuilder<TBuilder>()
public TBuilder WithCollectionBuilder<TBuilder>()
where TBuilder : ICollectionBuilder
{
Type typeOfBuilder = typeof(TBuilder);
if (_builders.TryGetValue(typeOfBuilder, out ICollectionBuilder? o))
{
return (TBuilder?)o;
return (TBuilder)o;
}
TBuilder? builder;
TBuilder builder;
if (typeof(TBuilder).GetConstructor(Type.EmptyTypes) != null)
{
builder = Activator.CreateInstance<TBuilder>();
@@ -121,7 +117,7 @@ namespace Umbraco.Cms.Core.DependencyInjection
else if (typeof(TBuilder).GetConstructor(new[] { typeof(IUmbracoBuilder) }) != null)
{
// Handle those collection builders which need a reference to umbraco builder i.e. DistributedLockingCollectionBuilder.
builder = (TBuilder?)Activator.CreateInstance(typeof(TBuilder), this);
builder = (TBuilder)Activator.CreateInstance(typeof(TBuilder), this)!;
}
else
{
@@ -134,9 +130,9 @@ namespace Umbraco.Cms.Core.DependencyInjection
public void Build()
{
foreach (ICollectionBuilder? builder in _builders.Values)
foreach (ICollectionBuilder builder in _builders.Values)
{
builder?.RegisterWith(Services);
builder.RegisterWith(Services);
}
_builders.Clear();
@@ -191,9 +187,6 @@ namespace Umbraco.Cms.Core.DependencyInjection
Services.AddSingleton<InstallStatusTracker>();
// by default, register a noop factory
Services.AddUnique<IPublishedModelFactory, NoopPublishedModelFactory>();
Services.AddUnique<ICultureDictionaryFactory, DefaultCultureDictionaryFactory>();
Services.AddSingleton(f => f.GetRequiredService<ICultureDictionaryFactory>().CreateDictionary());
@@ -327,6 +320,9 @@ namespace Umbraco.Cms.Core.DependencyInjection
// Register a noop IHtmlSanitizer to be replaced
Services.AddUnique<IHtmlSanitizer, NoopHtmlSanitizer>();
Services.AddUnique<IPropertyTypeUsageService, PropertyTypeUsageService>();
Services.AddUnique<IDataTypeUsageService, DataTypeUsageService>();
}
}
}

View File

@@ -1,58 +1,68 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
namespace Umbraco.Cms.Core.DependencyInjection
namespace Umbraco.Cms.Core.DependencyInjection;
/// <summary>
/// A custom <see cref="ServiceDescriptor" /> that supports unique checking
/// </summary>
/// <remarks>
/// This is required because the default implementation doesn't implement Equals or GetHashCode.
/// see: https://github.com/dotnet/runtime/issues/47262
/// </remarks>
public sealed class UniqueServiceDescriptor : ServiceDescriptor, IEquatable<UniqueServiceDescriptor>
{
/// <summary>
/// A custom <see cref="ServiceDescriptor"/> that supports unique checking
/// Initializes a new instance of the <see cref="UniqueServiceDescriptor" /> class.
/// </summary>
/// <remarks>
/// This is required because the default implementation doesn't implement Equals or GetHashCode.
/// see: https://github.com/dotnet/runtime/issues/47262
/// </remarks>
public sealed class UniqueServiceDescriptor : ServiceDescriptor, IEquatable<UniqueServiceDescriptor>
public UniqueServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime)
: base(serviceType, implementationType, lifetime)
{
/// <summary>
/// Initializes a new instance of the <see cref="UniqueServiceDescriptor"/> class.
/// </summary>
public UniqueServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime)
: base(serviceType, implementationType, lifetime)
}
/// <inheritdoc />
public bool Equals(UniqueServiceDescriptor? other) => other != null && Lifetime == other.Lifetime &&
EqualityComparer<Type>.Default.Equals(
ServiceType,
other.ServiceType) &&
EqualityComparer<Type?>.Default.Equals(
ImplementationType,
other.ImplementationType) &&
EqualityComparer<object?>.Default.Equals(
ImplementationInstance, other.ImplementationInstance) &&
EqualityComparer<Func<IServiceProvider, object>?>.Default
.Equals(
ImplementationFactory,
other.ImplementationFactory);
/// <inheritdoc />
public override bool Equals(object? obj) => Equals(obj as UniqueServiceDescriptor);
/// <inheritdoc />
public override int GetHashCode()
{
var hashCode = 493849952;
hashCode = (hashCode * -1521134295) + Lifetime.GetHashCode();
hashCode = (hashCode * -1521134295) + EqualityComparer<Type>.Default.GetHashCode(ServiceType);
if (ImplementationType is not null)
{
hashCode = (hashCode * -1521134295) + EqualityComparer<Type?>.Default.GetHashCode(ImplementationType);
}
/// <inheritdoc/>
public override bool Equals(object? obj) => Equals(obj as UniqueServiceDescriptor);
/// <inheritdoc/>
public bool Equals(UniqueServiceDescriptor? other) => other != null && Lifetime == other.Lifetime && EqualityComparer<Type>.Default.Equals(ServiceType, other.ServiceType) && EqualityComparer<Type?>.Default.Equals(ImplementationType, other.ImplementationType) && EqualityComparer<object?>.Default.Equals(ImplementationInstance, other.ImplementationInstance) && EqualityComparer<Func<IServiceProvider, object>?>.Default.Equals(ImplementationFactory, other.ImplementationFactory);
/// <inheritdoc/>
public override int GetHashCode()
if (ImplementationInstance is not null)
{
int hashCode = 493849952;
hashCode = (hashCode * -1521134295) + Lifetime.GetHashCode();
hashCode = (hashCode * -1521134295) + EqualityComparer<Type>.Default.GetHashCode(ServiceType);
if (ImplementationType is not null)
{
hashCode = (hashCode * -1521134295) + EqualityComparer<Type?>.Default.GetHashCode(ImplementationType);
}
if (ImplementationInstance is not null)
{
hashCode = (hashCode * -1521134295) + EqualityComparer<object?>.Default.GetHashCode(ImplementationInstance);
}
if (ImplementationFactory is not null)
{
hashCode = (hashCode * -1521134295) + EqualityComparer<Func<IServiceProvider, object>?>.Default.GetHashCode(ImplementationFactory);
}
return hashCode;
hashCode = (hashCode * -1521134295) + EqualityComparer<object?>.Default.GetHashCode(ImplementationInstance);
}
if (ImplementationFactory is not null)
{
hashCode = (hashCode * -1521134295) +
EqualityComparer<Func<IServiceProvider, object>?>.Default.GetHashCode(ImplementationFactory);
}
return hashCode;
}
}