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

Shell page dependency injection now uses IServiceProvider.GetService() #4281

Merged
merged 2 commits into from
Feb 1, 2022
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
4 changes: 2 additions & 2 deletions src/Controls/src/Core/Routing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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 @@ -250,7 +250,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 @@ -70,7 +70,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; }
}
}
}