Skip to content

Commit 611b825

Browse files
Add OpenGenericFactoryRegistrationSource only once and only for open generic types.
1 parent baf90ee commit 611b825

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

LazyProxy.Autofac.Tests/AutofacExtensionTests.cs

+56
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,50 @@ public void NonLazyRegistrationMutatorMustBeApplied(string name)
383383
}
384384
}
385385

386+
[Theory]
387+
[InlineData(null)]
388+
[InlineData("name")]
389+
public void RegisterLazyMustAddOpenGenericFactoryRegistrationSourceOnlyOnceForOpenGenericTypes(
390+
string name)
391+
{
392+
var containerBuilder = new ContainerBuilder();
393+
containerBuilder.RegisterLazy(typeof(IGenericService<,,>), typeof(GenericService<,,>), name);
394+
containerBuilder.RegisterLazy(typeof(IGenericService2<>), typeof(GenericService2<>), name);
395+
396+
using (var container = containerBuilder.Build())
397+
{
398+
var count = container.ComponentRegistry.Sources
399+
.OfType<OpenGenericFactoryRegistrationSource>()
400+
.Count();
401+
402+
Assert.Equal(1, count);
403+
}
404+
}
405+
406+
[Theory]
407+
[InlineData(null)]
408+
[InlineData("name")]
409+
public void RegisterLazyMustNotAddOpenGenericFactoryRegistrationSourceForNonOpenGenericTypes(
410+
string name)
411+
{
412+
var containerBuilder = new ContainerBuilder();
413+
414+
containerBuilder.RegisterLazy(typeof(IService1), typeof(Service1), name);
415+
416+
containerBuilder.RegisterLazy(
417+
typeof(IGenericService<ParameterType1, ParameterType2, ParameterType3>),
418+
typeof(GenericService<ParameterType1, ParameterType2, ParameterType3>), name);
419+
420+
using (var container = containerBuilder.Build())
421+
{
422+
var count = container.ComponentRegistry.Sources
423+
.OfType<OpenGenericFactoryRegistrationSource>()
424+
.Count();
425+
426+
Assert.Equal(0, count);
427+
}
428+
}
429+
386430
#region Lifetime tests
387431

388432
[Theory]
@@ -790,6 +834,18 @@ public GenericService()
790834
}
791835
}
792836

837+
// ReSharper disable once MemberCanBePrivate.Global
838+
public interface IGenericService2<T>
839+
{
840+
// ReSharper disable once UnusedMember.Global
841+
T Get(T arg1);
842+
}
843+
844+
private class GenericService2<T> : IGenericService2<T>
845+
{
846+
public T Get(T arg1) => arg1;
847+
}
848+
793849
#endregion
794850
}
795851
}

LazyProxy.Autofac/AutofacExtensions.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace LazyProxy.Autofac
1212
/// </summary>
1313
public static class AutofacExtensions
1414
{
15+
private const string OpenGenericFactoryRegistrationSourceIsAlreadyAdded =
16+
nameof(OpenGenericFactoryRegistrationSourceIsAlreadyAdded);
17+
1518
/// <summary>
1619
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
1720
/// The real class To will be instantiated only after first method or property execution.
@@ -48,12 +51,16 @@ public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrati
4851
throw new NotSupportedException("The lazy registration is supported only for interfaces.");
4952
}
5053

51-
builder.RegisterSource<OpenGenericFactoryRegistrationSource>();
52-
5354
var registrationName = Guid.NewGuid().ToString();
5455

5556
if (typeTo.IsGenericTypeDefinition)
5657
{
58+
if (!builder.Properties.ContainsKey(OpenGenericFactoryRegistrationSourceIsAlreadyAdded))
59+
{
60+
builder.RegisterSource<OpenGenericFactoryRegistrationSource>();
61+
builder.Properties.Add(OpenGenericFactoryRegistrationSourceIsAlreadyAdded, true);
62+
}
63+
5764
var nonLazyRegistration = builder.RegisterGeneric(typeTo).Named(registrationName, typeFrom);
5865
nonLazyRegistrationMutator?.Mutate(nonLazyRegistration);
5966
}

0 commit comments

Comments
 (0)