-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v8 updates - fixes for MS Extensions DI
- Loading branch information
Showing
30 changed files
with
368 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
src/Prism.Microsoft.DependencyInjection.Extensions/ConcreteAwareOverrideProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Prism.Microsoft.DependencyInjection.Extensions | ||
{ | ||
internal class ConcreteAwareOverrideProvider : IServiceProvider | ||
{ | ||
private IServiceProvider _rootProvider { get; } | ||
private IServiceCollection _services { get; } | ||
private (Type type, object instance)[] _overrides { get; } | ||
|
||
public ConcreteAwareOverrideProvider(IServiceProvider serviceProvider, IServiceCollection services, (Type type, object instance)[] overrides) | ||
{ | ||
_rootProvider = serviceProvider; | ||
_overrides = overrides; | ||
} | ||
|
||
public object GetService(Type serviceType) | ||
{ | ||
if (!serviceType.IsAbstract && serviceType.IsClass && serviceType != typeof(object)) | ||
{ | ||
return BuildInstance(serviceType); | ||
} | ||
|
||
var serviceDescriptor = _services.LastOrDefault(x => x.ServiceType == serviceType); | ||
|
||
if (serviceDescriptor?.ImplementationType is null) | ||
return _rootProvider.GetService(serviceType); | ||
|
||
var implType = serviceDescriptor.ImplementationType; | ||
return BuildInstance(implType); | ||
} | ||
|
||
private object BuildInstance(Type implType) | ||
{ | ||
var constructors = implType.GetConstructors(); | ||
|
||
if (constructors is null || !constructors.Any()) | ||
return Activator.CreateInstance(implType); | ||
|
||
var ctor = constructors.OrderByDescending(x => x.GetParameters().Length).First(); | ||
var parameters = ctor.GetParameters().Select(x => | ||
{ | ||
(var t, var instance) = _overrides.FirstOrDefault(o => x.ParameterType == o.type); | ||
if (t != null) | ||
{ | ||
return instance; | ||
} | ||
return _rootProvider.GetService(x.ParameterType); | ||
}).ToArray(); | ||
|
||
return ctor.Invoke(parameters); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.