@@ -8,6 +8,8 @@ namespace LazyProxy.Unity
88{
99 public static class UnityExtensions
1010 {
11+ private static readonly Func < LifetimeManager > GetTransientLifetimeManager = ( ) => new TransientLifetimeManager ( ) ;
12+
1113 /// <summary>
1214 /// Is used to register interface TFrom to class TTo by creation a lazy proxy at runtime.
1315 /// The real class To will be instantiated only after first method execution.
@@ -19,10 +21,24 @@ public static class UnityExtensions
1921 /// <returns>The instance of Unity container.</returns>
2022 public static IUnityContainer RegisterLazy < TFrom , TTo > ( this IUnityContainer container ,
2123 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 ) ;
2642
2743 /// <summary>
2844 /// 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
3551 /// <typeparam name="TTo">The binded class.</typeparam>
3652 /// <returns>The instance of Unity container.</returns>
3753 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 ,
3872 Func < LifetimeManager > getLifetimeManager ,
3973 params InjectionMember [ ] injectionMembers )
4074 where TTo : TFrom where TFrom : class
4175 {
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+
4282 var lazyProxyType = LazyProxyBuilder . BuildLazyProxyType < TFrom > ( ) ;
4383 var registrationName = Guid . NewGuid ( ) . ToString ( ) ;
4484
4585 return container
4686 . RegisterType < TFrom , TTo > ( registrationName , getLifetimeManager ( ) , injectionMembers )
47- . RegisterType ( typeof ( TFrom ) , lazyProxyType ,
87+ . RegisterType ( typeof ( TFrom ) , lazyProxyType , name ,
4888 getLifetimeManager ( ) ,
4989 new InjectionConstructor (
5090 new ResolvedParameter < Lazy < TFrom > > ( registrationName ) )
0 commit comments