forked from witskeeper/geektime
-
Notifications
You must be signed in to change notification settings - Fork 0
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
weiyu.xiao
committed
Feb 12, 2020
1 parent
8309f49
commit 10d9767
Showing
53 changed files
with
1,578 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
microservices/GeekTime.API/Application/Commands/CreateOrderCommand.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,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
|
||
namespace GeekTime.API.Application.Commands | ||
{ | ||
public class CreateOrderCommand : IRequest<long> | ||
{ | ||
|
||
//ublic CreateOrderCommand() { } | ||
public CreateOrderCommand(int itemCount) | ||
{ | ||
ItemCount = itemCount; | ||
} | ||
|
||
public long ItemCount { get; private set; } | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
microservices/GeekTime.API/Application/Commands/CreateOrderCommandHandler.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,36 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GeekTime.Infrastructure.Repositories; | ||
using GeekTime.Domain.OrderAggregate; | ||
using DotNetCore.CAP; | ||
using GeekTime.API.Application.IntegrationEvents; | ||
|
||
namespace GeekTime.API.Application.Commands | ||
{ | ||
public class CreateOrderCommandHandler : IRequestHandler<CreateOrderCommand, long> | ||
{ | ||
IOrderRepository _orderRepository; | ||
ICapPublisher _capPublisher; | ||
public CreateOrderCommandHandler(IOrderRepository orderRepository, ICapPublisher capPublisher) | ||
{ | ||
_orderRepository = orderRepository; | ||
_capPublisher = capPublisher; | ||
} | ||
|
||
|
||
public async Task<long> Handle(CreateOrderCommand request, CancellationToken cancellationToken) | ||
{ | ||
|
||
var address = new Address("wen san lu", "hangzhou", "310000"); | ||
var order = new Order("xiaohong1999", "xiaohong", 25, address); | ||
|
||
_orderRepository.Add(order); | ||
await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); | ||
return order.Id; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
microservices/GeekTime.API/Application/DomainEventHandlers/OrderCreatedDomainEventHandler.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,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GeekTime.Domain; | ||
using GeekTime.Domain.Events; | ||
using DotNetCore.CAP; | ||
using GeekTime.API.Application.IntegrationEvents; | ||
|
||
namespace GeekTime.API.Application.DomainEventHandlers | ||
{ | ||
public class OrderCreatedDomainEventHandler : IDomainEventHandler<OrderCreatedDomainEvent> | ||
{ | ||
ICapPublisher _capPublisher; | ||
public OrderCreatedDomainEventHandler(ICapPublisher capPublisher) | ||
{ | ||
_capPublisher = capPublisher; | ||
} | ||
|
||
public async Task Handle(OrderCreatedDomainEvent notification, CancellationToken cancellationToken) | ||
{ | ||
await _capPublisher.PublishAsync("OrderCreated", new OrderCreatedIntegrationEvent(notification.Order.Id)); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
microservices/GeekTime.API/Application/IntegrationEvents/ISubscriberService.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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace GeekTime.API.Application.IntegrationEvents | ||
{ | ||
public interface ISubscriberService | ||
{ | ||
void OrderPaymentSucceeded(OrderPaymentSucceededIntegrationEvent @event); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
microservices/GeekTime.API/Application/IntegrationEvents/OrderCreatedIntegrationEvent.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 System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace GeekTime.API.Application.IntegrationEvents | ||
{ | ||
public class OrderCreatedIntegrationEvent | ||
{ | ||
public OrderCreatedIntegrationEvent(long orderId) => OrderId = orderId; | ||
public long OrderId { get; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...vices/GeekTime.API/Application/IntegrationEvents/OrderPaymentSucceededIntegrationEvent.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,9 @@ | ||
namespace GeekTime.API.Application.IntegrationEvents | ||
{ | ||
public class OrderPaymentSucceededIntegrationEvent | ||
{ | ||
public OrderPaymentSucceededIntegrationEvent(long orderId) => OrderId = orderId; | ||
public long OrderId { get; } | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
microservices/GeekTime.API/Application/IntegrationEvents/SubscriberService.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,34 @@ | ||
using DotNetCore.CAP; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
namespace GeekTime.API.Application.IntegrationEvents | ||
{ | ||
public class SubscriberService : ISubscriberService, ICapSubscribe | ||
{ | ||
IMediator _mediator; | ||
public SubscriberService(IMediator mediator) | ||
{ | ||
_mediator = mediator; | ||
} | ||
|
||
|
||
[CapSubscribe("OrderPaymentSucceeded")] | ||
public void OrderPaymentSucceeded(OrderPaymentSucceededIntegrationEvent @event) | ||
{ | ||
//Do SomeThing | ||
} | ||
|
||
[CapSubscribe("OrderCreated")] | ||
public void OrderCreated(OrderCreatedIntegrationEvent @event) | ||
{ | ||
|
||
|
||
|
||
|
||
//Do SomeThing | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
microservices/GeekTime.API/Application/Queries/MyOrderQuery.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,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
namespace GeekTime.API.Application.Queries | ||
{ | ||
public class MyOrderQuery : IRequest<List<string>> | ||
{ | ||
public MyOrderQuery(string userName) => UserName = userName; | ||
|
||
public string UserName { get; private set; } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
microservices/GeekTime.API/Application/Queries/MyOrderQueryHandler.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 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
namespace GeekTime.API.Application.Queries | ||
{ | ||
public class MyOrderQueryHandler : IRequestHandler<MyOrderQuery, List<string>> | ||
{ | ||
public Task<List<string>> Handle(MyOrderQuery request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new List<string>()); | ||
} | ||
} | ||
} |
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,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using GeekTime.API.Application.Commands; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Authorization; | ||
using GeekTime.API.Application.Queries; | ||
|
||
namespace GeekTime.API.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class OrderController : ControllerBase | ||
{ | ||
IMediator _mediator; | ||
public OrderController(IMediator mediator) | ||
{ | ||
_mediator = mediator; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<long> CreateOrder([FromBody]CreateOrderCommand cmd) | ||
{ | ||
return await _mediator.Send(cmd, HttpContext.RequestAborted); | ||
} | ||
|
||
|
||
|
||
[HttpGet] | ||
public async Task<List<string>> QueryOrder([FromBody]MyOrderQuery myOrderQuery) | ||
{ | ||
return await _mediator.Send(myOrderQuery); | ||
} | ||
|
||
#region 不建议的写法 | ||
//[HttpPost] | ||
//public Task<long> CreateOrder([FromBody]CreateOrderVeiwModel viewModel) | ||
//{ | ||
// var model = viewModel.ToModel(); | ||
// return await orderService.CreateOrder(model); | ||
//} | ||
|
||
|
||
//class OrderService:IOrderService | ||
//{ | ||
// public long CreateOrder(CreateOrderModel model) | ||
// { | ||
// var address = new Address("wen san lu", "hangzhou", "310000"); | ||
// var order = new Order("xiaohong1999", "xiaohong", 25, address); | ||
|
||
// _orderRepository.Add(order); | ||
// await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); | ||
// return order.Id; | ||
// } | ||
//} | ||
#endregion | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
microservices/GeekTime.API/Extensions/ApplicationBuilderExtensions.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,7 @@ | ||
namespace GeekTime.API.Extensions | ||
{ | ||
public static class ApplicationBuilderExtensions | ||
{ | ||
|
||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
microservices/GeekTime.API/Extensions/ServiceCollectionExtensions.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,67 @@ | ||
using GeekTime.API.Application.IntegrationEvents; | ||
using GeekTime.Domain.OrderAggregate; | ||
using GeekTime.Infrastructure; | ||
using GeekTime.Infrastructure.Repositories; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace GeekTime.API.Extensions | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
|
||
public static IServiceCollection AddMediatRServices(this IServiceCollection services) | ||
{ | ||
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(DomainContextTransactionBehavior<,>)); | ||
return services.AddMediatR(typeof(Order).Assembly, typeof(Program).Assembly); | ||
} | ||
|
||
|
||
public static IServiceCollection AddDomainContext(this IServiceCollection services, Action<DbContextOptionsBuilder> optionsAction) | ||
{ | ||
return services.AddDbContext<DomainContext>(optionsAction); | ||
} | ||
|
||
public static IServiceCollection AddInMemoryDomainContext(this IServiceCollection services) | ||
{ | ||
return services.AddDomainContext(builder => builder.UseInMemoryDatabase("domanContextDatabase")); | ||
} | ||
|
||
public static IServiceCollection AddMySqlDomainContext(this IServiceCollection services, string connectionString) | ||
{ | ||
return services.AddDomainContext(builder => | ||
{ | ||
builder.UseMySql(connectionString); | ||
}); | ||
} | ||
|
||
|
||
public static IServiceCollection AddRepositories(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IOrderRepository, OrderRepository>(); | ||
return services; | ||
} | ||
|
||
|
||
|
||
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.AddTransient<ISubscriberService, SubscriberService>(); | ||
services.AddCap(options => | ||
{ | ||
options.UseEntityFramework<DomainContext>(); | ||
|
||
options.UseRabbitMQ(options => | ||
{ | ||
configuration.GetSection("RabbitMQ").Bind(options); | ||
}); | ||
//options.UseDashboard(); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} |
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,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NoWarn>$(NoWarn);1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DotNetCore.CAP.Dashboard" Version="3.0.0" /> | ||
<PackageReference Include="DotNetCore.CAP.RabbitMQ" Version="3.0.0" /> | ||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" /> | ||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.0" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\GeekTime.Infrastructure\GeekTime.Infrastructure.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Application\Commands\Models\" /> | ||
<Folder Include="Application\Queries\ViewModels\" /> | ||
<Folder Include="Infrastructure\Auth\" /> | ||
<Folder Include="Infrastructure\Caching\" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
Oops, something went wrong.