Skip to content

Commit

Permalink
Shell page dependency injection now uses IServiceProvider.GetService() (
Browse files Browse the repository at this point in the history
#4281)

* Shell page dependency injection now uses IServiceProvider.GetService()

* Added test case
  • Loading branch information
brunck authored Feb 1, 2022
1 parent 678ed3f commit 815ea34
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Controls/src/Core/Routing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static Element GetOrCreateContent(string route, IServiceProvider services
{
if (services != null)
{
result = Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(services, type) as Element;
result = (services.GetService(type) ?? Activator.CreateInstance(type)) as Element;
}
else
{
Expand Down Expand Up @@ -260,7 +260,7 @@ public override Element GetOrCreate(IServiceProvider services)
{
if (services != null)
{
return Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(services, _type) as Element;
return (services.GetService(_type) ?? Activator.CreateInstance(_type)) as Element;
}
return Activator.CreateInstance(_type) as Element;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Shell/ShellContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Page IShellContentController.GetOrCreateContent()
var services = Parent?.FindMauiContext()?.Services;
if (services != null)
{
return Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(services, template.Type);
return services.GetService(template.Type) ?? Activator.CreateInstance(template.Type);
}
return Activator.CreateInstance(template.Type);
};
Expand Down
24 changes: 22 additions & 2 deletions src/Controls/tests/Core.UnitTests/ShellNavigatingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,15 @@ public async Task RouteWithGlobalPageRoute()
}

[TestCase(typeof(PageWithDependency), typeof(PageWithDependency))]
[TestCase(typeof(PageWithDependencyAndMultipleConstructors), typeof(PageWithDependencyAndMultipleConstructors))]
[TestCase(typeof(PageWithDependency), typeof(Dependency))]
[TestCase(typeof(PageWithUnregisteredDependencyAndParameterlessConstructor), typeof(PageWithUnregisteredDependencyAndParameterlessConstructor))]
public async Task GlobalRouteWithDependencyResolution(Type typeForRouteName, Type type)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<Dependency>();
serviceCollection.AddTransient<PageWithDependency>();
serviceCollection.AddTransient<PageWithDependencyAndMultipleConstructors>();
IServiceProvider services = serviceCollection.BuildServiceProvider();
var fakeMauiContext = Substitute.For<IMauiContext>();
var fakeHandler = Substitute.For<IElementHandler>();
Expand All @@ -650,8 +654,24 @@ public async Task GlobalRouteWithDependencyResolution(Type typeForRouteName, Typ
Assert.IsNotNull(shell.Navigation.NavigationStack);
var page = shell.Navigation.NavigationStack[1];
Assert.That(page, Is.Not.Null);
Assert.IsInstanceOf<PageWithDependency>(page);
Assert.That((page as PageWithDependency).TestDependency, Is.Not.Null);
if (type == typeof(PageWithDependency) || type == typeof(Dependency))
{
Assert.IsInstanceOf<PageWithDependency>(page);
Assert.That((page as PageWithDependency).TestDependency, Is.Not.Null);
}

if (type == typeof(PageWithDependencyAndMultipleConstructors))
{
Assert.IsInstanceOf<PageWithDependencyAndMultipleConstructors>(page);
var testPage = page as PageWithDependencyAndMultipleConstructors;
Assert.That(testPage.TestDependency, Is.Not.Null);
Assert.That(testPage.OtherTestDependency, Is.Null);
}

if (type == typeof(PageWithUnregisteredDependencyAndParameterlessConstructor))
{
Assert.IsInstanceOf<PageWithUnregisteredDependencyAndParameterlessConstructor>(page);
}
}

[Test]
Expand Down
39 changes: 39 additions & 0 deletions src/Controls/tests/Core.UnitTests/ShellTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,48 @@ public PageWithDependency(Dependency dependency)
}
}

public class PageWithDependencyAndMultipleConstructors : ContentPage
{
public Dependency TestDependency { get; set; }
public UnregisteredDependency OtherTestDependency { get; set; }

public PageWithDependencyAndMultipleConstructors(Dependency dependency)
{
TestDependency = dependency;
}

public PageWithDependencyAndMultipleConstructors(Dependency dependency, UnregisteredDependency unregisteredDependency)
{
OtherTestDependency = unregisteredDependency;
}

public PageWithDependencyAndMultipleConstructors()
{
// parameterless constructor
}
}

public class PageWithUnregisteredDependencyAndParameterlessConstructor : ContentPage
{
public PageWithUnregisteredDependencyAndParameterlessConstructor(UnregisteredDependency dependency)
{

}

public PageWithUnregisteredDependencyAndParameterlessConstructor()
{

}
}

public class Dependency
{
public int Test { get; set; }
}

public class UnregisteredDependency
{
public int Test { get; set; }
}
}
}

0 comments on commit 815ea34

Please sign in to comment.