Skip to content

Commit b80ff44

Browse files
Implementation refactoring.
1 parent a207ba4 commit b80ff44

File tree

2 files changed

+33
-134
lines changed

2 files changed

+33
-134
lines changed

LazyProxy.Autofac.Tests/AutofacExtensionTests.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -384,25 +384,20 @@ public void InstancePerLifetimeScopeServiceLifetimeMustBeActualForOpenGenericSer
384384
}
385385

386386
[Theory]
387-
[InlineData(ServiceLifetime.Unknown, null)]
388-
[InlineData(ServiceLifetime.Unknown, "name")]
389-
[InlineData(ServiceLifetime.InstancePerDependency, null)]
390-
[InlineData(ServiceLifetime.InstancePerDependency, "name")]
391-
public void InstancePerDependencyServiceLifetimeMustBeActual(ServiceLifetime lifetime, string name) =>
387+
[InlineData(null)]
388+
[InlineData("name")]
389+
public void InstancePerDependencyServiceLifetimeMustBeActual(string name) =>
392390
AssertInstancePerDependencyServiceLifetimeMustBeActual<IService>(
393-
typeof(IService), typeof(Service), lifetime, name);
391+
typeof(IService), typeof(Service), name);
394392

395393
[Theory]
396-
[InlineData(ServiceLifetime.Unknown, null)]
397-
[InlineData(ServiceLifetime.Unknown, "name")]
398-
[InlineData(ServiceLifetime.InstancePerDependency, null)]
399-
[InlineData(ServiceLifetime.InstancePerDependency, "name")]
400-
public void InstancePerDependencyServiceLifetimeMustBeActualForOpenGenericService(
401-
ServiceLifetime lifetime, string name)
394+
[InlineData(null)]
395+
[InlineData("name")]
396+
public void InstancePerDependencyServiceLifetimeMustBeActualForOpenGenericService(string name)
402397
{
403398
AssertInstancePerDependencyServiceLifetimeMustBeActual<
404399
IGenericService<ParameterType1, ParameterType2, ParameterType3>>(
405-
typeof(IGenericService<,,>), typeof(GenericService<,,>), lifetime, name);
400+
typeof(IGenericService<,,>), typeof(GenericService<,,>), name);
406401
}
407402

408403
#endregion
@@ -459,7 +454,7 @@ private IEnumerable<Guid> All()
459454
private static void AssertSingleInstanceServiceLifetimeMustBeActual<T>(Type typeFrom, Type typeTo, string name)
460455
{
461456
var containerBuilder = new ContainerBuilder();
462-
containerBuilder.RegisterLazy(typeFrom, typeTo, name, ServiceLifetime.SingleInstance);
457+
containerBuilder.RegisterLazy(typeFrom, typeTo, name).SingleInstance();
463458

464459
using (var container = containerBuilder.Build()) {
465460
var resolves = GetResolves<T>(container, name);
@@ -482,7 +477,7 @@ private static void AssertInstancePerLifetimeScopeServiceLifetimeMustBeActual<T>
482477
Type typeFrom, Type typeTo, string name)
483478
{
484479
var containerBuilder = new ContainerBuilder();
485-
containerBuilder.RegisterLazy(typeFrom, typeTo, name, ServiceLifetime.InstancePerLifetimeScope);
480+
containerBuilder.RegisterLazy(typeFrom, typeTo, name).InstancePerLifetimeScope();
486481

487482
using (var container = containerBuilder.Build()) {
488483
var resolves = GetResolves<T>(container, name);
@@ -511,10 +506,10 @@ private static void AssertInstancePerLifetimeScopeServiceLifetimeMustBeActual<T>
511506
}
512507

513508
private static void AssertInstancePerDependencyServiceLifetimeMustBeActual<T>(
514-
Type typeFrom, Type typeTo, ServiceLifetime lifetime, string name)
509+
Type typeFrom, Type typeTo, string name)
515510
{
516511
var containerBuilder = new ContainerBuilder();
517-
containerBuilder.RegisterLazy(typeFrom, typeTo, name, lifetime);
512+
containerBuilder.RegisterLazy(typeFrom, typeTo, name).InstancePerDependency();
518513

519514
using (var container = containerBuilder.Build()) {
520515
var resolves = GetResolves<T>(container, name)
Lines changed: 21 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Autofac;
3+
using Autofac.Builder;
34

45
namespace LazyProxy.Autofac
56
{
@@ -8,59 +9,32 @@ namespace LazyProxy.Autofac
89
/// </summary>
910
public static class AutofacExtensions
1011
{
11-
/// <summary>
12-
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
13-
/// The real class To will be instantiated only after first method execution.
14-
/// </summary>
15-
/// <param name="builder">The instance of the Autofac container builder.</param>
16-
/// <typeparam name="TFrom">The binded interface.</typeparam>
17-
/// <typeparam name="TTo">The binded class.</typeparam>
18-
/// <returns>The instance of the Autofac container builder.</returns>
19-
public static ContainerBuilder RegisterLazy<TFrom, TTo>(this ContainerBuilder builder)
20-
where TTo : TFrom where TFrom : class =>
21-
builder.RegisterLazy<TFrom, TTo>(null, ServiceLifetime.InstancePerDependency);
22-
23-
/// <summary>
24-
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
25-
/// The real class To will be instantiated only after first method or property execution.
26-
/// </summary>
27-
/// <param name="builder">The instance of the Autofac container builder.</param>
28-
/// <param name="name">The registration name.</param>
29-
/// <typeparam name="TFrom">The binded interface.</typeparam>
30-
/// <typeparam name="TTo">The binded class.</typeparam>
31-
/// <returns>The instance of the Autofac container builder.</returns>
32-
public static ContainerBuilder RegisterLazy<TFrom, TTo>(this ContainerBuilder builder, string name)
33-
where TTo : TFrom where TFrom : class =>
34-
builder.RegisterLazy<TFrom, TTo>(name, ServiceLifetime.InstancePerDependency);
35-
3612
/// <summary>
3713
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
3814
/// The real class To will be instantiated only after first method or property execution.
3915
/// </summary>
4016
/// <param name="builder">The instance of the Autofac container builder.</param>
41-
/// <param name="serviceLifetime">The instance lifetime.</param>
4217
/// <typeparam name="TFrom">The binded interface.</typeparam>
4318
/// <typeparam name="TTo">The binded class.</typeparam>
44-
/// <returns>The instance of the Autofac container builder.</returns>
45-
public static ContainerBuilder RegisterLazy<TFrom, TTo>(
46-
this ContainerBuilder builder, ServiceLifetime serviceLifetime)
19+
/// <returns>The instance of the Autofac registration builder.</returns>
20+
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle>
21+
RegisterLazy<TFrom, TTo>(this ContainerBuilder builder)
4722
where TTo : TFrom where TFrom : class =>
48-
builder.RegisterLazy<TFrom, TTo>(null, serviceLifetime);
23+
builder.RegisterLazy(typeof(TFrom), typeof(TTo), null);
4924

5025
/// <summary>
5126
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
5227
/// The real class To will be instantiated only after first method or property execution.
5328
/// </summary>
5429
/// <param name="builder">The instance of the Autofac container builder.</param>
55-
/// <param name="name">The registration name.</param>
56-
/// <param name="serviceLifetime">The instance lifetime.</param>
30+
/// <param name="name">The registration name. Null if named registration is not required.</param>
5731
/// <typeparam name="TFrom">The binded interface.</typeparam>
5832
/// <typeparam name="TTo">The binded class.</typeparam>
59-
/// <returns>The instance of the Autofac container builder.</returns>
60-
public static ContainerBuilder RegisterLazy<TFrom, TTo>(
61-
this ContainerBuilder builder, string name, ServiceLifetime serviceLifetime)
33+
/// <returns>The instance of the Autofac registration builder.</returns>
34+
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle>
35+
RegisterLazy<TFrom, TTo>(this ContainerBuilder builder, string name)
6236
where TTo : TFrom where TFrom : class =>
63-
builder.RegisterLazy(typeof(TFrom), typeof(TTo), name, serviceLifetime);
37+
builder.RegisterLazy(typeof(TFrom), typeof(TTo), name);
6438

6539
/// <summary>
6640
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
@@ -69,36 +43,10 @@ public static ContainerBuilder RegisterLazy<TFrom, TTo>(
6943
/// <param name="typeFrom">The binded interface.</param>
7044
/// <param name="typeTo">The binded class.</param>
7145
/// <param name="builder">The instance of the Autofac container builder.</param>
72-
/// <returns>The instance of the Autofac container builder.</returns>
73-
public static ContainerBuilder RegisterLazy(
74-
this ContainerBuilder builder, Type typeFrom, Type typeTo) =>
75-
builder.RegisterLazy(typeFrom, typeTo, null, ServiceLifetime.InstancePerDependency);
76-
77-
/// <summary>
78-
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
79-
/// The real class To will be instantiated only after first method or property execution.
80-
/// </summary>
81-
/// <param name="typeFrom">The binded interface.</param>
82-
/// <param name="typeTo">The binded class.</param>
83-
/// <param name="builder">The instance of the Autofac container builder.</param>
84-
/// <param name="name">The registration name.</param>
85-
/// <returns>The instance of the Autofac container builder.</returns>
86-
public static ContainerBuilder RegisterLazy(
87-
this ContainerBuilder builder, Type typeFrom, Type typeTo, string name) =>
88-
builder.RegisterLazy(typeFrom, typeTo, name, ServiceLifetime.InstancePerDependency);
89-
90-
/// <summary>
91-
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
92-
/// The real class To will be instantiated only after first method or property execution.
93-
/// </summary>
94-
/// <param name="typeFrom">The binded interface.</param>
95-
/// <param name="typeTo">The binded class.</param>
96-
/// <param name="builder">The instance of the Autofac container builder.</param>
97-
/// <param name="serviceLifetime">The instance lifetime.</param>
98-
/// <returns>The instance of the Autofac container builder.</returns>
99-
public static ContainerBuilder RegisterLazy(
100-
this ContainerBuilder builder, Type typeFrom, Type typeTo, ServiceLifetime serviceLifetime) =>
101-
builder.RegisterLazy(typeFrom, typeTo, null, serviceLifetime);
46+
/// <returns>The instance of the Autofac registration builder.</returns>
47+
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle>
48+
RegisterLazy(this ContainerBuilder builder, Type typeFrom, Type typeTo) =>
49+
builder.RegisterLazy(typeFrom, typeTo, null);
10250

10351
/// <summary>
10452
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
@@ -107,11 +55,10 @@ public static ContainerBuilder RegisterLazy(
10755
/// <param name="typeFrom">The binded interface.</param>
10856
/// <param name="typeTo">The binded class.</param>
10957
/// <param name="builder">The instance of the Autofac container builder.</param>
110-
/// <param name="name">The registration name.</param>
111-
/// <param name="serviceLifetime">The instance lifetime.</param>
112-
/// <returns>The instance of the Autofac container builder.</returns>
113-
public static ContainerBuilder RegisterLazy(
114-
this ContainerBuilder builder, Type typeFrom, Type typeTo, string name, ServiceLifetime serviceLifetime)
58+
/// <param name="name">The registration name. Null if named registration is not required.</param>
59+
/// <returns>The instance of the Autofac registration builder.</returns>
60+
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle>
61+
RegisterLazy(this ContainerBuilder builder, Type typeFrom, Type typeTo, string name)
11562
{
11663
// There is no way to constraint it on the compilation step.
11764
if (!typeFrom.IsInterface)
@@ -132,52 +79,9 @@ public static ContainerBuilder RegisterLazy(
13279
);
13380
});
13481

135-
// Variant 2 (Overrides dont work
136-
// builder.RegisterType(typeTo).Named(registrationName, typeTo);
137-
//
138-
// var funcType = typeof(Func<>);
139-
// var factoryType = funcType.MakeGenericType(typeTo);
140-
//
141-
// var registration = builder.Register((c, p) =>
142-
// LazyProxyBuilder.CreateInstance(typeFrom, (Func<object>)c.ResolveNamed(registrationName, factoryType)));
143-
144-
if (name == null)
145-
{
146-
registration.As(typeFrom);
147-
}
148-
else
149-
{
150-
registration.Named(name, typeFrom);
151-
}
152-
153-
switch (serviceLifetime)
154-
{
155-
case ServiceLifetime.Unknown:
156-
case ServiceLifetime.InstancePerDependency:
157-
registration.InstancePerDependency().ExternallyOwned();
158-
break;
159-
case ServiceLifetime.SingleInstance:
160-
registration.SingleInstance();
161-
break;
162-
case ServiceLifetime.InstancePerLifetimeScope:
163-
registration.InstancePerLifetimeScope();
164-
break;
165-
default:
166-
throw new ArgumentOutOfRangeException(nameof(serviceLifetime), serviceLifetime, null);
167-
}
168-
169-
return builder;
82+
return name == null
83+
? registration.As(typeFrom)
84+
: registration.Named(name, typeFrom);
17085
}
17186
}
172-
173-
/// <summary>
174-
/// Service lifetime provided by the IoC container.
175-
/// </summary>
176-
public enum ServiceLifetime
177-
{
178-
Unknown = 0,
179-
SingleInstance = 1,
180-
InstancePerLifetimeScope = 2,
181-
InstancePerDependency = 3
182-
}
18387
}

0 commit comments

Comments
 (0)