@@ -71,23 +71,91 @@ public static IUnityContainer RegisterLazy<TFrom, TTo>(this IUnityContainer cont
71
71
string name ,
72
72
Func < LifetimeManager > getLifetimeManager ,
73
73
params InjectionMember [ ] injectionMembers )
74
- where TTo : TFrom where TFrom : class
74
+ where TTo : TFrom where TFrom : class =>
75
+ container . RegisterLazy ( typeof ( TFrom ) , typeof ( TTo ) , name , getLifetimeManager , injectionMembers ) ;
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 execution.
80
+ /// </summary>
81
+ /// <param name="typeFrom">The binded interface.</param>
82
+ /// <param name="typeTo">The binded class.</param>
83
+ /// <param name="container">The instance of Unity container.</param>
84
+ /// <param name="injectionMembers">The set of injection members.</param>
85
+ /// <returns>The instance of Unity container.</returns>
86
+ public static IUnityContainer RegisterLazy ( this IUnityContainer container ,
87
+ Type typeFrom ,
88
+ Type typeTo ,
89
+ params InjectionMember [ ] injectionMembers ) =>
90
+ container . RegisterLazy ( typeFrom , typeTo , null , GetTransientLifetimeManager , injectionMembers ) ;
91
+
92
+ /// <summary>
93
+ /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
94
+ /// The real class To will be instantiated only after first method or property execution.
95
+ /// </summary>
96
+ /// <param name="typeFrom">The binded interface.</param>
97
+ /// <param name="typeTo">The binded class.</param>
98
+ /// <param name="container">The instance of Unity container.</param>
99
+ /// <param name="name">The registration name.</param>
100
+ /// <param name="injectionMembers">The set of injection members.</param>
101
+ /// <returns>The instance of Unity container.</returns>
102
+ public static IUnityContainer RegisterLazy ( this IUnityContainer container ,
103
+ Type typeFrom ,
104
+ Type typeTo ,
105
+ string name ,
106
+ params InjectionMember [ ] injectionMembers ) =>
107
+ container . RegisterLazy ( typeFrom , typeTo , name , GetTransientLifetimeManager , injectionMembers ) ;
108
+
109
+ /// <summary>
110
+ /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
111
+ /// The real class To will be instantiated only after first method or property execution.
112
+ /// </summary>
113
+ /// <param name="typeFrom">The binded interface.</param>
114
+ /// <param name="typeTo">The binded class.</param>
115
+ /// <param name="container">The instance of Unity container.</param>
116
+ /// <param name="getLifetimeManager">The function instance lifetime provides.</param>
117
+ /// <param name="injectionMembers">The set of injection members.</param>
118
+ /// <returns>The instance of Unity container.</returns>
119
+ public static IUnityContainer RegisterLazy ( this IUnityContainer container ,
120
+ Type typeFrom ,
121
+ Type typeTo ,
122
+ Func < LifetimeManager > getLifetimeManager ,
123
+ params InjectionMember [ ] injectionMembers ) =>
124
+ container . RegisterLazy ( typeFrom , typeTo , null , getLifetimeManager , injectionMembers ) ;
125
+
126
+ /// <summary>
127
+ /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
128
+ /// The real class To will be instantiated only after first method or property execution.
129
+ /// </summary>
130
+ /// <param name="typeFrom">The binded interface.</param>
131
+ /// <param name="typeTo">The binded class.</param>
132
+ /// <param name="container">The instance of Unity container.</param>
133
+ /// <param name="name">The registration name.</param>
134
+ /// <param name="getLifetimeManager">The function instance lifetime provides.</param>
135
+ /// <param name="injectionMembers">The set of injection members.</param>
136
+ /// <returns>The instance of Unity container.</returns>
137
+ public static IUnityContainer RegisterLazy ( this IUnityContainer container ,
138
+ Type typeFrom ,
139
+ Type typeTo ,
140
+ string name ,
141
+ Func < LifetimeManager > getLifetimeManager ,
142
+ params InjectionMember [ ] injectionMembers )
75
143
{
76
144
// There is no way to constraint it on the compilation step.
77
- if ( ! typeof ( TFrom ) . IsInterface )
145
+ if ( ! typeFrom . IsInterface )
78
146
{
79
147
throw new NotSupportedException ( "The lazy registration is supported only for interfaces." ) ;
80
148
}
81
149
82
- var lazyProxyType = LazyProxyBuilder . BuildLazyProxyType < TFrom > ( ) ;
150
+ var lazyProxyType = LazyProxyBuilder . BuildLazyProxyType ( typeFrom ) ;
83
151
var registrationName = Guid . NewGuid ( ) . ToString ( ) ;
84
152
85
153
return container
86
- . RegisterType < TFrom , TTo > ( registrationName , getLifetimeManager ( ) , injectionMembers )
87
- . RegisterType ( typeof ( TFrom ) , lazyProxyType , name ,
154
+ . RegisterType ( typeFrom , typeTo , registrationName , getLifetimeManager ( ) , injectionMembers )
155
+ . RegisterType ( typeFrom , lazyProxyType , name ,
88
156
getLifetimeManager ( ) ,
89
157
new InjectionConstructor (
90
- new ResolvedParameter < Lazy < TFrom > > ( registrationName ) )
158
+ new ResolvedParameter ( typeof ( Lazy < > ) . MakeGenericType ( typeFrom ) , registrationName ) )
91
159
) ;
92
160
}
93
161
}
0 commit comments