-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
327 additions
and
124 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
13 changes: 13 additions & 0 deletions
13
src/framework/Luck.AutoDependencyInjection/Abstractions/IPropertyInjectionServiceProvider.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,13 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Luck.AutoDependencyInjection.Abstractions | ||
{ | ||
|
||
/// <summary> | ||
/// 属性注入提供者接口 | ||
/// </summary> | ||
public interface IPropertyInjectionServiceProvider : IServiceProvider, ISupportRequiredService | ||
{ | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/framework/Luck.AutoDependencyInjection/Abstractions/IPropertyInjector.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,16 @@ | ||
namespace Luck.AutoDependencyInjection.Abstractions | ||
{ | ||
|
||
/// <summary> | ||
/// 属性注入接口 | ||
/// </summary> | ||
internal interface IPropertyInjector | ||
{ | ||
/// <summary> | ||
/// 注入属性 | ||
/// </summary> | ||
/// <param name="instance">要注入的实例</param> | ||
/// <returns></returns> | ||
object InjectProperties(object instance); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...work/Luck.AutoDependencyInjection/PropertyInjection/PropertyInjectionControllerFactory.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,41 @@ | ||
using Luck.AutoDependencyInjection.Abstractions; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
|
||
namespace Luck.AutoDependencyInjection.PropertyInjection | ||
{ | ||
/// <summary> | ||
/// 属性注入控制器我工厂,用来创建控制器,激活控制器 | ||
/// </summary> | ||
internal class PropertyInjectionControllerFactory : IControllerFactory | ||
{ | ||
private readonly IPropertyInjector _propertyInjector; | ||
private readonly IControllerActivator _controllerActivator; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="propertyInjector"></param> | ||
/// <param name="controllerActivator"></param> | ||
public PropertyInjectionControllerFactory(IPropertyInjector propertyInjector, IControllerActivator controllerActivator) | ||
{ | ||
_propertyInjector = propertyInjector; | ||
_controllerActivator = controllerActivator; | ||
} | ||
|
||
/// <summary> | ||
/// 创建控制器 | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <returns></returns> | ||
public object CreateController(ControllerContext context) => _propertyInjector.InjectProperties(_controllerActivator.Create(context)); | ||
|
||
|
||
/// <summary> | ||
/// 替换控制器 | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="controller"></param> | ||
public void ReleaseController(ControllerContext context, object controller) => _controllerActivator.Release(context, controller); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...mework/Luck.AutoDependencyInjection/PropertyInjection/PropertyInjectionServiceProvider.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,40 @@ | ||
using Luck.AutoDependencyInjection.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Luck.AutoDependencyInjection.PropertyInjection | ||
{ | ||
/// <summary> | ||
/// 属性注入提供者接口 | ||
/// </summary> | ||
internal class PropertyInjectionServiceProvider : IPropertyInjectionServiceProvider | ||
{ | ||
private readonly IPropertyInjector _propertyInjector; | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="service"></param> | ||
public PropertyInjectionServiceProvider(IServiceCollection service) | ||
{ | ||
ArgumentNullException.ThrowIfNull(service, nameof(service)); | ||
service.AddSingleton<IPropertyInjectionServiceProvider>(this); | ||
_serviceProvider = service.BuildServiceProvider(); | ||
_propertyInjector = new PropertyInjector(this); | ||
} | ||
|
||
|
||
|
||
public object? GetService(Type serviceType) | ||
{ | ||
var instance = _serviceProvider.GetService(serviceType); | ||
return instance is null ? null : _propertyInjector.InjectProperties(instance); | ||
} | ||
|
||
public object GetRequiredService(Type serviceType) | ||
{ | ||
var service = GetService(serviceType); | ||
return service; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...Luck.AutoDependencyInjection/PropertyInjection/PropertyInjectionServiceProviderFactory.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,33 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Luck.AutoDependencyInjection.PropertyInjection | ||
{ | ||
/// <summary> | ||
/// 属性注入服务提供者工厂 | ||
/// </summary> | ||
internal class PropertyInjectionServiceProviderFactory : IServiceProviderFactory<IServiceCollection> | ||
{ | ||
/// <summary> | ||
/// 创建服务提供者 | ||
/// </summary> | ||
/// <param name="containerBuilder">容器建造者</param> | ||
/// <returns></returns> | ||
public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder) | ||
{ | ||
return new PropertyInjectionServiceProvider(containerBuilder); | ||
} | ||
|
||
/// <summary> | ||
/// 创建构建器 | ||
/// </summary> | ||
/// <param name="services">服务集合</param> | ||
/// <returns></returns> | ||
|
||
public IServiceCollection CreateBuilder(IServiceCollection? services) | ||
{ | ||
|
||
return services ?? new ServiceCollection(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.