diff --git a/src/Umbraco.Core/Cache/CacheRefresherCollectionBuilder.cs b/src/Umbraco.Core/Cache/CacheRefresherCollectionBuilder.cs index 11ac05844b..bb4a14c147 100644 --- a/src/Umbraco.Core/Cache/CacheRefresherCollectionBuilder.cs +++ b/src/Umbraco.Core/Cache/CacheRefresherCollectionBuilder.cs @@ -1,12 +1,10 @@ -using System.Collections.Generic; -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.Cache { public class CacheRefresherCollectionBuilder : LazyCollectionBuilderBase { - public CacheRefresherCollectionBuilder(IServiceContainer container) + public CacheRefresherCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/Composing/CollectionBuilderBase.cs b/src/Umbraco.Core/Composing/CollectionBuilderBase.cs index 2dbefc860f..ecbd9533ee 100644 --- a/src/Umbraco.Core/Composing/CollectionBuilderBase.cs +++ b/src/Umbraco.Core/Composing/CollectionBuilderBase.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using LightInject; namespace Umbraco.Core.Composing { @@ -19,14 +18,14 @@ namespace Umbraco.Core.Composing private readonly List _types = new List(); private readonly object _locker = new object(); private Func, TCollection> _collectionCtor; - private ServiceRegistration[] _registrations; + private Registration[] _registrations; /// /// Initializes a new instance of the /// class with a service container. /// - /// A service container. - protected CollectionBuilderBase(IServiceContainer container) + /// A container. + protected CollectionBuilderBase(IContainer container) { Container = container; // ReSharper disable once DoNotCallOverridableMethodsInConstructor @@ -34,9 +33,9 @@ namespace Umbraco.Core.Composing } /// - /// Gets the service container. + /// Gets the container. /// - protected IServiceContainer Container { get; } + protected IContainer Container { get; } /// /// Gets the internal list of types as an IEnumerable (immutable). @@ -64,7 +63,7 @@ namespace Umbraco.Core.Composing // else _collectionCtor remains null, assuming CreateCollection has been overriden // we just don't want to support re-registering collections here - var registration = Container.GetAvailableService(); + var registration = Container.GetRegistered().FirstOrDefault(); if (registration != null) throw new InvalidOperationException("Collection builders cannot be registered once the collection itself has been registered."); @@ -75,8 +74,7 @@ namespace Umbraco.Core.Composing /// /// Gets the collection lifetime. /// - /// Return null for transient collections. - protected virtual ILifetime CollectionLifetime => new PerContainerLifetime(); + protected virtual Lifetime CollectionLifetime => Lifetime.Singleton; /// /// Configures the internal list of types. @@ -122,7 +120,11 @@ namespace Umbraco.Core.Composing Container.Register(typeof(TItem), type, name); } - _registrations = Container.AvailableServices + // note: we do this, because we don't want to get "all", + // because other types implementing TItem may be registered, + // and we only want those for *this* builder + + _registrations = Container.GetRegistered(typeof(TItem)) .Where(x => x.ServiceName.StartsWith(prefix)) .OrderBy(x => x.ServiceName) .ToArray(); @@ -134,13 +136,13 @@ namespace Umbraco.Core.Composing /// /// The arguments. /// The collection items. - protected virtual IEnumerable CreateItems(params object[] args) + protected virtual IEnumerable CreateItems(/*params object[] args*/) { RegisterTypes(); // will do it only once var type = typeof (TItem); return _registrations - .Select(x => (TItem) Container.GetInstance(type, x.ServiceName, args)) + .Select(x => (TItem) Container.GetInstance(type, x.ServiceName/*, args*/)) .ToArray(); // safe } diff --git a/src/Umbraco.Core/Composing/IContainer.cs b/src/Umbraco.Core/Composing/IContainer.cs index ba611063e2..6e0417a334 100644 --- a/src/Umbraco.Core/Composing/IContainer.cs +++ b/src/Umbraco.Core/Composing/IContainer.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Reflection; namespace Umbraco.Core.Composing { @@ -66,6 +67,9 @@ namespace Umbraco.Core.Composing /// The type of the service. IEnumerable GetAllInstances(); + // fixme + IEnumerable GetRegistered(Type serviceType); + #endregion #region Registry @@ -117,6 +121,10 @@ namespace Umbraco.Core.Composing /// A collection builder of the specified type. T RegisterCollectionBuilder(); + // fixme - very LightInject specific? or? + void RegisterConstructorDependency(Func factory); + void RegisterConstructorDependency(Func factory); + #endregion #region Control @@ -132,11 +140,11 @@ namespace Umbraco.Core.Composing // fixme - document all these - void ConfigureForUmbraco(); + IContainer ConfigureForUmbraco(); - void ConfigureForWeb(); + IContainer ConfigureForWeb(); - void EnablePerWebRequestScope(); + IContainer EnablePerWebRequestScope(); #endregion } diff --git a/src/Umbraco.Core/Composing/LazyCollectionBuilderBase.cs b/src/Umbraco.Core/Composing/LazyCollectionBuilderBase.cs index 52e5a764fd..04110b2070 100644 --- a/src/Umbraco.Core/Composing/LazyCollectionBuilderBase.cs +++ b/src/Umbraco.Core/Composing/LazyCollectionBuilderBase.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; namespace Umbraco.Core.Composing { @@ -16,13 +15,13 @@ namespace Umbraco.Core.Composing where TCollection : IBuilderCollection { private readonly List>> _producers1 = new List>>(); - private readonly List>> _producers2 = new List>>(); + private readonly List>> _producers2 = new List>>(); private readonly List _excluded = new List(); /// /// Initializes a new instance of the class. /// - protected LazyCollectionBuilderBase(IServiceContainer container) + protected LazyCollectionBuilderBase(IContainer container) : base(container) { } @@ -125,7 +124,7 @@ namespace Umbraco.Core.Composing /// /// The types producer. /// The builder. - public TBuilder Add(Func> producer) + public TBuilder Add(Func> producer) { Configure(types => { diff --git a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs index 0cc7700888..de942c1e4c 100644 --- a/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs +++ b/src/Umbraco.Core/Composing/LightInject/LightInjectContainer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using System.Linq; using LightInject; namespace Umbraco.Core.Composing.LightInject @@ -56,6 +57,10 @@ namespace Umbraco.Core.Composing.LightInject public IEnumerable GetAllInstances(Type type) => Container.GetAllInstances(type); + /// + public IEnumerable GetRegistered(Type type) + => Container.GetAvailableServices(type).Select(x => new Registration(x.ServiceType, x.ServiceName)); + #endregion #region Registry @@ -173,6 +178,14 @@ namespace Umbraco.Core.Composing.LightInject public void RegisterOrdered(Type serviceType, Type[] implementingTypes, Lifetime lifetime = Lifetime.Transient) => Container.RegisterOrdered(serviceType, implementingTypes, _ => GetLifetime(lifetime)); + /// + public void RegisterConstructorDependency(Func factory) + => Container.RegisterConstructorDependency((f, x) => factory(this, x)); + + /// + public void RegisterConstructorDependency(Func factory) + => Container.RegisterConstructorDependency((f, x, a) => factory(this, x, a)); + /// public T RegisterCollectionBuilder() => Container.RegisterCollectionBuilder(); @@ -186,7 +199,7 @@ namespace Umbraco.Core.Composing.LightInject => Container.BeginScope(); /// - public void ConfigureForUmbraco() + public IContainer ConfigureForUmbraco() { // supports annotated constructor injections // eg to specify the service name on some services @@ -211,6 +224,8 @@ namespace Umbraco.Core.Composing.LightInject // see notes in MixedLightInjectScopeManagerProvider Container.ScopeManagerProvider = new MixedLightInjectScopeManagerProvider(); + + return this; } private class AssemblyScanner : IAssemblyScanner @@ -234,15 +249,18 @@ namespace Umbraco.Core.Composing.LightInject } /// - public virtual void ConfigureForWeb() - { } + public virtual IContainer ConfigureForWeb() + { + return this; + } /// - public void EnablePerWebRequestScope() + public IContainer EnablePerWebRequestScope() { if (!(Container.ScopeManagerProvider is MixedLightInjectScopeManagerProvider smp)) throw new Exception("Container.ScopeManagerProvider is not MixedLightInjectScopeManagerProvider."); smp.EnablePerWebRequestScope(); + return this; } #endregion diff --git a/src/Umbraco.Core/Composing/MixedLightInjectScopeManagerProvider.cs b/src/Umbraco.Core/Composing/LightInject/MixedLightInjectScopeManagerProvider.cs similarity index 97% rename from src/Umbraco.Core/Composing/MixedLightInjectScopeManagerProvider.cs rename to src/Umbraco.Core/Composing/LightInject/MixedLightInjectScopeManagerProvider.cs index 05bdbc446d..470079c6c0 100644 --- a/src/Umbraco.Core/Composing/MixedLightInjectScopeManagerProvider.cs +++ b/src/Umbraco.Core/Composing/LightInject/MixedLightInjectScopeManagerProvider.cs @@ -1,7 +1,7 @@ using LightInject; using LightInject.Web; -namespace Umbraco.Core.Composing +namespace Umbraco.Core.Composing.LightInject { // by default, the container's scope manager provider is PerThreadScopeManagerProvider, // and then container.EnablePerWebRequestScope() replaces it with PerWebRequestScopeManagerProvider, diff --git a/src/Umbraco.Core/Composing/LightInjectExtensions.cs b/src/Umbraco.Core/Composing/LightInjectExtensions.cs index 446b2850f7..6c8961ec66 100644 --- a/src/Umbraco.Core/Composing/LightInjectExtensions.cs +++ b/src/Umbraco.Core/Composing/LightInjectExtensions.cs @@ -134,7 +134,7 @@ namespace Umbraco.Core.Composing /// /// Updates a registration. /// - private static void UpdateRegistration(Registration registration, Type implementingType, Delegate factoryExpression) + private static void UpdateRegistration(ServiceRegistration registration, Type implementingType, Delegate factoryExpression) { // if the container has compiled already then the registrations have been captured, // and re-registering - although updating available services - does not modify the diff --git a/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs b/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs index 4811551cd2..6dd6945ee1 100644 --- a/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs +++ b/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using LightInject; namespace Umbraco.Core.Composing { @@ -18,7 +17,7 @@ namespace Umbraco.Core.Composing /// Initializes a new instance of the class. /// /// - protected OrderedCollectionBuilderBase(IServiceContainer container) + protected OrderedCollectionBuilderBase(IContainer container) : base (container) { } @@ -92,7 +91,7 @@ namespace Umbraco.Core.Composing /// /// The types to append. /// The builder. - public TBuilder Append(Func> types) + public TBuilder Append(Func> types) { Configure(list => { diff --git a/src/Umbraco.Core/Composing/Registration.cs b/src/Umbraco.Core/Composing/Registration.cs new file mode 100644 index 0000000000..a3cb62522b --- /dev/null +++ b/src/Umbraco.Core/Composing/Registration.cs @@ -0,0 +1,31 @@ +using System; + +namespace Umbraco.Core.Composing +{ + /// + /// Represents a service registration in the container. + /// + public class Registration + { + /// + /// Initializes a new instance of the class. + /// + /// + /// + public Registration(Type serviceType, string serviceName) + { + ServiceType = serviceType; + ServiceName = serviceName; + } + + /// + /// Gets the service type. + /// + public Type ServiceType { get; } + + /// + /// Gets the service name. + /// + public string ServiceName { get; } + } +} diff --git a/src/Umbraco.Core/Composing/WeightedCollectionBuilderBase.cs b/src/Umbraco.Core/Composing/WeightedCollectionBuilderBase.cs index 99fa2d3eb9..499984a931 100644 --- a/src/Umbraco.Core/Composing/WeightedCollectionBuilderBase.cs +++ b/src/Umbraco.Core/Composing/WeightedCollectionBuilderBase.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; namespace Umbraco.Core.Composing { @@ -19,7 +18,7 @@ namespace Umbraco.Core.Composing /// Initializes a new instance of the class. /// /// - protected WeightedCollectionBuilderBase(IServiceContainer container) + protected WeightedCollectionBuilderBase(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/ContainerExtensions.cs b/src/Umbraco.Core/ContainerExtensions.cs index ddaab485c1..0a45de4f15 100644 --- a/src/Umbraco.Core/ContainerExtensions.cs +++ b/src/Umbraco.Core/ContainerExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Umbraco.Core.Composing; namespace Umbraco.Core @@ -45,6 +46,9 @@ namespace Umbraco.Core // fixme - document all these + public static IEnumerable GetRegistered(this IContainer container) + => container.GetRegistered(typeof(TService)); + public static void Register(this IContainer container, Lifetime lifetime = Lifetime.Transient) => container.Register(typeof(TService), typeof(TImplementing), lifetime); diff --git a/src/Umbraco.Core/Migrations/MigrationBuilder.cs b/src/Umbraco.Core/Migrations/MigrationBuilder.cs index fb01afc35a..c4a06f4789 100644 --- a/src/Umbraco.Core/Migrations/MigrationBuilder.cs +++ b/src/Umbraco.Core/Migrations/MigrationBuilder.cs @@ -1,5 +1,4 @@ using System; -using LightInject; using Umbraco.Core.Composing; namespace Umbraco.Core.Migrations diff --git a/src/Umbraco.Core/Migrations/PostMigrationCollectionBuilder.cs b/src/Umbraco.Core/Migrations/PostMigrationCollectionBuilder.cs index 63cdaf4454..ba3eb96ce9 100644 --- a/src/Umbraco.Core/Migrations/PostMigrationCollectionBuilder.cs +++ b/src/Umbraco.Core/Migrations/PostMigrationCollectionBuilder.cs @@ -1,16 +1,15 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.Migrations { public class PostMigrationCollectionBuilder : LazyCollectionBuilderBase { - public PostMigrationCollectionBuilder(IServiceContainer container) + public PostMigrationCollectionBuilder(IContainer container) : base(container) { } protected override PostMigrationCollectionBuilder This => this; - protected override ILifetime CollectionLifetime => null; // transient + protected override Lifetime CollectionLifetime => Lifetime.Transient; } } diff --git a/src/Umbraco.Core/Persistence/Mappers/MapperCollectionBuilder.cs b/src/Umbraco.Core/Persistence/Mappers/MapperCollectionBuilder.cs index 82085027ab..f861c450aa 100644 --- a/src/Umbraco.Core/Persistence/Mappers/MapperCollectionBuilder.cs +++ b/src/Umbraco.Core/Persistence/Mappers/MapperCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.Persistence.Mappers { public class MapperCollectionBuilder : LazyCollectionBuilderBase { - public MapperCollectionBuilder(IServiceContainer container) + public MapperCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/PropertyEditors/DataEditorCollectionBuilder.cs b/src/Umbraco.Core/PropertyEditors/DataEditorCollectionBuilder.cs index 2a53142a1c..c23972c684 100644 --- a/src/Umbraco.Core/PropertyEditors/DataEditorCollectionBuilder.cs +++ b/src/Umbraco.Core/PropertyEditors/DataEditorCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.PropertyEditors { public class DataEditorCollectionBuilder : LazyCollectionBuilderBase { - public DataEditorCollectionBuilder(IServiceContainer container) + public DataEditorCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/PropertyEditors/ManifestValueValidatorCollectionBuilder.cs b/src/Umbraco.Core/PropertyEditors/ManifestValueValidatorCollectionBuilder.cs index d616ecf715..3ba6f4e402 100644 --- a/src/Umbraco.Core/PropertyEditors/ManifestValueValidatorCollectionBuilder.cs +++ b/src/Umbraco.Core/PropertyEditors/ManifestValueValidatorCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.PropertyEditors { internal class ManifestValueValidatorCollectionBuilder : LazyCollectionBuilderBase { - public ManifestValueValidatorCollectionBuilder(IServiceContainer container) + public ManifestValueValidatorCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/PropertyEditors/PropertyValueConverterCollectionBuilder.cs b/src/Umbraco.Core/PropertyEditors/PropertyValueConverterCollectionBuilder.cs index 5c5a8c16c8..e95159ed0b 100644 --- a/src/Umbraco.Core/PropertyEditors/PropertyValueConverterCollectionBuilder.cs +++ b/src/Umbraco.Core/PropertyEditors/PropertyValueConverterCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.PropertyEditors { public class PropertyValueConverterCollectionBuilder : OrderedCollectionBuilderBase { - public PropertyValueConverterCollectionBuilder(IServiceContainer container) + public PropertyValueConverterCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/Strings/UrlSegmentProviderCollectionBuilder.cs b/src/Umbraco.Core/Strings/UrlSegmentProviderCollectionBuilder.cs index a9b8234e14..7c45bf0884 100644 --- a/src/Umbraco.Core/Strings/UrlSegmentProviderCollectionBuilder.cs +++ b/src/Umbraco.Core/Strings/UrlSegmentProviderCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core.Strings { public class UrlSegmentProviderCollectionBuilder : OrderedCollectionBuilderBase { - public UrlSegmentProviderCollectionBuilder(IServiceContainer container) + public UrlSegmentProviderCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index efea4bdd22..8d24625f6d 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -170,8 +170,9 @@ - + + diff --git a/src/Umbraco.Core/_Legacy/PackageActions/PackageActionCollectionBuilder.cs b/src/Umbraco.Core/_Legacy/PackageActions/PackageActionCollectionBuilder.cs index 42ab3ec7c2..a92d340b48 100644 --- a/src/Umbraco.Core/_Legacy/PackageActions/PackageActionCollectionBuilder.cs +++ b/src/Umbraco.Core/_Legacy/PackageActions/PackageActionCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Core._Legacy.PackageActions { internal class PackageActionCollectionBuilder : LazyCollectionBuilderBase { - public PackageActionCollectionBuilder(IServiceContainer container) + public PackageActionCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs index 3ab554b28e..d34df9b8b2 100644 --- a/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs +++ b/src/Umbraco.Tests/Composing/CollectionBuildersTests.cs @@ -435,7 +435,7 @@ namespace Umbraco.Tests.Composing // ReSharper disable once ClassNeverInstantiated.Local private class TestCollectionBuilder : OrderedCollectionBuilderBase { - public TestCollectionBuilder(IServiceContainer container) + public TestCollectionBuilder(IContainer container) : base(container) { } @@ -445,31 +445,31 @@ namespace Umbraco.Tests.Composing // ReSharper disable once ClassNeverInstantiated.Local private class TestCollectionBuilderTransient : OrderedCollectionBuilderBase { - public TestCollectionBuilderTransient(IServiceContainer container) + public TestCollectionBuilderTransient(IContainer container) : base(container) { } protected override TestCollectionBuilderTransient This => this; - protected override ILifetime CollectionLifetime => null; // transient + protected override Lifetime CollectionLifetime => Lifetime.Transient; // transient } // ReSharper disable once ClassNeverInstantiated.Local private class TestCollectionBuilderScope : OrderedCollectionBuilderBase { - public TestCollectionBuilderScope(IServiceContainer container) + public TestCollectionBuilderScope(IContainer container) : base(container) { } protected override TestCollectionBuilderScope This => this; - protected override ILifetime CollectionLifetime => new PerScopeLifetime(); + protected override Lifetime CollectionLifetime => Lifetime.Scope; } // ReSharper disable once ClassNeverInstantiated.Local private class TestCollectionBuilderWeighted : WeightedCollectionBuilderBase { - public TestCollectionBuilderWeighted(IServiceContainer container) + public TestCollectionBuilderWeighted(IContainer container) : base(container) { } diff --git a/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs b/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs index a9b7dd5ba0..97dde9e43a 100644 --- a/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs +++ b/src/Umbraco.Tests/Composing/LazyCollectionBuilderTests.cs @@ -1,11 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using LightInject; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Composing; -using Umbraco.Core.Composing.LightInject; namespace Umbraco.Tests.Composing { @@ -24,6 +22,9 @@ namespace Umbraco.Tests.Composing Current.Reset(); } + private IContainer CreateContainer() + => Current.Container = (new Core.Composing.LightInject.LightInjectContainer(new LightInject.ServiceContainer())).ConfigureForUmbraco(); + // note // lazy collection builder does not throw on duplicate, just uses distinct types // so we don't have a test for duplicates as we had with resolvers in v7 @@ -31,9 +32,7 @@ namespace Umbraco.Tests.Composing [Test] public void LazyCollectionBuilderHandlesTypes() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - Current.Container = new LightInjectContainer(container); + var container = CreateContainer(); container.RegisterCollectionBuilder() .Add() @@ -56,9 +55,7 @@ namespace Umbraco.Tests.Composing [Test] public void LazyCollectionBuilderHandlesProducers() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - Current.Container = new LightInjectContainer(container); + var container = CreateContainer(); container.RegisterCollectionBuilder() .Add(() => new[] { typeof(TransientObject3), typeof(TransientObject2) }) @@ -80,9 +77,7 @@ namespace Umbraco.Tests.Composing [Test] public void LazyCollectionBuilderHandlesTypesAndProducers() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - Current.Container = new LightInjectContainer(container); + var container = CreateContainer(); container.RegisterCollectionBuilder() .Add() @@ -105,9 +100,7 @@ namespace Umbraco.Tests.Composing [Test] public void LazyCollectionBuilderThrowsOnIllegalTypes() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - Current.Container = new LightInjectContainer(container); + var container = CreateContainer(); container.RegisterCollectionBuilder() .Add() @@ -128,9 +121,7 @@ namespace Umbraco.Tests.Composing [Test] public void LazyCollectionBuilderCanExcludeTypes() { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - Current.Container = new LightInjectContainer(container); + var container = CreateContainer(); container.RegisterCollectionBuilder() .Add() @@ -171,13 +162,13 @@ namespace Umbraco.Tests.Composing // ReSharper disable once ClassNeverInstantiated.Local private class TestCollectionBuilder : LazyCollectionBuilderBase { - public TestCollectionBuilder(IServiceContainer container) + public TestCollectionBuilder(IContainer container) : base(container) { } protected override TestCollectionBuilder This => this; - protected override ILifetime CollectionLifetime => null; // transient + protected override Lifetime CollectionLifetime => Lifetime.Transient; // transient } // ReSharper disable once ClassNeverInstantiated.Local diff --git a/src/Umbraco.Tests/Integration/ContentEventsTests.cs b/src/Umbraco.Tests/Integration/ContentEventsTests.cs index 05708a88a3..4356d3733f 100644 --- a/src/Umbraco.Tests/Integration/ContentEventsTests.cs +++ b/src/Umbraco.Tests/Integration/ContentEventsTests.cs @@ -54,7 +54,7 @@ namespace Umbraco.Tests.Integration base.Compose(); Container.Register(_ => new TestServerRegistrar()); // localhost-only - Container.Register(new PerContainerLifetime()); + Container.RegisterSingleton(); Container.RegisterCollectionBuilder() .Add() diff --git a/src/Umbraco.Tests/Issues/U9560.cs b/src/Umbraco.Tests/Issues/U9560.cs index c5748e21e0..34cf23bbb8 100644 --- a/src/Umbraco.Tests/Issues/U9560.cs +++ b/src/Umbraco.Tests/Issues/U9560.cs @@ -3,7 +3,7 @@ using NUnit.Framework; using Umbraco.Core.Models; using Umbraco.Core.Persistence; using Umbraco.Tests.Testing; -using LightInject; +using Umbraco.Core; using Umbraco.Tests.TestHelpers; namespace Umbraco.Tests.Issues diff --git a/src/Umbraco.Tests/Models/ContentTests.cs b/src/Umbraco.Tests/Models/ContentTests.cs index ba1a4e739c..7704854471 100644 --- a/src/Umbraco.Tests/Models/ContentTests.cs +++ b/src/Umbraco.Tests/Models/ContentTests.cs @@ -6,7 +6,7 @@ using System.IO; using System.Linq; using System.Web; using Moq; -using LightInject; +using Umbraco.Core; using NUnit.Framework; using Umbraco.Core.Cache; using Umbraco.Core.Configuration.UmbracoSettings; diff --git a/src/Umbraco.Tests/Models/Mapping/AutoMapperTests.cs b/src/Umbraco.Tests/Models/Mapping/AutoMapperTests.cs index 349804a432..0196a86b9a 100644 --- a/src/Umbraco.Tests/Models/Mapping/AutoMapperTests.cs +++ b/src/Umbraco.Tests/Models/Mapping/AutoMapperTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using AutoMapper; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Manifest; using Umbraco.Core.PropertyEditors; diff --git a/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs index 8e8306095f..04a000b4f3 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/DictionaryRepositoryTest.cs @@ -6,8 +6,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Persistence.Repositories; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.Testing; -using LightInject; -using Umbraco.Core.Scoping; +using Umbraco.Core; namespace Umbraco.Tests.Persistence.Repositories { diff --git a/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs b/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs index 79cb1dd4f1..99b5972c45 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs @@ -1,13 +1,11 @@ using System.Linq; using Moq; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.Models; -using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.PropertyEditors; -using Umbraco.Core.Composing; using Umbraco.Core.Persistence.Repositories.Implement; -using Umbraco.Core.Scoping; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.Testing; diff --git a/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs index e414fc1c3a..2f346ff976 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs @@ -3,8 +3,8 @@ using System.Linq; using System.Text; using Moq; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; -using Umbraco.Core.Composing; using Umbraco.Core.IO; using Umbraco.Core.Models; using Umbraco.Core.Persistence.Repositories.Implement; @@ -68,7 +68,7 @@ namespace Umbraco.Tests.Persistence.Repositories // Act var script = new Script("test-add-script.js") { Content = "/// " }; repository.Save(script); - + //Assert Assert.That(_fileSystem.FileExists("test-add-script.js"), Is.True); @@ -87,11 +87,11 @@ namespace Umbraco.Tests.Persistence.Repositories // Act var script = new Script("test-updated-script.js") { Content = "/// " }; repository.Save(script); - + script.Content = "/// "; repository.Save(script); - + var scriptUpdated = repository.Get("test-updated-script.js"); @@ -113,7 +113,7 @@ namespace Umbraco.Tests.Persistence.Repositories // Act var script = repository.Get("test-script.js"); repository.Delete(script); - + // Assert @@ -155,7 +155,7 @@ namespace Umbraco.Tests.Persistence.Repositories repository.Save(script2); var script3 = new Script("test-script3.js") { Content = "/// " }; repository.Save(script3); - + // Act var scripts = repository.GetMany(); @@ -183,7 +183,7 @@ namespace Umbraco.Tests.Persistence.Repositories repository.Save(script2); var script3 = new Script("test-script3.js") { Content = "/// " }; repository.Save(script3); - + // Act var scripts = repository.GetMany("test-script1.js", "test-script2.js"); @@ -226,13 +226,13 @@ namespace Umbraco.Tests.Persistence.Repositories var script = new Script("test-move-script.js") { Content = content }; repository.Save(script); - + // Act script = repository.Get("test-move-script.js"); script.Path = "moved/test-move-script.js"; repository.Save(script); - + var existsOld = repository.Exists("test-move-script.js"); var existsNew = repository.Exists("moved/test-move-script.js"); @@ -259,7 +259,7 @@ namespace Umbraco.Tests.Persistence.Repositories var script = new Script("test-path-1.js") { Content = "// script" }; repository.Save(script); - + Assert.IsTrue(_fileSystem.FileExists("test-path-1.js")); Assert.AreEqual("test-path-1.js", script.Path); Assert.AreEqual("/scripts/test-path-1.js", script.VirtualPath); @@ -267,14 +267,14 @@ namespace Umbraco.Tests.Persistence.Repositories //ensure you can prefix the same path as the root path name script = new Script("scripts/path-2/test-path-2.js") { Content = "// script" }; repository.Save(script); - + Assert.IsTrue(_fileSystem.FileExists("scripts/path-2/test-path-2.js")); Assert.AreEqual("scripts\\path-2\\test-path-2.js", script.Path); Assert.AreEqual("/scripts/scripts/path-2/test-path-2.js", script.VirtualPath); script = new Script("path-2/test-path-2.js") { Content = "// script" }; repository.Save(script); - + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.js")); Assert.AreEqual("path-2\\test-path-2.js", script.Path); // fixed in 7.3 - 7.2.8 does not update the path Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath); @@ -286,7 +286,7 @@ namespace Umbraco.Tests.Persistence.Repositories script = new Script("path-2\\test-path-3.js") { Content = "// script" }; repository.Save(script); - + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.js")); Assert.AreEqual("path-2\\test-path-3.js", script.Path); Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); diff --git a/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs b/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs index 987fa0a869..a4bc1e9a12 100644 --- a/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs +++ b/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs @@ -1,9 +1,9 @@ -using System; -using Moq; +using Moq; using NUnit.Framework; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.Routing; +using Umbraco.Core; namespace Umbraco.Tests.Routing { diff --git a/src/Umbraco.Tests/Routing/UrlProviderTests.cs b/src/Umbraco.Tests/Routing/UrlProviderTests.cs index 2f1e4e3476..f5637118c3 100644 --- a/src/Umbraco.Tests/Routing/UrlProviderTests.cs +++ b/src/Umbraco.Tests/Routing/UrlProviderTests.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using Moq; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; diff --git a/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs b/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs index 6965c4dce9..bf94d2ffbf 100644 --- a/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs +++ b/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs @@ -2,7 +2,7 @@ using System.Linq; using Moq; using NUnit.Framework; -using Umbraco.Core.Composing; +using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; diff --git a/src/Umbraco.Tests/Routing/UrlsProviderWithDomainsTests.cs b/src/Umbraco.Tests/Routing/UrlsProviderWithDomainsTests.cs index c6bd0fdb88..face9b8c05 100644 --- a/src/Umbraco.Tests/Routing/UrlsProviderWithDomainsTests.cs +++ b/src/Umbraco.Tests/Routing/UrlsProviderWithDomainsTests.cs @@ -3,12 +3,12 @@ using System.Collections.Generic; using System.Linq; using Moq; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Umbraco.Web.Routing; -using Umbraco.Core.Composing; namespace Umbraco.Tests.Routing { diff --git a/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs b/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs index 6b2737a3d1..f23ca66f99 100644 --- a/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs +++ b/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs @@ -1,12 +1,12 @@ using System; using Moq; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Umbraco.Web.Routing; using Umbraco.Core.Services; -using Umbraco.Core.Composing; namespace Umbraco.Tests.Routing { diff --git a/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs b/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs index 6604e25aa2..4292cffdb0 100644 --- a/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs +++ b/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs @@ -6,6 +6,7 @@ using System.Web.Http; using Moq; using Newtonsoft.Json; using NUnit.Framework; +using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Core.Models; using Umbraco.Core.Models.Membership; @@ -41,7 +42,7 @@ namespace Umbraco.Tests.Web.Controllers // kill the true IEntityService too Container.RegisterSingleton(f => Mock.Of()); - + Container.RegisterSingleton(); } @@ -68,7 +69,6 @@ namespace Umbraco.Tests.Web.Controllers .Returns((int id) => id == 1234 ? new User(1234, "Test", "test@test.com", "test@test.com", "", new List(), new int[0], new int[0]) : null); var usersController = new UsersController(); - Container.InjectProperties(usersController); return usersController; } @@ -125,7 +125,6 @@ namespace Umbraco.Tests.Web.Controllers ApiController Factory(HttpRequestMessage message, UmbracoHelper helper) { var usersController = new UsersController(); - Container.InjectProperties(usersController); return usersController; } @@ -153,7 +152,6 @@ namespace Umbraco.Tests.Web.Controllers .Returns(() => users); var usersController = new UsersController(); - Container.InjectProperties(usersController); return usersController; } diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs index 22bf9ef602..bd68325e06 100644 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs @@ -95,7 +95,6 @@ namespace Umbraco.Tests.Web.Mvc var controller = new TestSurfaceController(umbracoContext); Container.Register(_ => umbracoContext); - Container.InjectProperties(controller); Assert.IsNotNull(controller.Umbraco); } @@ -187,7 +186,7 @@ namespace Umbraco.Tests.Web.Mvc : base(ctx, null, new ServiceContext(), Mock.Of(), null, null) { if (helper != null) - { + { Umbraco = helper; } } diff --git a/src/Umbraco.Web/Composing/LightInject/LightInjectContainer.cs b/src/Umbraco.Web/Composing/LightInject/LightInjectContainer.cs index 1cc30be73f..cf6a949fac 100644 --- a/src/Umbraco.Web/Composing/LightInject/LightInjectContainer.cs +++ b/src/Umbraco.Web/Composing/LightInject/LightInjectContainer.cs @@ -2,6 +2,7 @@ using System.Web.Http; using LightInject; using Umbraco.Core.Composing; +using Umbraco.Core.Composing.LightInject; namespace Umbraco.Web.Composing.LightInject { @@ -18,7 +19,7 @@ namespace Umbraco.Web.Composing.LightInject { } /// - public override void ConfigureForWeb() + public override IContainer ConfigureForWeb() { // IoC setup for LightInject for MVC/WebApi // see comments on MixedLightInjectScopeManagerProvider for explainations of what we are doing here @@ -27,6 +28,8 @@ namespace Umbraco.Web.Composing.LightInject Container.EnableMvc(); // does container.EnablePerWebRequestScope() Container.ScopeManagerProvider = smp; // reverts - we will do it last (in WebRuntime) Container.EnableWebApi(GlobalConfiguration.Configuration); + + return this; } } } diff --git a/src/Umbraco.Web/Editors/EditorValidatorCollectionBuilder.cs b/src/Umbraco.Web/Editors/EditorValidatorCollectionBuilder.cs index e05c85a1b6..3189f2e71f 100644 --- a/src/Umbraco.Web/Editors/EditorValidatorCollectionBuilder.cs +++ b/src/Umbraco.Web/Editors/EditorValidatorCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Web.Editors { internal class EditorValidatorCollectionBuilder : LazyCollectionBuilderBase { - public EditorValidatorCollectionBuilder(IServiceContainer container) + public EditorValidatorCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckNotificationMethodCollectionBuilder.cs b/src/Umbraco.Web/HealthCheck/HealthCheckNotificationMethodCollectionBuilder.cs index 4f958da9a0..76a93fe217 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckNotificationMethodCollectionBuilder.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckNotificationMethodCollectionBuilder.cs @@ -1,12 +1,11 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; using Umbraco.Web.HealthCheck.NotificationMethods; namespace Umbraco.Web.HealthCheck { internal class HealthCheckNotificationMethodCollectionBuilder : LazyCollectionBuilderBase { - public HealthCheckNotificationMethodCollectionBuilder(IServiceContainer container) + public HealthCheckNotificationMethodCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Web/HealthCheck/HeathCheckCollectionBuilder.cs b/src/Umbraco.Web/HealthCheck/HeathCheckCollectionBuilder.cs index 9528f9a9dd..7e600d9238 100644 --- a/src/Umbraco.Web/HealthCheck/HeathCheckCollectionBuilder.cs +++ b/src/Umbraco.Web/HealthCheck/HeathCheckCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Web.HealthCheck { public class HealthCheckCollectionBuilder : LazyCollectionBuilderBase { - public HealthCheckCollectionBuilder(IServiceContainer container) + public HealthCheckCollectionBuilder(IContainer container) : base(container) { } @@ -13,6 +12,6 @@ namespace Umbraco.Web.HealthCheck // note: in v7 they were per-request, not sure why? // the collection is injected into the controller & there's only 1 controller per request anyways - protected override ILifetime CollectionLifetime => null; // transient! + protected override Lifetime CollectionLifetime => Lifetime.Transient; // transient! } } diff --git a/src/Umbraco.Web/Mvc/FilteredControllerFactoryCollectionBuilder.cs b/src/Umbraco.Web/Mvc/FilteredControllerFactoryCollectionBuilder.cs index 930b5a1eb2..4ec48bac3f 100644 --- a/src/Umbraco.Web/Mvc/FilteredControllerFactoryCollectionBuilder.cs +++ b/src/Umbraco.Web/Mvc/FilteredControllerFactoryCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Web.Mvc { public class FilteredControllerFactoryCollectionBuilder : OrderedCollectionBuilderBase { - public FilteredControllerFactoryCollectionBuilder(IServiceContainer container) + public FilteredControllerFactoryCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Web/Routing/ContentFinderCollectionBuilder.cs b/src/Umbraco.Web/Routing/ContentFinderCollectionBuilder.cs index 7d9ef952c4..47186dbb74 100644 --- a/src/Umbraco.Web/Routing/ContentFinderCollectionBuilder.cs +++ b/src/Umbraco.Web/Routing/ContentFinderCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Web.Routing { public class ContentFinderCollectionBuilder : OrderedCollectionBuilderBase { - public ContentFinderCollectionBuilder(IServiceContainer container) + public ContentFinderCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Web/Routing/UrlProviderCollectionBuilder.cs b/src/Umbraco.Web/Routing/UrlProviderCollectionBuilder.cs index 196c8a7a25..d66a7554b8 100644 --- a/src/Umbraco.Web/Routing/UrlProviderCollectionBuilder.cs +++ b/src/Umbraco.Web/Routing/UrlProviderCollectionBuilder.cs @@ -1,11 +1,10 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; namespace Umbraco.Web.Routing { public class UrlProviderCollectionBuilder : OrderedCollectionBuilderBase { - public UrlProviderCollectionBuilder(IServiceContainer container) + public UrlProviderCollectionBuilder(IContainer container) : base(container) { } diff --git a/src/Umbraco.Web/Search/SearchableTreeCollectionBuilder.cs b/src/Umbraco.Web/Search/SearchableTreeCollectionBuilder.cs index ae83cc5eab..e64738329b 100644 --- a/src/Umbraco.Web/Search/SearchableTreeCollectionBuilder.cs +++ b/src/Umbraco.Web/Search/SearchableTreeCollectionBuilder.cs @@ -1,5 +1,4 @@ -using LightInject; -using Umbraco.Core.Composing; +using Umbraco.Core.Composing; using Umbraco.Core.Services; using Umbraco.Web.Trees; @@ -9,7 +8,7 @@ namespace Umbraco.Web.Search { private readonly IApplicationTreeService _treeService; - public SearchableTreeCollectionBuilder(IServiceContainer container, IApplicationTreeService treeService) + public SearchableTreeCollectionBuilder(IContainer container, IApplicationTreeService treeService) : base(container) { _treeService = treeService; diff --git a/src/Umbraco.Web/Tour/TourFilterCollectionBuilder.cs b/src/Umbraco.Web/Tour/TourFilterCollectionBuilder.cs index 5084975bbd..f521b91f26 100644 --- a/src/Umbraco.Web/Tour/TourFilterCollectionBuilder.cs +++ b/src/Umbraco.Web/Tour/TourFilterCollectionBuilder.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; -using LightInject; using Umbraco.Core; using Umbraco.Core.Composing; @@ -18,14 +17,14 @@ namespace Umbraco.Web.Tour /// /// Initializes a new instance of the class. /// - public TourFilterCollectionBuilder(IServiceContainer container) + public TourFilterCollectionBuilder(IContainer container) : base(container) { } /// - protected override IEnumerable CreateItems(params object[] args) + protected override IEnumerable CreateItems(/*params object[] args*/) { - return base.CreateItems(args).Concat(_instances); + return base.CreateItems(/*args*/).Concat(_instances); } ///