Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark RegisterGeneratedFactory obsolete #1366

Merged
merged 6 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Autofac/Builder/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class RegistrationExtensions
/// <returns>Registration builder allowing the registration to be configured.</returns>
/// <remarks>Factory delegates are provided automatically in Autofac 2,
/// and this method is generally not required.</remarks>
[Obsolete("Update your code to use the Func<T> 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<Delegate, GeneratedFactoryActivatorData, SingleRegistrationStyle>
RegisterGeneratedFactory(this ContainerBuilder builder, Type delegateType)
{
Expand All @@ -48,6 +49,7 @@ public static IRegistrationBuilder<Delegate, GeneratedFactoryActivatorData, Sing
/// <returns>Registration builder allowing the registration to be configured.</returns>
/// <remarks>Factory delegates are provided automatically in Autofac 2, and
/// this method is generally not required.</remarks>
[Obsolete("Update your code to use the Func<T> 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<Delegate, GeneratedFactoryActivatorData, SingleRegistrationStyle>
RegisterGeneratedFactory(this ContainerBuilder builder, Type delegateType, Service service)
{
Expand All @@ -68,6 +70,7 @@ public static IRegistrationBuilder<Delegate, GeneratedFactoryActivatorData, Sing
/// <returns>Registration builder allowing the registration to be configured.</returns>
/// <remarks>Factory delegates are provided automatically in Autofac 2,
/// and this method is generally not required.</remarks>
[Obsolete("Update your code to use the Func<T> 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<TDelegate, GeneratedFactoryActivatorData, SingleRegistrationStyle>
RegisterGeneratedFactory<TDelegate>(this ContainerBuilder builder, Service service)
where TDelegate : class
Expand All @@ -88,6 +91,7 @@ public static IRegistrationBuilder<TDelegate, GeneratedFactoryActivatorData, Sin
/// <returns>Registration builder allowing the registration to be configured.</returns>
/// <remarks>Factory delegates are provided automatically in Autofac 2,
/// and this method is generally not required.</remarks>
[Obsolete("Update your code to use the Func<T> 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<TDelegate, GeneratedFactoryActivatorData, SingleRegistrationStyle>
RegisterGeneratedFactory<TDelegate>(this ContainerBuilder builder)
where TDelegate : class
Expand Down Expand Up @@ -115,6 +119,7 @@ public static IRegistrationBuilder<TDelegate, GeneratedFactoryActivatorData, Sin
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="registration" /> is <see langword="null" />.
/// </exception>
[Obsolete("Update your code to use the Func<T> 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<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle>
NamedParameterMapping<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle>(
this IRegistrationBuilder<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle> registration)
Expand Down Expand Up @@ -142,6 +147,7 @@ public static IRegistrationBuilder<TDelegate, TGeneratedFactoryActivatorData, TS
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="registration" /> is <see langword="null" />.
/// </exception>
[Obsolete("Update your code to use the Func<T> 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<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle>
PositionalParameterMapping<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle>(
this IRegistrationBuilder<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle> registration)
Expand Down Expand Up @@ -169,6 +175,7 @@ public static IRegistrationBuilder<TDelegate, TGeneratedFactoryActivatorData, TS
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="registration" /> is <see langword="null" />.
/// </exception>
[Obsolete("Update your code to use the Func<T> 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<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle>
TypedParameterMapping<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle>(
this IRegistrationBuilder<TDelegate, TGeneratedFactoryActivatorData, TSingleRegistrationStyle> registration)
Expand Down
29 changes: 5 additions & 24 deletions src/Autofac/Core/Registration/ServiceRegistrationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,12 @@ public void AddImplementation(IComponentRegistration registration, bool preserve
{
if (originatedFromSource)
{
if (_sourceImplementations == null)
{
_sourceImplementations = new List<IComponentRegistration>();
}

_sourceImplementations ??= new List<IComponentRegistration>();
_sourceImplementations.Add(registration);
}
else
{
if (_preserveDefaultImplementations == null)
{
_preserveDefaultImplementations = new List<IComponentRegistration>();
}

_preserveDefaultImplementations ??= new List<IComponentRegistration>();
_preserveDefaultImplementations.Add(registration);
}
}
Expand All @@ -214,11 +206,7 @@ public void AddImplementation(IComponentRegistration registration, bool preserve
/// <param name="insertionMode">The insertion mode for the pipeline.</param>
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);
}

Expand All @@ -234,11 +222,7 @@ public void UseServiceMiddlewareRange(IEnumerable<IResolveMiddleware> middleware
return;
}

if (_customPipelineBuilder is null)
{
_customPipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
}

_customPipelineBuilder ??= new ResolvePipelineBuilder(PipelineType.Service);
_customPipelineBuilder.UseRange(middleware, insertionMode);
}

Expand Down Expand Up @@ -280,10 +264,7 @@ public void BeginInitialization(IEnumerable<IRegistrationSource> 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();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/Core/Resolving/SegmentedStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ IEnumerator IEnumerable.GetEnumerator()
return new Enumerator(this);
}

private struct StackSegment : IDisposable
private readonly struct StackSegment : IDisposable
{
private readonly SegmentedStack<T> _stack;
private readonly int _resetPosition;
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/Core/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Autofac.Core;
/// <summary>
/// Defines a combination of a service pipeline and a registration. Used to instantiate a <see cref="ResolveRequest"/>.
/// </summary>
public struct ServiceRegistration : IEquatable<ServiceRegistration>
public readonly struct ServiceRegistration : IEquatable<ServiceRegistration>
{
/// <summary>
/// Initializes a new instance of the <see cref="ServiceRegistration"/> struct.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Autofac.Features.GeneratedFactories;
/// <summary>
/// Data used to create factory activators.
/// </summary>
[Obsolete("Update your code to use the Func<T> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Autofac.Features.GeneratedFactories;

#pragma warning disable CS0618

/// <summary>
/// Helper methods for registering factories.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ private static void ScanAssemblies(IEnumerable<Assembly> assemblies, IComponentR
.Where(t => t.IsGenericTypeDefinition)
.CanBeRegistered(rb.ActivatorData);

Func<Type, IRegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle>> scannedConstructor =
(type) => new RegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle>(
static IRegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle> TypeBuilderFactory(Type type) => new RegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle>(
new TypedService(type),
new ReflectionActivatorData(type),
new DynamicRegistrationStyle());

Action<IComponentRegistryBuilder, IRegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle>> register =
(cr, scanned) => cr.AddRegistrationSource(new OpenGenericRegistrationSource(scanned.RegistrationData, scanned.ResolvePipeline, scanned.ActivatorData));
static void RegistrationSourceFactory(IComponentRegistryBuilder registry, IRegistrationBuilder<object, ReflectionActivatorData, DynamicRegistrationStyle> 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<TActivatorData, TScanStyle, TRegistrationBuilderStyle>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ private static void ScanTypes(IEnumerable<Type> types, IComponentRegistryBuilder
rb.RegistrationData.Services.OfType<IServiceWithType>().All(swt =>
swt.ServiceType.IsAssignableFrom(t)));

Func<Type, IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle>> scannedConstructor =
(type) => RegistrationBuilder.ForType(type);
static IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle> TypeBuilderFactory(Type type) => RegistrationBuilder.ForType(type);

Action<IComponentRegistryBuilder, IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle>> register =
(cr, scanned) => RegistrationBuilder.RegisterSingleComponent(cr, scanned);
static void SingleComponentRegistration(IComponentRegistryBuilder registry, IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle> data) => RegistrationBuilder.RegisterSingleComponent(registry, data);

ScanTypesTemplate(closedTypes, cr, rb, scannedConstructor, register);
ScanTypesTemplate(closedTypes, cr, rb, TypeBuilderFactory, SingleComponentRegistration);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Autofac/ModuleRegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ public static IModuleRegistrar OnlyIf(this IModuleRegistrar registrar, Predicate
var registrarCallback = registrar.RegistrarData.Callback;

var original = registrarCallback.Callback;
Action<IComponentRegistryBuilder> updated = registry =>
void Updated(IComponentRegistryBuilder registry)
{
if (predicate(registry))
{
original(registry);
}
};
}

registrarCallback.Callback = updated;
registrarCallback.Callback = Updated;

return registrar;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Autofac/RegistrationExtensions.Conditional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public static IRegistrationBuilder<TLimit, TActivatorData, TStyle>
}

var original = c.Callback;
Action<IComponentRegistryBuilder> updated = registry =>
void Updated(IComponentRegistryBuilder registry)
{
if (predicate(registry))
{
original(registry);
}
};
}

c.Callback = updated;
c.Callback = Updated;
return registration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,12 @@ public void OnlyIf_EnabledByStandardRegistrations()
builder.RegisterDecorator<IService>((ctx, p, s) => new Decorator(s), "from").OnlyIf(r => true);
builder.RegisterDecorator<IService>((ctx, s) => new Decorator(s), "from").OnlyIf(r => true);
builder.RegisterDecorator<IService>(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<SimpleFactory>().OnlyIf(r => true);
builder.RegisterGeneratedFactory<SimpleFactory>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ public void FindConstructorsWith_FinderFunctionProvided_PassedToConstructorFinde
{
var cb = new ContainerBuilder();
var finderCalled = false;
Func<Type, ConstructorInfo[]> finder = type =>
ConstructorInfo[] Finder(Type type)
{
finderCalled = true;
return type.GetConstructors();
};
}

cb.RegisterType<A1>();
cb.RegisterType<MultipleConstructors>().FindConstructorsWith(finder);
cb.RegisterType<MultipleConstructors>().FindConstructorsWith(Finder);
var container = cb.Build();

container.Resolve<MultipleConstructors>();
Expand Down
8 changes: 4 additions & 4 deletions test/Autofac.Test/Concurrency/ConcurrencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void WhenTwoThreadsInitializeASharedInstanceSimultaneouslyViaChildLifetim

var container = builder.Build();

ThreadStart work = () =>
void Work()
{
try
{
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/Autofac.Test/Core/Registration/ComponentRegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ public void AddingConcreteImplementationWhenAdapterImplementationsExist_AddsChai
var pre = container.ComponentRegistry.RegistrationsFor(chainedService);
Assert.Single(pre);

Func<object> 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<object>)ObjectFactory).CreateRegistration())))
{
var post = lifetimeScope.ComponentRegistry.RegistrationsFor(chainedService);
Assert.Equal(2, post.Count());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Autofac.Test.Features.GeneratedFactories;

#pragma warning disable CS0618

public class GeneratedFactoriesTests
{
public class A<T>
Expand Down Expand Up @@ -164,7 +166,7 @@ public void RespectsContexts()
}

[Fact]
public void CanSetParmeterMappingToPositional()
public void CanSetParameterMappingToPositional()
{
var builder = new ContainerBuilder();

Expand Down Expand Up @@ -259,7 +261,7 @@ public void WillNotAutoGenerateFactoriesWhenProductNotRegistered()
}

[Fact]
public void CreateGenericFromNongenericFactoryDelegate()
public void CreateGenericFromNonGenericFactoryDelegate()
{
var builder = new ContainerBuilder();

Expand All @@ -278,7 +280,7 @@ public void CreateGenericFromNongenericFactoryDelegate()
}

[Fact]
public void CreateGenericFromNongenericFactoryDelegateImpliedServiceType()
public void CreateGenericFromNonGenericFactoryDelegateImpliedServiceType()
{
var builder = new ContainerBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void CallingDisposeOnGeneratedOwnedT_DoesNotDisposeCurrentLifetimeScope()
Assert.False(containerDisposeTracker.IsDisposed);
}

#pragma warning disable CS0618
[Fact]
public void CanResolveAndUse_OwnedGeneratedFactory()
{
Expand All @@ -55,6 +56,7 @@ public void CanResolveAndUse_OwnedGeneratedFactory()

Assert.True(isAccessed);
}
#pragma warning restore CS0618

[Fact]
public void IfInnerTypeIsNotRegistered_OwnedTypeIsNotEither()
Expand Down