/// <summary> /// Gets a stub instance of the specified type. /// </summary> /// <returns>A stub object.</returns> /// <typeparam name="T">The type of the stub to create.</typeparam> public T GetStub <T>() where T : class { InterfaceDetector.AssertIsInterface <T>(); var interceptor = interceptorFactory(); return(proxyGenerator.CreateInterfaceProxyWithoutTarget <T>(interceptor)); }
/// <summary> /// Gets an auto-lazy service which will wrap the specified <see cref="Lazy{T}"/> instance. /// </summary> /// <remarks> /// <para> /// This works by creating a DynamicProxy using <see cref="IProxyGenerator.CreateInterfaceProxyWithTargetInterface{TInterface}(TInterface, IInterceptor[])"/>. /// That proxy implements the interface <typeparamref name="T"/> and is initially satisfied by a stub /// implementation of that interface type. The stub implementation is created via <see cref="IGetsStubs"/>. /// </para> /// <para> /// One interceptor is created and used with the proxy, and that interceptor is created via /// <see cref="IGetsAutoLazyInterceptors"/>. That interceptor will redirect any invocation of any /// functionality to <see cref="Lazy{T}.Value"/> of the specified <paramref name="lazy"/> /// object. /// </para> /// </remarks> /// <returns>The auto-lazy service.</returns> /// <param name="lazy">A lazy instance, to wrap in an auto-lazy service.</param> /// <typeparam name="T">The type of service.</typeparam> /// <exception cref="ArgumentNullException">If <paramref name="lazy"/> is <c>null</c>.</exception> /// <exception cref="AutoLazyException">If the service type <typeparamref name="T"/> is not valid to be an auto-lazy service.</exception> public T GetAutoLazyService <T>(Lazy <T> lazy) where T : class { if (lazy == null) { throw new ArgumentNullException(nameof(lazy)); } InterfaceDetector.AssertIsInterface <T>(); var lazyInterceptor = autoLazyInterceptorFactory.GetAutoLazyInterceptor(lazy); var stubImplementation = stubFactory.GetStub <T>(); return(proxyGenerator.CreateInterfaceProxyWithTargetInterface(stubImplementation, lazyInterceptor)); }
/// <summary> /// Initializes a new instance of the <see cref="LazyInstanceProvider{T}"/> class. /// </summary> /// <param name="lazy">A lazy object (which will point at the original implementation of <typeparamref name="T"/>).</param> public LazyInstanceProvider(Lazy <T> lazy) { InterfaceDetector.AssertIsInterface <T>(); Instance = lazy ?? throw new ArgumentNullException(nameof(lazy)); }