Update to match new rules. And fix dup usings

This commit is contained in:
James Jackson-South
2020-12-03 13:32:04 +00:00
parent 606d8bcf51
commit 826e5a9bad
15 changed files with 138 additions and 129 deletions

View File

@@ -1,4 +1,4 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Umbraco.Core.Composing;

View File

@@ -1,15 +1,18 @@
using Microsoft.Extensions.DependencyInjection;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Core.Events;
namespace Umbraco.Core.DependencyInjection
{
/// <summary>
/// Contains extensions methods for <see cref="IUmbracoBuilder"/>.
/// 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.
/// 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>

View File

@@ -1,9 +1,12 @@
using Microsoft.Extensions.Configuration;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using Umbraco.Core.Composing;
using Umbraco.Core.Events;

View File

@@ -1,4 +1,7 @@
using System;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
@@ -12,13 +15,13 @@ namespace Umbraco.Core.Events
/// </content>
public partial class EventAggregator : IEventAggregator
{
private static readonly ConcurrentDictionary<Type, NotificationHandlerWrapper> NotificationHandlers
private static readonly ConcurrentDictionary<Type, NotificationHandlerWrapper> s_notificationHandlers
= new ConcurrentDictionary<Type, NotificationHandlerWrapper>();
private Task PublishNotificationAsync(INotification notification, CancellationToken cancellationToken = default)
{
var notificationType = notification.GetType();
var handler = NotificationHandlers.GetOrAdd(
Type notificationType = notification.GetType();
NotificationHandlerWrapper handler = s_notificationHandlers.GetOrAdd(
notificationType,
t => (NotificationHandlerWrapper)Activator.CreateInstance(typeof(NotificationHandlerWrapperImpl<>).MakeGenericType(notificationType)));
@@ -27,9 +30,10 @@ namespace Umbraco.Core.Events
private async Task PublishCoreAsync(
IEnumerable<Func<INotification, CancellationToken, Task>> allHandlers,
INotification notification, CancellationToken cancellationToken)
INotification notification,
CancellationToken cancellationToken)
{
foreach (var handler in allHandlers)
foreach (Func<INotification, CancellationToken, Task> handler in allHandlers)
{
await handler(notification, cancellationToken).ConfigureAwait(false);
}

View File

@@ -1,10 +1,21 @@
using System;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Umbraco.Core.Events
{
/// <summary>
/// A factory method used to resolve all services.
/// For multiple instances, it will resolve against <see cref="IEnumerable{T}" />.
/// </summary>
/// <param name="serviceType">Type of service to resolve.</param>
/// <returns>An instance of type <paramref name="serviceType" />.</returns>
public delegate object ServiceFactory(Type serviceType);
/// <inheritdoc/>
public partial class EventAggregator : IEventAggregator
{
@@ -31,14 +42,6 @@ namespace Umbraco.Core.Events
}
}
/// <summary>
/// A factory method used to resolve all services.
/// For multiple instances, it will resolve against <see cref="IEnumerable{T}" />.
/// </summary>
/// <param name="serviceType">Type of service to resolve.</param>
/// <returns>An instance of type <paramref name="serviceType" />.</returns>
public delegate object ServiceFactory(Type serviceType);
/// <summary>
/// Extensions for <see cref="ServiceFactory"/>.
/// </summary>

View File

@@ -1,4 +1,7 @@
using System.Threading;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Threading;
using System.Threading.Tasks;
namespace Umbraco.Core.Events
@@ -14,7 +17,7 @@ namespace Umbraco.Core.Events
/// </summary>
/// <typeparam name="TNotification">The type of notification being handled.</typeparam>
/// <param name="notification">The notification object.</param>
/// <param name="cancellationToken">An optional cancellation token</param>
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>A task that represents the publish operation.</returns>
Task PublishAsync<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
where TNotification : INotification;
@@ -23,10 +26,12 @@ namespace Umbraco.Core.Events
/// <summary>
/// A marker interface to represent a notification.
/// </summary>
public interface INotification { }
public interface INotification
{
}
/// <summary>
/// Defines a handler for a notification
/// Defines a handler for a notification.
/// </summary>
/// <typeparam name="TNotification">The type of notification being handled.</typeparam>
public interface INotificationHandler<in TNotification>
@@ -36,7 +41,8 @@ namespace Umbraco.Core.Events
/// Handles a notification
/// </summary>
/// <param name="notification">The notification</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task HandleAsync(TNotification notification, CancellationToken cancellationToken);
}
}

View File

@@ -1,29 +1,17 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Mappers;
using Umbraco.Core.Runtime;
using Umbraco.Core.DependencyInjection;
using Umbraco.Extensions;
using Umbraco.Tests.Common;
using Umbraco.Tests.Integration.Extensions;
using Umbraco.Tests.Integration.Implementations;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Core.DependencyInjection;
namespace Umbraco.Tests.Integration
{
@@ -70,8 +58,8 @@ namespace Umbraco.Tests.Integration
AppCaches.NoCache,
hostContext.Configuration,
testHelper.Profiler);
var builder = new UmbracoBuilder(services, hostContext.Configuration, typeLoader, testHelper.ConsoleLoggerFactory);
var builder = new UmbracoBuilder(services, hostContext.Configuration, typeLoader, testHelper.ConsoleLoggerFactory);
builder.Services.AddUnique<AppCaches>(AppCaches.NoCache);
builder.AddConfiguration();
builder.AddUmbracoCore();
@@ -109,7 +97,7 @@ namespace Umbraco.Tests.Integration
var webHostEnvironment = testHelper.GetWebHostEnvironment();
services.AddSingleton(testHelper.DbProviderFactoryCreator);
services.AddRequiredNetCoreServices(testHelper, webHostEnvironment);
// Add it!
var typeLoader = services.AddTypeLoader(

View File

@@ -1,11 +1,9 @@
using Moq;
using Moq;
using Umbraco.Core;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Cache;
using Umbraco.Core.Logging;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Runtime;
using Umbraco.Tests.Integration.Implementations;
using Umbraco.Core.DependencyInjection;
namespace Umbraco.Tests.Integration.TestServerTest
{

View File

@@ -1,41 +1,35 @@
using System;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Serilog;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Mappers;
using Umbraco.Core.Runtime;
using Umbraco.Core.Scoping;
using Umbraco.Core.Strings;
using Umbraco.Extensions;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Integration.Extensions;
using Umbraco.Tests.Integration.Implementations;
using Umbraco.Extensions;
using Umbraco.Tests.Testing;
using Umbraco.Web;
using Umbraco.Core.Runtime;
using Umbraco.Core;
using Moq;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using Umbraco.Core.Configuration.Models;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.DependencyInjection;
using ConnectionStrings = Umbraco.Core.Configuration.Models.ConnectionStrings;
namespace Umbraco.Tests.Integration.Testing
{
@@ -65,7 +59,8 @@ namespace Umbraco.Tests.Integration.Testing
[OneTimeTearDown]
public void FixtureTearDown()
{
foreach (var a in _fixtureTeardown) a();
foreach (var a in _fixtureTeardown)
a();
}
[TearDown]
@@ -73,7 +68,8 @@ namespace Umbraco.Tests.Integration.Testing
{
if (_testTeardown != null)
{
foreach (var a in _testTeardown) a();
foreach (var a in _testTeardown)
a();
}
_testTeardown = null;
FirstTestInFixture = false;
@@ -161,7 +157,7 @@ namespace Umbraco.Tests.Integration.Testing
});
return hostBuilder;
}
#endregion
#region IStartup
@@ -275,7 +271,8 @@ namespace Umbraco.Tests.Integration.Testing
{
lock (_dbLocker)
{
if (_dbInstance != null) return _dbInstance;
if (_dbInstance != null)
return _dbInstance;
var localDb = new LocalDb();
if (localDb.IsAvailable == false)

View File

@@ -1,19 +1,19 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
@@ -21,7 +21,6 @@ using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Mappers;
using Umbraco.Core.Scoping;
using Umbraco.Tests.TestHelpers;
using Umbraco.Core.DependencyInjection;
namespace Umbraco.Tests.UnitTests.Umbraco.Core.Components
{
@@ -49,12 +48,12 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Components
var mediaFileSystem = Mock.Of<IMediaFileSystem>();
var p = new ScopeProvider(f, fs, Options.Create(coreDebug), mediaFileSystem, loggerFactory.CreateLogger<ScopeProvider>(), loggerFactory, typeFinder, NoAppCache.Instance);
mock.Setup(x => x.GetService(typeof (ILogger))).Returns(logger);
mock.Setup(x => x.GetService(typeof(ILogger))).Returns(logger);
mock.Setup(x => x.GetService(typeof(ILogger<ComponentCollection>))).Returns(loggerFactory.CreateLogger<ComponentCollection>);
mock.Setup(x => x.GetService(typeof(ILoggerFactory))).Returns(loggerFactory);
mock.Setup(x => x.GetService(typeof (IProfilingLogger))).Returns(new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), Mock.Of<IProfiler>()));
mock.Setup(x => x.GetService(typeof (IUmbracoDatabaseFactory))).Returns(f);
mock.Setup(x => x.GetService(typeof (IScopeProvider))).Returns(p);
mock.Setup(x => x.GetService(typeof(IProfilingLogger))).Returns(new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), Mock.Of<IProfiler>()));
mock.Setup(x => x.GetService(typeof(IUmbracoDatabaseFactory))).Returns(f);
mock.Setup(x => x.GetService(typeof(IScopeProvider))).Returns(p);
setup?.Invoke(mock);
return mock.Object;
@@ -94,11 +93,16 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Components
m.Setup(x => x.GetService(It.Is<Type>(t => t == typeof(ISomeResource)))).Returns(() => new SomeResource());
m.Setup(x => x.GetService(It.IsAny<Type>())).Returns<Type>((type) =>
{
if (type == typeof(Composer1)) return new Composer1();
if (type == typeof(Composer5)) return new Composer5();
if (type == typeof(Component5)) return new Component5(new SomeResource());
if (type == typeof(IProfilingLogger)) return new ProfilingLogger(Mock.Of<ILogger<ProfilingLogger>>(), Mock.Of<IProfiler>());
if (type == typeof(ILogger<ComponentCollection>)) return Mock.Of<ILogger<ComponentCollection>>();
if (type == typeof(Composer1))
return new Composer1();
if (type == typeof(Composer5))
return new Composer5();
if (type == typeof(Component5))
return new Component5(new SomeResource());
if (type == typeof(IProfilingLogger))
return new ProfilingLogger(Mock.Of<ILogger<ProfilingLogger>>(), Mock.Of<IProfiler>());
if (type == typeof(ILogger<ComponentCollection>))
return Mock.Of<ILogger<ComponentCollection>>();
throw new NotSupportedException(type.FullName);
});
});
@@ -213,16 +217,23 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Components
var typeLoader = MockTypeLoader();
var factory = MockFactory(m =>
{
m.Setup(x => x.GetService(It.Is<Type>(t => t == typeof (ISomeResource)))).Returns(() => new SomeResource());
m.Setup(x => x.GetService(It.Is<Type>(t => t == typeof(ISomeResource)))).Returns(() => new SomeResource());
m.Setup(x => x.GetService(It.IsAny<Type>())).Returns<Type>((type) =>
{
if (type == typeof(Composer1)) return new Composer1();
if (type == typeof(Composer5)) return new Composer5();
if (type == typeof(Composer5a)) return new Composer5a();
if (type == typeof(Component5)) return new Component5(new SomeResource());
if (type == typeof(Component5a)) return new Component5a();
if (type == typeof(IProfilingLogger)) return new ProfilingLogger(Mock.Of<ILogger<ProfilingLogger>>(), Mock.Of<IProfiler>());
if (type == typeof(ILogger<ComponentCollection>)) return Mock.Of<ILogger<ComponentCollection>>();
if (type == typeof(Composer1))
return new Composer1();
if (type == typeof(Composer5))
return new Composer5();
if (type == typeof(Composer5a))
return new Composer5a();
if (type == typeof(Component5))
return new Component5(new SomeResource());
if (type == typeof(Component5a))
return new Component5a();
if (type == typeof(IProfilingLogger))
return new ProfilingLogger(Mock.Of<ILogger<ProfilingLogger>>(), Mock.Of<IProfiler>());
if (type == typeof(ILogger<ComponentCollection>))
return Mock.Of<ILogger<ComponentCollection>>();
throw new NotSupportedException(type.FullName);
});
});

View File

@@ -1,18 +1,14 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.DependencyInjection;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.UnitTests.TestHelpers;
using Umbraco.Core.DependencyInjection;
namespace Umbraco.Tests.UnitTests.Umbraco.Core.Composing
{
@@ -128,7 +124,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Composing
//builder.Append<Resolved4>(); // does not compile
Assert.Throws<InvalidOperationException>(() =>
builder.Append(new[] { typeof (Resolved4) }) // throws
builder.Append(new[] { typeof(Resolved4) }) // throws
);
}

View File

@@ -1,9 +1,12 @@
using Microsoft.Extensions.Configuration;
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
using System.Threading;
using System.Threading.Tasks;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Events;
using Umbraco.Tests.TestHelpers;
@@ -20,7 +23,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Events
[SetUp]
public void Setup()
{
var register = TestHelper.GetServiceCollection();
IServiceCollection register = TestHelper.GetServiceCollection();
_builder = new UmbracoBuilder(register, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
}
@@ -31,10 +34,10 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Events
_builder.AddNotificationHandler<Notification, NotificationHandlerA>();
_builder.AddNotificationHandler<Notification, NotificationHandlerB>();
_builder.AddNotificationHandler<Notification, NotificationHandlerC>();
var provider = _builder.Services.BuildServiceProvider();
ServiceProvider provider = _builder.Services.BuildServiceProvider();
var notification = new Notification();
var aggregator = provider.GetService<IEventAggregator>();
IEventAggregator aggregator = provider.GetService<IEventAggregator>();
await aggregator.PublishAsync(notification);
Assert.AreEqual(A + B + C, notification.SubscriberCount);
@@ -78,10 +81,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Events
public class Adder
{
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b) => a + b;
}
}
}

View File

@@ -1,11 +1,9 @@
using System;
using System;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualBasic;
using Umbraco.Core.DependencyInjection;
using Umbraco.Web.BackOffice.Filters;
using Umbraco.Web.BackOffice.Security;
using Umbraco.Core.DependencyInjection;
namespace Umbraco.Extensions
{
@@ -36,7 +34,7 @@ namespace Umbraco.Extensions
builder.Services
.AddAuthentication() // This just creates a builder, nothing more
// Add our custom schemes which are cookie handlers
// Add our custom schemes which are cookie handlers
.AddCookie(Core.Constants.Security.BackOfficeAuthenticationType)
.AddCookie(Core.Constants.Security.BackOfficeExternalAuthenticationType, o =>
{

View File

@@ -1,5 +1,5 @@
using System;
using Umbraco.Core.Builder;
using System;
using Umbraco.Core.DependencyInjection;
namespace Umbraco.Web.BackOffice.Security
{

View File

@@ -1,18 +1,17 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@@ -20,12 +19,12 @@ using Serilog;
using Smidge;
using Smidge.Nuglify;
using Umbraco.Core;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Configuration.Models.Validation;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
@@ -52,8 +51,10 @@ namespace Umbraco.Core.DependencyInjection
IWebHostEnvironment webHostEnvironment,
IConfiguration config)
{
if (services is null) throw new ArgumentNullException(nameof(services));
if (config is null) throw new ArgumentNullException(nameof(config));
if (services is null)
throw new ArgumentNullException(nameof(services));
if (config is null)
throw new ArgumentNullException(nameof(config));
var loggingConfig = new LoggingConfiguration(Path.Combine(webHostEnvironment.ContentRootPath, "umbraco", "logs"));
@@ -79,7 +80,8 @@ namespace Umbraco.Core.DependencyInjection
/// <remarks>Composes Composers</remarks>
public static IUmbracoBuilder AddUmbracoCore(this IUmbracoBuilder builder)
{
if (builder is null) throw new ArgumentNullException(nameof(builder));
if (builder is null)
throw new ArgumentNullException(nameof(builder));
builder.Services.AddLazySupport();