public void CreateFromFactory_executes_factory_with_resolved_parameters([Frozen] IFulfilsResolutionRequests resolver, InstanceCreator sut, IFactoryAdapter factory, ResolutionPath path, IServiceRegistration registration, object resolved, [MockParam(typeof(string), Name = "Foo")] ParameterInfo paramOne, object paramOneValue, [MockParam(typeof(int), Name = "Bar")] ParameterInfo paramTwo, object paramTwoValue) { // Arrange Mock.Get(factory).SetupGet(x => x.RequiresParameterResolution).Returns(true); Mock.Get(factory).Setup(x => x.GetParameters()).Returns(new [] { paramOne, paramTwo }); Mock.Get(resolver) .Setup(x => x.Resolve(It.Is <ResolutionRequest>(r => r.Name == paramOne.Name && r.ServiceType == paramOne.ParameterType))) .Returns(() => ResolutionResult.Success(path, paramOneValue)); Mock.Get(resolver) .Setup(x => x.Resolve(It.Is <ResolutionRequest>(r => r.Name == paramTwo.Name && r.ServiceType == paramTwo.ParameterType))) .Returns(() => ResolutionResult.Success(path, paramTwoValue)); Mock.Get(factory) .Setup(x => x.Execute(It.Is <object[]>(e => e.Length == 2 && e[0] == paramOneValue && e[1] == paramTwoValue))) .Returns(resolved); // Act var result = sut.CreateFromFactory(factory, path, registration); // Assert Mock.Get(factory) .Verify(x => x.Execute(It.Is <object[]>(e => e.Length == 2 && e[0] == paramOneValue && e[1] == paramTwoValue)), Times.Once); Assert.That(result, Is.SameAs(resolved)); }
ResolutionResult ResolveNamedInstanceDictionary(ResolutionRequest request) { var keyType = dictionaryFactory.GetKeyType(request.ServiceType); var valueType = dictionaryFactory.GetValueType(request.ServiceType); var dictionary = dictionaryFactory.Create(keyType, valueType); var allServiceTypeRegistrations = registrationAccessor.GetAll(valueType); foreach (var registration in allServiceTypeRegistrations) { var serviceResult = ResolveSingleInstance(registration, request); if (!serviceResult.IsSuccess) { // TODO: Throw an exception? Return a failed resolution result? Currently I'm silently skipping the registration. continue; } var dictionaryKey = ConvertToNameType(registration.Name, keyType); dictionary.Add(dictionaryKey, serviceResult.ResolvedObject); } var dictionaryRegistration = CreateNamedInstanceDictionaryRegistration(request.ServiceType, dictionary); var resolutionPath = request.ResolutionPath.CreateChild(dictionaryRegistration); return(ResolutionResult.Success(resolutionPath, dictionary)); }
public void Resolve_can_create_dictionary_of_instances_by_enum_values([Frozen, ResolvesToFailure] IResolver proxiedResolver, [Frozen] IDictionaryFactory dictionaryFactory, [Frozen] IServiceRegistrationProvider registrationProvider, NamedInstanceDictionaryResolverProxy sut, ResolutionRequest request, SampleEnum[] names, ResolutionPath path) { // Arrange var serviceType = request.ServiceType; var keyType = typeof(SampleEnum); var valueType = typeof(ISampleService); Mock.Get(dictionaryFactory) .Setup(x => x.IsGenericDictionaryType(serviceType)) .Returns(true); Mock.Get(dictionaryFactory) .Setup(x => x.GetKeyType(serviceType)) .Returns(keyType); Mock.Get(dictionaryFactory) .Setup(x => x.GetValueType(serviceType)) .Returns(valueType); Mock.Get(dictionaryFactory) .Setup(x => x.Create(keyType, valueType)) .Returns(() => new Dictionary <SampleEnum, ISampleService>()); var registrations = names .Select(x => x.ToString()) .Select(x => Mock.Of <IServiceRegistration>(r => r.Name == x && r.ServiceType == valueType)) .ToArray(); Mock.Get(registrationProvider) .Setup(x => x.GetAll(valueType)) .Returns(registrations); Mock.Get(proxiedResolver) .Setup(x => x.Resolve(It.Is <ResolutionRequest>(r => r.ServiceType == valueType))) .Returns((ResolutionRequest r) => ResolutionResult.Success(path, new SampleServiceImplementationOne())); // Act var result = sut.Resolve(request); // Assert Mock.Get(proxiedResolver) .Verify(x => x.Resolve(It.Is <ResolutionRequest>(r => r.ServiceType == valueType)), Times.Exactly(names.Length)); foreach (var name in names.Select(x => x.ToString()).ToArray()) { Mock.Get(proxiedResolver) .Verify(x => x.Resolve(It.Is <ResolutionRequest>(r => r.Name == name)), Times.Once); } Assert.That(result.IsSuccess, Is.True); Assert.That(result.ResolvedObject, Is.InstanceOf <IDictionary <SampleEnum, ISampleService> >()); var resolvedObject = (IDictionary <SampleEnum, ISampleService>)result.ResolvedObject; Assert.That(resolvedObject, Has.Count.EqualTo(names.Length)); }
/// <summary> /// Resolves the given resolution request and returns the result. /// </summary> /// <param name="request">Request.</param> public override ResolutionResult Resolve(ResolutionRequest request) { var result = ProxiedResolver.Resolve(request); if (result.IsSuccess) { return(result); } var defaultValue = GetDefaultForType(request.ServiceType); return(ResolutionResult.Success(request.ResolutionPath, defaultValue)); }
/// <summary> /// Resolves the given resolution request and returns the result. /// </summary> /// <param name="request">Request.</param> public override ResolutionResult Resolve(ResolutionRequest request) { if (!lazyFactory.IsLazyType(request.ServiceType)) { return(ProxiedResolver.Resolve(request)); } var lazyInnerType = lazyFactory.GetInnerLazyType(request.ServiceType); var lazyRequest = GetLazyResolutionRequest(request, lazyInnerType); var lazyObject = lazyFactory.GetLazyObject(lazyInnerType, GetObjectFactory(lazyRequest)); return(ResolutionResult.Success(request.ResolutionPath, lazyObject)); }
/// <summary> /// Attempts to fulfil the given resolution request, using the given registration, from the cache. /// </summary> /// <returns>A resolution result.</returns> /// <param name="registration">Registration.</param> /// <param name="request">Request.</param> protected virtual ResolutionResult ResolveFromCache(IServiceRegistration registration, ResolutionRequest request) { if (registration == null) { return(ResolutionResult.Failure(request.ResolutionPath)); } object resolved; if (TryGetFromCache(registration, out resolved)) { return(ResolutionResult.Success(request.ResolutionPath, resolved)); } return(ResolutionResult.Failure(request.ResolutionPath)); }
public void Resolve_returns_result_from_proxy_when_resolution_is_successful([Frozen, ResolvesToFailure] IResolver proxiedResolver, UnregisteredServiceResolverProxy sut, ResolutionRequest request, ResolutionPath path, object resolved) { // Arrange var successResult = ResolutionResult.Success(path, resolved); Mock.Get(proxiedResolver) .Setup(x => x.Resolve(request)) .Returns(successResult); // Act var result = sut.Resolve(request); // Assert Assert.That(result, Is.SameAs(successResult)); }
/// <summary> /// Resolves the given resolution request and returns the result. /// </summary> /// <param name="request">Request.</param> public override ResolutionResult Resolve(ResolutionRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (request.ServiceType != typeof(string) || request.Name != RegisteredName) { return(ProxiedResolver.Resolve(request)); } var currentResolutionPath = request.ResolutionPath; var resolvedName = currentResolutionPath.CurrentRegistration.Name; var childResolutionPath = CreateRegisteredNameResolutionPath(currentResolutionPath, resolvedName); return(ResolutionResult.Success(childResolutionPath, resolvedName)); }
public void Resolve_does_not_use_fallback_resolver_if_primary_resolver_returns_success_result([ResolvesToFailure] IResolver primaryResolver, [ResolvesToFailure] IResolver fallbackResolver, ResolutionRequest request, ResolutionPath path, object resolved) { // Arrange var sut = new FallbackResolverProxy(primaryResolver, fallbackResolver); Mock.Get(primaryResolver) .Setup(x => x.Resolve(request)) .Returns(ResolutionResult.Success(path, resolved)); // Act sut.Resolve(request); // Assert Mock.Get(primaryResolver).Verify(x => x.Resolve(request), Times.Once); Mock.Get(fallbackResolver).Verify(x => x.Resolve(request), Times.Never); }
/// <summary> /// Resolves the given resolution request and returns the result. /// </summary> /// <param name="request">Request.</param> public override ResolutionResult Resolve(ResolutionRequest request) { var result = ProxiedResolver.Resolve(request); if (request.ServiceType != typeof(IObjectContainer) || !result.IsSuccess || request.ResolutionPath.IsEmpty) { return(result); } var container = (IObjectContainer)result.ResolvedObject; if (container is DynamicResolutionObjectContainerProxy) { return(result); } var dynamicContainer = new DynamicResolutionObjectContainerProxy((ObjectContainer)container, request.ResolutionPath); var registration = GetFakeContainerRegistration(dynamicContainer, request.Name); return(ResolutionResult.Success(request.ResolutionPath, dynamicContainer)); }
public void Resolve_does_not_add_anything_to_the_cache_if_it_is_not_cacheable(IResolver proxiedResolver, [Registration(Cacheable = false)] IServiceRegistration registration, ICachesResolvedServiceInstances cache, ResolutionRequest request, ISampleService resolved, ResolutionPath resolutionPath) { // Arrange object cachedItem = null; Mock.Get(proxiedResolver).Setup(x => x.GetRegistration(request)).Returns(registration); Mock.Get(proxiedResolver).Setup(x => x.Resolve(request)).Returns(ResolutionResult.Success(resolutionPath, resolved)); Mock.Get(cache).Setup(x => x.TryGet(registration, out cachedItem)).Returns(false); var sut = new CachingResolverProxy(proxiedResolver, cache); // Act var result = sut.Resolve(request); // Assert Mock.Get(cache).Verify(x => x.Add(registration, resolved), Times.Never); }
/// <summary> /// Resolves the given resolution request and returns the result. /// </summary> /// <param name="request">Request.</param> public override ResolutionResult Resolve(ResolutionRequest request) { if (request.ServiceType != typeof(IResolvesServices) || request.ResolutionPath.IsEmpty) { return(ProxiedResolver.Resolve(request)); } var containerRequest = new ResolutionRequest(typeof(IContainer), request.Name, request.ResolutionPath); var result = ProxiedResolver.Resolve(containerRequest); if (!result.IsSuccess) { return(result); } var container = (IContainer)result.ResolvedObject; var dynamicContainer = new ServiceResolvingContainerProxy(container, request.ResolutionPath); var registration = GetFakeContainerRegistration(dynamicContainer, request.Name); return(ResolutionResult.Success(request.ResolutionPath, dynamicContainer)); }
public void Resolve_resolves_from_proxied_resolver_if_item_is_not_cached(IResolver proxiedResolver, [Registration] IServiceRegistration registration, ICachesResolvedServiceInstances cache, ResolutionRequest request, ISampleService resolved, ResolutionPath resolutionPath) { // Arrange object cachedItem = null; Mock.Get(proxiedResolver).Setup(x => x.GetRegistration(request)).Returns(registration); Mock.Get(proxiedResolver).Setup(x => x.Resolve(request)).Returns(ResolutionResult.Success(resolutionPath, resolved)); Mock.Get(cache).Setup(x => x.TryGet(registration, out cachedItem)).Returns(false); var sut = new CachingResolverProxy(proxiedResolver, cache); // Act var result = sut.Resolve(request); // Assert Assert.That(result.IsSuccess, Is.True); Assert.That(result.ResolvedObject, Is.SameAs(resolved)); }
public void Resolve_returns_container_within_proxy_when_applicable([ResolvesToFailure] IResolver proxiedResolver, [Registration] IServiceRegistration registration, IContainer container) { // Arrange var sut = new DynamicRecursionResolverProxy(proxiedResolver); var request = new ResolutionRequest(typeof(IResolvesServices), new ResolutionPath(registration)); Mock.Get(proxiedResolver) .Setup(x => x.Resolve(It.Is <ResolutionRequest>(r => r.ServiceType == typeof(IContainer)))) .Returns(ResolutionResult.Success(new ResolutionPath(), container)); // Act var result = sut.Resolve(request); // Assert Assert.That(result.IsSuccess, Is.True); Assert.That(result.ResolvedObject, Is.InstanceOf <ServiceResolvingContainerProxy>()); var proxy = (ServiceResolvingContainerProxy)result.ResolvedObject; Assert.That(proxy.ProxiedResolver, Is.SameAs(container)); }