Skip to content

Commit

Permalink
修改冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
KawhiWei committed Jul 31, 2023
2 parents be48505 + 1e1c59e commit a3edaa2
Show file tree
Hide file tree
Showing 20 changed files with 327 additions and 124 deletions.
7 changes: 7 additions & 0 deletions Luck.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Luck.AppModule", "src\frame
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Luck.AutoDependencyInjection", "src\framework\Luck.AutoDependencyInjection\Luck.AutoDependencyInjection.csproj", "{404C5BB1-243A-4BB8-8FFF-740A2A5A1F5C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Luck.EventBus.Kafka", "src\framework\Luck.EventBus.Kafka\Luck.EventBus.Kafka.csproj", "{9876D845-5C04-4B04-84A0-A57148253666}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -145,6 +147,10 @@ Global
{404C5BB1-243A-4BB8-8FFF-740A2A5A1F5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{404C5BB1-243A-4BB8-8FFF-740A2A5A1F5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{404C5BB1-243A-4BB8-8FFF-740A2A5A1F5C}.Release|Any CPU.Build.0 = Release|Any CPU
{9876D845-5C04-4B04-84A0-A57148253666}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9876D845-5C04-4B04-84A0-A57148253666}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9876D845-5C04-4B04-84A0-A57148253666}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9876D845-5C04-4B04-84A0-A57148253666}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -176,6 +182,7 @@ Global
{2893A93A-E580-4137-8229-BEB5EACA86E8} = {50FF08B1-264C-40B1-BA12-91A57B3C896D}
{CD68536D-E0DF-44E0-9243-14E9A44DCF07} = {DE49CCE6-B41E-493A-ADCC-EA6075DD0038}
{404C5BB1-243A-4BB8-8FFF-740A2A5A1F5C} = {DE49CCE6-B41E-493A-ADCC-EA6075DD0038}
{9876D845-5C04-4B04-84A0-A57148253666} = {5779CF18-7FD7-434D-B47A-1C8A50A3E5A4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4719568B-E10A-4815-AAAA-8C6D6CEE798F}
Expand Down
25 changes: 13 additions & 12 deletions sample/Module.Sample/Controllers/TestsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Luck.Framework.Extensions;
using Microsoft.AspNetCore.Http;
using Luck.AutoDependencyInjection.Attributes;
using Luck.Framework.Extensions;
using Microsoft.AspNetCore.Mvc;
using Module.Sample.Services;
using System.ComponentModel;
Expand All @@ -11,32 +11,33 @@ namespace Module.Sample.Controllers
public class TestsController : ControllerBase
{

[Injection]
private readonly IOrderService _orderService;

public TestsController(IOrderService orderService)
{
_orderService = orderService;
}
//public TestsController(IOrderService orderService)
//{
// _orderService = orderService;
//}


[HttpGet]
public Task TestEnumToList()
{

var list= typeof(TestEnum).TypeToEnumList();
return Task.FromResult(list);
var list = typeof(TestEnum).TypeToEnumList();

return Task.FromResult(list);
}

[HttpPost]
public Task CreateAndEventAsync()
public Task CreateAndEventAsync()
{

return _orderService.CreateAndEventAsync();
}

[HttpPost]
public Task CreateOrder()
public Task CreateOrder()
{

return _orderService.CreateAndEventAsync();
Expand Down
13 changes: 7 additions & 6 deletions sample/Module.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Diagnostics;
using Luck.Framework.Infrastructure;
using MediatR;
using Module.Sample;
using Luck.AppModule;
using Luck.AutoDependencyInjection;
using Luck.AutoDependencyInjection.PropertyInjection;
using Module.Sample;
using System.Diagnostics;


var builder = WebApplication.CreateBuilder(args);
Expand All @@ -15,8 +13,11 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();

//Ìí¼ÓÕâ¸ö¡£
builder.Host.UsePropertyInjection();
// builder.Services.AddDoveLogger();
builder.Host.UseDefaultPropertyInjection();


var app = builder.Build();

Expand Down
21 changes: 7 additions & 14 deletions sample/Module.Sample/Services/OrderService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Luck.AutoDependencyInjection.Attributes;
using Luck.DDD.Domain.Repositories;
using Luck.Framework.Extensions;
using Luck.Framework.Infrastructure.Caching;
using Luck.Framework.Infrastructure.DependencyInjectionModule;
using Luck.Framework.UnitOfWorks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Module.Sample.Domain;
using Module.Sample.EventHandlers;
Expand All @@ -14,27 +11,22 @@ namespace Module.Sample.Services
public class OrderService : IOrderService
{

[Injection]
private readonly IAggregateRootRepository<Order, string> _aggregateRootRepository;
//[Injection]
[Injection]
private readonly IUnitOfWork _unitOfWork;
private readonly IMediator _mediator;

public OrderService(IAggregateRootRepository<Order, string> aggregateRootRepository
, IUnitOfWork unitOfWork
//, IMediator mediator
)
{
_aggregateRootRepository = aggregateRootRepository;
_unitOfWork = unitOfWork;
//_mediator = mediator;
}

[Injection]
private readonly ILogger<OrderService> _logger;

public async Task CreateAsync()
{
var order = new Order("asdasdsa", "asdasdadas");
order.SetOrderItem();

_aggregateRootRepository.Add(order);
_logger?.LogInformation("调用了CreateAsync()方法");
await _unitOfWork.CommitAsync();
}

Expand All @@ -48,6 +40,7 @@ public async Task CreateAndEventAsync()
var order = new Order("asdasdsa", "asdasdadas");
_aggregateRootRepository.Add(order);
order.AddDomainEvent(new OrderCreatedEto() { Id = order.Id, Name = order.Name });
_logger?.LogInformation("调用了CreateAndEventAsync()方法");
await _unitOfWork.CommitAsync();
//await _cache.AddAsync("order_a1", order);
//var test=await _cache.GetAsync<Order>("order_a1");
Expand Down
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
{

}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Luck.AppModule\Luck.AppModule.csproj" />
</ItemGroup>
Expand Down
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);
}
}
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;
}
}
}
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();
}

}
}
Loading

0 comments on commit a3edaa2

Please sign in to comment.