Skip to content

Commit 2773083

Browse files
Merge pull request #2 from servicetitan/non-generic-registration
Add non-generic RegisterLazy overload.
2 parents e083385 + 6ea808c commit 2773083

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

LazyProxy.Unity/LazyProxy.Unity.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1616
</PropertyGroup>
1717
<ItemGroup>
18-
<PackageReference Include="LazyProxy" Version="0.1.1" />
18+
<PackageReference Include="LazyProxy" Version="0.1.2" />
1919
<PackageReference Include="Unity" Version="5.8.6" />
2020
</ItemGroup>
2121
</Project>

LazyProxy.Unity/UnityExtensions.cs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,91 @@ public static IUnityContainer RegisterLazy<TFrom, TTo>(this IUnityContainer cont
7171
string name,
7272
Func<LifetimeManager> getLifetimeManager,
7373
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)
75143
{
76144
// There is no way to constraint it on the compilation step.
77-
if (!typeof(TFrom).IsInterface)
145+
if (!typeFrom.IsInterface)
78146
{
79147
throw new NotSupportedException("The lazy registration is supported only for interfaces.");
80148
}
81149

82-
var lazyProxyType = LazyProxyBuilder.BuildLazyProxyType<TFrom>();
150+
var lazyProxyType = LazyProxyBuilder.BuildLazyProxyType(typeFrom);
83151
var registrationName = Guid.NewGuid().ToString();
84152

85153
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,
88156
getLifetimeManager(),
89157
new InjectionConstructor(
90-
new ResolvedParameter<Lazy<TFrom>>(registrationName))
158+
new ResolvedParameter(typeof(Lazy<>).MakeGenericType(typeFrom), registrationName))
91159
);
92160
}
93161
}

0 commit comments

Comments
 (0)