@@ -8,6 +8,8 @@ namespace LazyProxy.Unity
8
8
{
9
9
public static class UnityExtensions
10
10
{
11
+ private static readonly Func < LifetimeManager > GetTransientLifetimeManager = ( ) => new TransientLifetimeManager ( ) ;
12
+
11
13
/// <summary>
12
14
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
13
15
/// The real class To will be instantiated only after first method execution.
@@ -19,10 +21,24 @@ public static class UnityExtensions
19
21
/// <returns>The instance of Unity container.</returns>
20
22
public static IUnityContainer RegisterLazy < TFrom , TTo > ( this IUnityContainer container ,
21
23
params InjectionMember [ ] injectionMembers )
22
- where TTo : TFrom where TFrom : class
23
- {
24
- return container . RegisterLazy < TFrom , TTo > ( ( ) => new TransientLifetimeManager ( ) , injectionMembers ) ;
25
- }
24
+ where TTo : TFrom where TFrom : class =>
25
+ container . RegisterLazy < TFrom , TTo > ( null , GetTransientLifetimeManager , injectionMembers ) ;
26
+
27
+ /// <summary>
28
+ /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
29
+ /// The real class To will be instantiated only after first method or property execution.
30
+ /// </summary>
31
+ /// <param name="container">The instance of Unity container.</param>
32
+ /// <param name="name">The registration name.</param>
33
+ /// <param name="injectionMembers">The set of injection members.</param>
34
+ /// <typeparam name="TFrom">The binded interface.</typeparam>
35
+ /// <typeparam name="TTo">The binded class.</typeparam>
36
+ /// <returns>The instance of Unity container.</returns>
37
+ public static IUnityContainer RegisterLazy < TFrom , TTo > ( this IUnityContainer container ,
38
+ string name ,
39
+ params InjectionMember [ ] injectionMembers )
40
+ where TTo : TFrom where TFrom : class =>
41
+ container . RegisterLazy < TFrom , TTo > ( name , GetTransientLifetimeManager , injectionMembers ) ;
26
42
27
43
/// <summary>
28
44
/// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
@@ -35,16 +51,40 @@ public static IUnityContainer RegisterLazy<TFrom, TTo>(this IUnityContainer cont
35
51
/// <typeparam name="TTo">The binded class.</typeparam>
36
52
/// <returns>The instance of Unity container.</returns>
37
53
public static IUnityContainer RegisterLazy < TFrom , TTo > ( this IUnityContainer container ,
54
+ Func < LifetimeManager > getLifetimeManager ,
55
+ params InjectionMember [ ] injectionMembers )
56
+ where TTo : TFrom where TFrom : class =>
57
+ container . RegisterLazy < TFrom , TTo > ( null , getLifetimeManager , injectionMembers ) ;
58
+
59
+ /// <summary>
60
+ /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
61
+ /// The real class To will be instantiated only after first method or property execution.
62
+ /// </summary>
63
+ /// <param name="container">The instance of Unity container.</param>
64
+ /// <param name="name">The registration name.</param>
65
+ /// <param name="getLifetimeManager">The function instance lifetime provides.</param>
66
+ /// <param name="injectionMembers">The set of injection members.</param>
67
+ /// <typeparam name="TFrom">The binded interface.</typeparam>
68
+ /// <typeparam name="TTo">The binded class.</typeparam>
69
+ /// <returns>The instance of Unity container.</returns>
70
+ public static IUnityContainer RegisterLazy < TFrom , TTo > ( this IUnityContainer container ,
71
+ string name ,
38
72
Func < LifetimeManager > getLifetimeManager ,
39
73
params InjectionMember [ ] injectionMembers )
40
74
where TTo : TFrom where TFrom : class
41
75
{
76
+ // There is no way to constraint it on the compilation step.
77
+ if ( ! typeof ( TFrom ) . IsInterface )
78
+ {
79
+ throw new NotSupportedException ( "The lazy registration is supported only for interfaces." ) ;
80
+ }
81
+
42
82
var lazyProxyType = LazyProxyBuilder . BuildLazyProxyType < TFrom > ( ) ;
43
83
var registrationName = Guid . NewGuid ( ) . ToString ( ) ;
44
84
45
85
return container
46
86
. RegisterType < TFrom , TTo > ( registrationName , getLifetimeManager ( ) , injectionMembers )
47
- . RegisterType ( typeof ( TFrom ) , lazyProxyType ,
87
+ . RegisterType ( typeof ( TFrom ) , lazyProxyType , name ,
48
88
getLifetimeManager ( ) ,
49
89
new InjectionConstructor (
50
90
new ResolvedParameter < Lazy < TFrom > > ( registrationName ) )
0 commit comments