/// <summary> /// Resolves an instance of the type registered on the service. /// </summary> /// <param name="serviceType">The type of the service.</param> /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param> /// <returns>An instance of the type registered on the service.</returns> /// <exception cref="ArgumentNullException">The <paramref name="serviceType" /> is <c>null</c>.</exception> /// <exception cref="NotSupportedException">The type is not found in any container.</exception> /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks> public object ResolveType(Type serviceType, object tag = null) { Argument.IsNotNull("serviceType", serviceType); bool isTypeRegistered = false; lock (_lockObject) { isTypeRegistered = IsTypeRegistered(serviceType, tag); } if (!isTypeRegistered) { if (CanResolveNonAbstractTypesWithoutRegistration && serviceType.IsClassEx() && !serviceType.IsAbstractEx()) { return(TypeFactory.CreateInstance(serviceType)); } var error = string.Format("The type '{0}' is not registered", serviceType.FullName); Log.Error(error); throw new NotSupportedException(error); } lock (_lockObject) { var serviceInfo = new ServiceInfo(serviceType, tag); if (_registeredInstances.ContainsKey(serviceInfo)) { return(_registeredInstances[serviceInfo].ImplementingInstance); } // If a type is registered, the original container is always known return(ResolveTypeFromKnownContainer(serviceType, tag)); } }
/// <summary> /// Creates the service instance. /// </summary> /// <param name="registration">The registration.</param> /// <returns>The service instance.</returns> private object CreateServiceInstance(ServiceLocatorRegistration registration) { object instance; if (SupportDependencyInjection) { instance = TypeFactory.CreateInstance(registration.ImplementingType); } else { instance = TypeFactory.CreateInstanceUsingActivator(registration.ImplementingType); } if (instance != null) { var handler = TypeInstantiated; if (handler != null) { handler(this, new TypeInstantiatedEventArgs(registration.DeclaringType, registration.ImplementingType, registration.Tag, registration.RegistrationType)); } } return(instance); }