Files
Umbraco-CMS/src/Umbraco.Web/Mvc/PluginController.cs
Elitsa Marinovska 84f19e093a NetCore: Refactor internal usages of UmbracoHelper (#7744)
* Removing properties related to IPublishedContentQuery, ITagQuery, ICultureDictionaryFactory, IUmbracoComponentRenderer from UmbracoHelper and using the corresponding private fields instead. Removing Media related methods

* Removed UmbracoHelper from UmbracoApiControllerBase

* Removed UmbracoHelper references

* Inject the needed Interfaces to classes that previously used UmbracoHelper to get the same functionality

* Fixed tests referencing UmbracoHelper

* Adding Media methods back

* Cleanup and moved UmbracoHelper from more base classes, when not used + simplified tests

* Reintroduced some methods..

* Reintroduced some methods..

* Reintroducing last missing bits

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2020-03-03 11:59:17 +01:00

119 lines
4.2 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Security;
using Umbraco.Web.WebApi;
using Current = Umbraco.Web.Composing.Current;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// Provides a base class for plugin controllers.
/// </summary>
public abstract class PluginController : Controller, IDiscoverable
{
private static readonly ConcurrentDictionary<Type, PluginControllerMetadata> MetadataStorage
= new ConcurrentDictionary<Type, PluginControllerMetadata>();
// for debugging purposes
internal Guid InstanceId { get; } = Guid.NewGuid();
/// <summary>
/// Gets the Umbraco context.
/// </summary>
public virtual IUmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext;
/// <summary>
/// Gets the database context accessor.
/// </summary>
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; }
/// <summary>
/// Gets the database context.
/// </summary>
public IUmbracoDatabaseFactory DatabaseFactory { get; }
/// <summary>
/// Gets or sets the services context.
/// </summary>
public ServiceContext Services { get; }
/// <summary>
/// Gets or sets the application cache.
/// </summary>
public AppCaches AppCaches { get; }
/// <summary>
/// Gets or sets the logger.
/// </summary>
public ILogger Logger { get; }
/// <summary>
/// Gets or sets the profiling logger.
/// </summary>
public IProfilingLogger ProfilingLogger { get; }
/// <summary>
/// Gets the Umbraco helper.
/// </summary>
public UmbracoHelper Umbraco { get; }
/// <summary>
/// Gets metadata for this instance.
/// </summary>
internal PluginControllerMetadata Metadata => GetMetadata(GetType());
protected PluginController()
: this(
Current.Factory.GetInstance<IUmbracoContextAccessor>(),
Current.Factory.GetInstance<IUmbracoDatabaseFactory>(),
Current.Factory.GetInstance<ServiceContext>(),
Current.Factory.GetInstance<AppCaches>(),
Current.Factory.GetInstance<ILogger>(),
Current.Factory.GetInstance<IProfilingLogger>()
)
{
}
protected PluginController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, ILogger logger, IProfilingLogger profilingLogger)
{
UmbracoContextAccessor = umbracoContextAccessor;
DatabaseFactory = databaseFactory;
Services = services;
AppCaches = appCaches;
Logger = logger;
ProfilingLogger = profilingLogger;
}
/// <summary>
/// Gets metadata for a controller type.
/// </summary>
/// <param name="controllerType">The controller type.</param>
/// <returns>Metadata for the controller type.</returns>
internal static PluginControllerMetadata GetMetadata(Type controllerType)
{
return MetadataStorage.GetOrAdd(controllerType, type =>
{
// plugin controller? back-office controller?
var pluginAttribute = controllerType.GetCustomAttribute<PluginControllerAttribute>(false);
var backOfficeAttribute = controllerType.GetCustomAttribute<IsBackOfficeAttribute>(true);
return new PluginControllerMetadata
{
AreaName = pluginAttribute?.AreaName,
ControllerName = ControllerExtensions.GetControllerName(controllerType),
ControllerNamespace = controllerType.Namespace,
ControllerType = controllerType,
IsBackOffice = backOfficeAttribute != null
};
});
}
}
}