Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use instance of IContainerProvider to resolve dependencies #2847

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public object Resolve(Type type, params (Type Type, object Instance)[] parameter
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, ex);
throw new ContainerResolutionException(type, ex, this);
}
}

Expand All @@ -329,7 +329,7 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, name, ex);
throw new ContainerResolutionException(type, name, ex, this);
}
}

Expand Down Expand Up @@ -427,7 +427,7 @@ public object Resolve(Type type, params (Type Type, object Instance)[] parameter
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, ex);
throw new ContainerResolutionException(type, ex, this);
}
}

Expand All @@ -443,7 +443,7 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, name, ex);
throw new ContainerResolutionException(type, name, ex, this);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Containers/Prism.Unity.Shared/UnityContainerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public object Resolve(Type type, params (Type Type, object Instance)[] parameter
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, ex);
throw new ContainerResolutionException(type, ex, this);
}
}

Expand All @@ -327,7 +327,7 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, name, ex);
throw new ContainerResolutionException(type, name, ex, this);
}
}

Expand Down Expand Up @@ -424,7 +424,7 @@ public object Resolve(Type type, params (Type Type, object Instance)[] parameter
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, ex);
throw new ContainerResolutionException(type, ex, this);
}
}

Expand All @@ -441,7 +441,7 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
}
catch (Exception ex)
{
throw new ContainerResolutionException(type, name, ex);
throw new ContainerResolutionException(type, name, ex, this);
}
}
}
Expand Down
45 changes: 26 additions & 19 deletions src/Prism.Core/Ioc/ContainerResolutionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public class ContainerResolutionException : Exception
/// </remarks>
public const string UnknownError = "You seem to have hit an edge case. Please file an issue with the Prism team along with a duplication.";

private IContainerProvider _instance { get; }
/// <summary>
/// Creates a new instance of the <see cref="ContainerResolutionException"/>
/// </summary>
/// <param name="serviceType">The failed Service <see cref="Type"/> that was attempted to be resolved.</param>
/// <param name="innerException">The actual <see cref="Exception"/> thrown by the Container.</param>
public ContainerResolutionException(Type serviceType, Exception innerException)
: this(serviceType, null, innerException)
/// <param name="instance">The instance of the container that was used to resolve the service type.</param>
public ContainerResolutionException(Type serviceType, Exception innerException, IContainerProvider instance)
: this(serviceType, null, innerException, instance)
{
}

Expand All @@ -62,17 +64,20 @@ public ContainerResolutionException(Type serviceType, Exception innerException)
/// <param name="serviceType">The failed Service <see cref="Type"/> that was attempted to be resolved.</param>
/// <param name="serviceName">The Service Name/Key used to resolve the Service Type.</param>
/// <param name="innerException">The actual <see cref="Exception"/> thrown by the Container.</param>
public ContainerResolutionException(Type serviceType, string serviceName, Exception innerException)
/// <param name="instance">The instance of the container that was used to resolve the service type.</param>
public ContainerResolutionException(Type serviceType, string serviceName, Exception innerException, IContainerProvider instance)
: base(GetErrorMessage(serviceType, serviceName), innerException)
{
_instance = instance;
ServiceType = serviceType;
ServiceName = serviceName;
}

// Used by GetErrors()
private ContainerResolutionException(Type serviceType, string message)
private ContainerResolutionException(Type serviceType, string message, IContainerProvider instance)
: base(message)
{
_instance = instance;
ServiceType = serviceType;
}

Expand Down Expand Up @@ -125,12 +130,12 @@ public ContainerResolutionErrorCollection GetErrors()
var implementingType = TryFindImplementingType();
if (implementingType is null)
{
errors.Add(ServiceType, new ContainerResolutionException(ServiceType, MissingRegistration));
errors.Add(ServiceType, new ContainerResolutionException(ServiceType, MissingRegistration, _instance));
return errors;
}
else if (implementingType.IsAbstract)
{
errors.Add(ServiceType, new ContainerResolutionException(implementingType, CannotResolveAbstractType));
errors.Add(ServiceType, new ContainerResolutionException(implementingType, CannotResolveAbstractType, _instance));
}

PopulateErrors(implementingType, ref errors);
Expand All @@ -139,58 +144,60 @@ public ContainerResolutionErrorCollection GetErrors()

private Type TryFindImplementingType()
{
var container = ContainerLocator.Current;
var name = ServiceName;

// A ViewModel generally isn't directly registered with the container
// as a result we want to provide the ServiceType when it's a concrete type.
var defaultValue = IsConcreteType(ServiceType) ? ServiceType : null;
if (string.IsNullOrEmpty(name))
{
if (!container.IsRegistered(ServiceType))
if (!_instance.IsRegistered(ServiceType))
{
return defaultValue;
}

return container.GetRegistrationType(ServiceType);
// RegistrationType must be retrieved from the Root Container
return ContainerLocator.Current.GetRegistrationType(ServiceType);
}
else if (!container.IsRegistered(ServiceType, ServiceName))
else if (!_instance.IsRegistered(ServiceType, ServiceName))
{
return defaultValue;
}

return container.GetRegistrationType(name);
// RegistrationType must be retrieved from the Root Container
return ContainerLocator.Current.GetRegistrationType(name);
}

[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "This method is meant to collect any exception thrown.")]
private static void PopulateErrors(Type implementingType, ref ContainerResolutionErrorCollection errors)
private void PopulateErrors(Type implementingType, ref ContainerResolutionErrorCollection errors)
{
var ctors = implementingType.GetConstructors();

if (ctors.Length > 1)
{
errors.Add(implementingType, new ContainerResolutionException(implementingType, MultipleConstructors));
errors.Add(implementingType, new ContainerResolutionException(implementingType, MultipleConstructors, _instance));
}
else if (ctors.Length == 0)
{
errors.Add(implementingType, new ContainerResolutionException(implementingType, NoPublicConstructors));
errors.Add(implementingType, new ContainerResolutionException(implementingType, NoPublicConstructors, _instance));
return;
}

var ctor = ctors.OrderByDescending(x => x.GetParameters().Length).FirstOrDefault();
var parameters = ctor.GetParameters();
var parameterInstances = new List<object>();
var container = ContainerLocator.Current;
foreach (var parameter in parameters)
{
try
{
var defaultImplementingType = IsConcreteType(parameter.ParameterType) ? parameter.ParameterType : null;
var parameterImplementingType = container.GetRegistrationType(parameter.ParameterType);

// RegistrationType must be retrieved from the Root Container
var parameterImplementingType = ContainerLocator.Current.GetRegistrationType(parameter.ParameterType);
if (parameterImplementingType is null)
throw new ContainerResolutionException(parameter.ParameterType, MissingRegistration);
throw new ContainerResolutionException(parameter.ParameterType, MissingRegistration, _instance);

var instance = container.Resolve(parameter.ParameterType);
var instance = _instance.Resolve(parameter.ParameterType);
parameterInstances.Add(instance);
}
catch (Exception ex)
Expand Down Expand Up @@ -219,7 +226,7 @@ private static void PopulateErrors(Type implementingType, ref ContainerResolutio
// If we managed to create an instance for every parameter and the
// constructor didn't throw an exception when activating the instance
// we really aren't sure what allowed us to get here...
throw new ContainerResolutionException(implementingType, UnknownError);
throw new ContainerResolutionException(implementingType, UnknownError, _instance);
}
catch (TargetInvocationException tie)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void WhenViewCannotBeCreated_ThenThrowsAnException()
{
var containerMock = new Mock<IContainerExtension>();
containerMock.Setup(x => x.Resolve(typeof(object), typeof(TestView).Name))
.Throws(new ContainerResolutionException(typeof(object), typeof(TestView).Name, null));
.Throws(new ContainerResolutionException(typeof(object), typeof(TestView).Name, null, containerMock.Object));
containerMock.Setup(x => x.Resolve(typeof(IActiveRegionHelper)))
.Returns(new RegionResolverOverrides());

Expand Down