Skip to content

Commit 2b17b03

Browse files
ARCH-408: Add comments about Unity.
1 parent 76bd325 commit 2b17b03

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

LazyProxy.Unity/UnityExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,39 @@ public static IUnityContainer RegisterLazy(this IUnityContainer container,
150150
throw new NotSupportedException("The lazy registration is supported only for interfaces.");
151151
}
152152

153+
// Note #1
154+
// -------
155+
// If UnityContainer has the correct overload of the '.RegisterFactory' method we can make it easier:
156+
//
157+
// var registrationName = Guid.NewGuid().ToString();
158+
// return container
159+
// .RegisterType(typeFrom, typeTo, registrationName, getLifetimeManager(), injectionMembers)
160+
// .RegisterFactory(typeFrom, name,
161+
// (c, t, n, o) => LazyProxyBuilder.CreateInstance(t, () => c.Resolve(t, registrationName, o)),
162+
// getLifetimeManager());
163+
//
164+
// We opened an issue on GitHub and suggested pull requests to introduce the overload:
165+
// - https://github.com/unitycontainer/container/issues/147
166+
// - https://github.com/unitycontainer/abstractions/pull/98
167+
// - https://github.com/unitycontainer/container/pull/148
168+
//
169+
// But unfortunately the issue and pull requests were rejected for reasons not entirely clear.
170+
// That is why we have to use extension.
171+
//
172+
// Note #2
173+
// -------
174+
// We have to use an extension per type because UnityContainer has weird behaviour when work with
175+
// open generic types therefore we can't use fake interface to avoid multiple extensions:
176+
//
177+
// public interface ILazyProxy<T> {}
178+
//
179+
// ...then register types like this:
180+
// container.Register(_typeFrom, _typeTo, "LazyProxyImpl" + _name);
181+
// container.Register(_typeFrom, typeof(ILazyProxy<>).MakeGenericType(_typeFrom), _name);
182+
//
183+
// ...and use single extension per container:
184+
// context.Policy.Set(typeof(ILazyProxy<>), ...)
185+
153186
return container.AddExtension(
154187
new LazyProxyUnityExtension(typeFrom, typeTo, name, getLifetimeManager, injectionMembers)
155188
);

0 commit comments

Comments
 (0)