diff --git a/src/Autofac/Builder/RegistrationExtensions.cs b/src/Autofac/Builder/RegistrationExtensions.cs index ca5723656..c0641a3fd 100644 --- a/src/Autofac/Builder/RegistrationExtensions.cs +++ b/src/Autofac/Builder/RegistrationExtensions.cs @@ -25,6 +25,7 @@ public static class RegistrationExtensions /// Registration builder allowing the registration to be configured. /// Factory delegates are provided automatically in Autofac 2, /// and this method is generally not required. + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder RegisterGeneratedFactory(this ContainerBuilder builder, Type delegateType) { @@ -48,6 +49,7 @@ public static IRegistrationBuilderRegistration builder allowing the registration to be configured. /// Factory delegates are provided automatically in Autofac 2, and /// this method is generally not required. + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder RegisterGeneratedFactory(this ContainerBuilder builder, Type delegateType, Service service) { @@ -68,6 +70,7 @@ public static IRegistrationBuilderRegistration builder allowing the registration to be configured. /// Factory delegates are provided automatically in Autofac 2, /// and this method is generally not required. + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder RegisterGeneratedFactory(this ContainerBuilder builder, Service service) where TDelegate : class @@ -88,6 +91,7 @@ public static IRegistrationBuilderRegistration builder allowing the registration to be configured. /// Factory delegates are provided automatically in Autofac 2, /// and this method is generally not required. + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder RegisterGeneratedFactory(this ContainerBuilder builder) where TDelegate : class @@ -115,6 +119,7 @@ public static IRegistrationBuilder /// Thrown if is . /// + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder NamedParameterMapping( this IRegistrationBuilder registration) @@ -142,6 +147,7 @@ public static IRegistrationBuilder /// Thrown if is . /// + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder PositionalParameterMapping( this IRegistrationBuilder registration) @@ -169,6 +175,7 @@ public static IRegistrationBuilder /// Thrown if is . /// + [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public static IRegistrationBuilder TypedParameterMapping( this IRegistrationBuilder registration) diff --git a/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs b/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs index 5740e2b9e..bd0c5e43b 100644 --- a/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs +++ b/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs @@ -177,20 +177,12 @@ public void AddImplementation(IComponentRegistration registration, bool preserve { if (originatedFromSource) { - if (_sourceImplementations == null) - { - _sourceImplementations = new List(); - } - + _sourceImplementations ??= new List(); _sourceImplementations.Add(registration); } else { - if (_preserveDefaultImplementations == null) - { - _preserveDefaultImplementations = new List(); - } - + _preserveDefaultImplementations ??= new List(); _preserveDefaultImplementations.Add(registration); } } @@ -214,11 +206,7 @@ public void AddImplementation(IComponentRegistration registration, bool preserve /// The insertion mode for the pipeline. public void UseServiceMiddleware(IResolveMiddleware middleware, MiddlewareInsertionMode insertionMode = MiddlewareInsertionMode.EndOfPhase) { - if (_customPipelineBuilder is null) - { - _customPipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service); - } - + _customPipelineBuilder ??= new ResolvePipelineBuilder(PipelineType.Service); _customPipelineBuilder.Use(middleware, insertionMode); } @@ -234,11 +222,7 @@ public void UseServiceMiddlewareRange(IEnumerable middleware return; } - if (_customPipelineBuilder is null) - { - _customPipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service); - } - + _customPipelineBuilder ??= new ResolvePipelineBuilder(PipelineType.Service); _customPipelineBuilder.UseRange(middleware, insertionMode); } @@ -280,10 +264,7 @@ public void BeginInitialization(IEnumerable sources) // Build the pipeline during service info initialization, so that sources can access it // while getting a registration recursively. - if (_resolvePipeline is null) - { - _resolvePipeline = BuildPipeline(); - } + _resolvePipeline ??= BuildPipeline(); } /// diff --git a/src/Autofac/Core/Resolving/SegmentedStack.cs b/src/Autofac/Core/Resolving/SegmentedStack.cs index 5833d58c2..e75b774d2 100644 --- a/src/Autofac/Core/Resolving/SegmentedStack.cs +++ b/src/Autofac/Core/Resolving/SegmentedStack.cs @@ -112,7 +112,7 @@ IEnumerator IEnumerable.GetEnumerator() return new Enumerator(this); } - private struct StackSegment : IDisposable + private readonly struct StackSegment : IDisposable { private readonly SegmentedStack _stack; private readonly int _resetPosition; diff --git a/src/Autofac/Core/ServiceRegistration.cs b/src/Autofac/Core/ServiceRegistration.cs index 63935cc57..2a51d0e8e 100644 --- a/src/Autofac/Core/ServiceRegistration.cs +++ b/src/Autofac/Core/ServiceRegistration.cs @@ -9,7 +9,7 @@ namespace Autofac.Core; /// /// Defines a combination of a service pipeline and a registration. Used to instantiate a . /// -public struct ServiceRegistration : IEquatable +public readonly struct ServiceRegistration : IEquatable { /// /// Initializes a new instance of the struct. diff --git a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs index 2f815ed20..2fbd78b83 100644 --- a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs +++ b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs @@ -10,6 +10,7 @@ namespace Autofac.Features.GeneratedFactories; /// /// Data used to create factory activators. /// +[Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")] public class GeneratedFactoryActivatorData : IConcreteActivatorData { private readonly Type _delegateType; diff --git a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs index 131aa9fd7..8944ec3eb 100644 --- a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs +++ b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs @@ -6,6 +6,8 @@ namespace Autofac.Features.GeneratedFactories; +#pragma warning disable CS0618 + /// /// Helper methods for registering factories. /// diff --git a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs index 2075f8dfd..09bb80db7 100644 --- a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs +++ b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs @@ -96,16 +96,14 @@ private static void ScanAssemblies(IEnumerable assemblies, IComponentR .Where(t => t.IsGenericTypeDefinition) .CanBeRegistered(rb.ActivatorData); - Func> scannedConstructor = - (type) => new RegistrationBuilder( + static IRegistrationBuilder TypeBuilderFactory(Type type) => new RegistrationBuilder( new TypedService(type), new ReflectionActivatorData(type), new DynamicRegistrationStyle()); - Action> register = - (cr, scanned) => cr.AddRegistrationSource(new OpenGenericRegistrationSource(scanned.RegistrationData, scanned.ResolvePipeline, scanned.ActivatorData)); + static void RegistrationSourceFactory(IComponentRegistryBuilder registry, IRegistrationBuilder data) => registry.AddRegistrationSource(new OpenGenericRegistrationSource(data.RegistrationData, data.ResolvePipeline, data.ActivatorData)); - ScanTypesTemplate(types, cr, rb, scannedConstructor, register); + ScanTypesTemplate(types, cr, rb, TypeBuilderFactory, RegistrationSourceFactory); } private static void ScanTypesTemplate( diff --git a/src/Autofac/Features/Scanning/ScanningRegistrationExtensions.cs b/src/Autofac/Features/Scanning/ScanningRegistrationExtensions.cs index 133178e4e..22a65906b 100644 --- a/src/Autofac/Features/Scanning/ScanningRegistrationExtensions.cs +++ b/src/Autofac/Features/Scanning/ScanningRegistrationExtensions.cs @@ -86,13 +86,11 @@ private static void ScanTypes(IEnumerable types, IComponentRegistryBuilder rb.RegistrationData.Services.OfType().All(swt => swt.ServiceType.IsAssignableFrom(t))); - Func> scannedConstructor = - (type) => RegistrationBuilder.ForType(type); + static IRegistrationBuilder TypeBuilderFactory(Type type) => RegistrationBuilder.ForType(type); - Action> register = - (cr, scanned) => RegistrationBuilder.RegisterSingleComponent(cr, scanned); + static void SingleComponentRegistration(IComponentRegistryBuilder registry, IRegistrationBuilder data) => RegistrationBuilder.RegisterSingleComponent(registry, data); - ScanTypesTemplate(closedTypes, cr, rb, scannedConstructor, register); + ScanTypesTemplate(closedTypes, cr, rb, TypeBuilderFactory, SingleComponentRegistration); } /// diff --git a/src/Autofac/ModuleRegistrationExtensions.cs b/src/Autofac/ModuleRegistrationExtensions.cs index c82eec66a..54774f447 100644 --- a/src/Autofac/ModuleRegistrationExtensions.cs +++ b/src/Autofac/ModuleRegistrationExtensions.cs @@ -278,15 +278,15 @@ public static IModuleRegistrar OnlyIf(this IModuleRegistrar registrar, Predicate var registrarCallback = registrar.RegistrarData.Callback; var original = registrarCallback.Callback; - Action updated = registry => + void Updated(IComponentRegistryBuilder registry) { if (predicate(registry)) { original(registry); } - }; + } - registrarCallback.Callback = updated; + registrarCallback.Callback = Updated; return registrar; } diff --git a/src/Autofac/RegistrationExtensions.Conditional.cs b/src/Autofac/RegistrationExtensions.Conditional.cs index 549a63ef3..0f1ba1f92 100644 --- a/src/Autofac/RegistrationExtensions.Conditional.cs +++ b/src/Autofac/RegistrationExtensions.Conditional.cs @@ -53,15 +53,15 @@ public static IRegistrationBuilder } var original = c.Callback; - Action updated = registry => + void Updated(IComponentRegistryBuilder registry) { if (predicate(registry)) { original(registry); } - }; + } - c.Callback = updated; + c.Callback = Updated; return registration; } diff --git a/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs b/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs index 1fffc6ea7..9c20e2e53 100644 --- a/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs +++ b/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs @@ -237,10 +237,12 @@ public void OnlyIf_EnabledByStandardRegistrations() builder.RegisterDecorator((ctx, p, s) => new Decorator(s), "from").OnlyIf(r => true); builder.RegisterDecorator((ctx, s) => new Decorator(s), "from").OnlyIf(r => true); builder.RegisterDecorator(s => new Decorator(s), "from").OnlyIf(r => true); +#pragma warning disable CS0618 builder.RegisterGeneratedFactory(typeof(SimpleFactory)).OnlyIf(r => true); builder.RegisterGeneratedFactory(typeof(SimpleFactory), new TypedService(typeof(object))).OnlyIf(r => true); builder.RegisterGeneratedFactory().OnlyIf(r => true); builder.RegisterGeneratedFactory(new TypedService(typeof(object))).OnlyIf(r => true); +#pragma warning restore CS0618 builder.RegisterGeneric(typeof(SimpleGeneric<>)).OnlyIf(r => true); builder.RegisterGenericDecorator(typeof(Decorator<>), typeof(IService<>), fromKey: "b").OnlyIf(r => true); builder.RegisterInstance(new object()).OnlyIf(r => true); diff --git a/test/Autofac.Specification.Test/Resolution/ConstructorFinderTests.cs b/test/Autofac.Specification.Test/Resolution/ConstructorFinderTests.cs index ae48de229..949bcbba8 100644 --- a/test/Autofac.Specification.Test/Resolution/ConstructorFinderTests.cs +++ b/test/Autofac.Specification.Test/Resolution/ConstructorFinderTests.cs @@ -92,13 +92,14 @@ public void FindConstructorsWith_FinderFunctionProvided_PassedToConstructorFinde { var cb = new ContainerBuilder(); var finderCalled = false; - Func finder = type => + ConstructorInfo[] Finder(Type type) { finderCalled = true; return type.GetConstructors(); - }; + } + cb.RegisterType(); - cb.RegisterType().FindConstructorsWith(finder); + cb.RegisterType().FindConstructorsWith(Finder); var container = cb.Build(); container.Resolve(); diff --git a/test/Autofac.Test/Concurrency/ConcurrencyTests.cs b/test/Autofac.Test/Concurrency/ConcurrencyTests.cs index 4c2802c65..5138f743b 100644 --- a/test/Autofac.Test/Concurrency/ConcurrencyTests.cs +++ b/test/Autofac.Test/Concurrency/ConcurrencyTests.cs @@ -106,7 +106,7 @@ public void WhenTwoThreadsInitializeASharedInstanceSimultaneouslyViaChildLifetim var container = builder.Build(); - ThreadStart work = () => + void Work() { try { @@ -117,10 +117,10 @@ public void WhenTwoThreadsInitializeASharedInstanceSimultaneouslyViaChildLifetim { exceptions.Add(ex); } - }; + } - var t1 = new Thread(work); - var t2 = new Thread(work); + var t1 = new Thread(Work); + var t2 = new Thread(Work); t1.Start(); t2.Start(); t1.Join(); diff --git a/test/Autofac.Test/Core/Registration/ComponentRegistryTests.cs b/test/Autofac.Test/Core/Registration/ComponentRegistryTests.cs index aa7fe6353..cb0a1e40c 100644 --- a/test/Autofac.Test/Core/Registration/ComponentRegistryTests.cs +++ b/test/Autofac.Test/Core/Registration/ComponentRegistryTests.cs @@ -318,9 +318,9 @@ public void AddingConcreteImplementationWhenAdapterImplementationsExist_AddsChai var pre = container.ComponentRegistry.RegistrationsFor(chainedService); Assert.Single(pre); - Func func = () => new object(); + static object ObjectFactory() => new(); using (var lifetimeScope = container.BeginLifetimeScope(builder => - builder.ComponentRegistryBuilder.Register(RegistrationBuilder.ForDelegate((c, p) => func).CreateRegistration()))) + builder.ComponentRegistryBuilder.Register(RegistrationBuilder.ForDelegate((c, p) => (Func)ObjectFactory).CreateRegistration()))) { var post = lifetimeScope.ComponentRegistry.RegistrationsFor(chainedService); Assert.Equal(2, post.Count()); diff --git a/test/Autofac.Test/Features/GeneratedFactories/GeneratedFactoriesTests.cs b/test/Autofac.Test/Features/GeneratedFactories/GeneratedFactoriesTests.cs index 0e04a5581..55b7f49cd 100644 --- a/test/Autofac.Test/Features/GeneratedFactories/GeneratedFactoriesTests.cs +++ b/test/Autofac.Test/Features/GeneratedFactories/GeneratedFactoriesTests.cs @@ -6,6 +6,8 @@ namespace Autofac.Test.Features.GeneratedFactories; +#pragma warning disable CS0618 + public class GeneratedFactoriesTests { public class A @@ -164,7 +166,7 @@ public void RespectsContexts() } [Fact] - public void CanSetParmeterMappingToPositional() + public void CanSetParameterMappingToPositional() { var builder = new ContainerBuilder(); @@ -259,7 +261,7 @@ public void WillNotAutoGenerateFactoriesWhenProductNotRegistered() } [Fact] - public void CreateGenericFromNongenericFactoryDelegate() + public void CreateGenericFromNonGenericFactoryDelegate() { var builder = new ContainerBuilder(); @@ -278,7 +280,7 @@ public void CreateGenericFromNongenericFactoryDelegate() } [Fact] - public void CreateGenericFromNongenericFactoryDelegateImpliedServiceType() + public void CreateGenericFromNonGenericFactoryDelegateImpliedServiceType() { var builder = new ContainerBuilder(); diff --git a/test/Autofac.Test/Features/OwnedInstances/OwnedInstanceRegistrationSourceTests.cs b/test/Autofac.Test/Features/OwnedInstances/OwnedInstanceRegistrationSourceTests.cs index 743c885f0..df3e4e96b 100644 --- a/test/Autofac.Test/Features/OwnedInstances/OwnedInstanceRegistrationSourceTests.cs +++ b/test/Autofac.Test/Features/OwnedInstances/OwnedInstanceRegistrationSourceTests.cs @@ -38,6 +38,7 @@ public void CallingDisposeOnGeneratedOwnedT_DoesNotDisposeCurrentLifetimeScope() Assert.False(containerDisposeTracker.IsDisposed); } +#pragma warning disable CS0618 [Fact] public void CanResolveAndUse_OwnedGeneratedFactory() { @@ -55,6 +56,7 @@ public void CanResolveAndUse_OwnedGeneratedFactory() Assert.True(isAccessed); } +#pragma warning restore CS0618 [Fact] public void IfInnerTypeIsNotRegistered_OwnedTypeIsNotEither()