Skip to content

Commit

Permalink
添加微服务工程
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyu.xiao committed Feb 12, 2020
1 parent 8309f49 commit 10d9767
Show file tree
Hide file tree
Showing 53 changed files with 1,578 additions and 0 deletions.
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; }
}
}
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;
}
}
}
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));
}
}
}
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);
}
}
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; }
}
}
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; }
}

}
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 microservices/GeekTime.API/Application/Queries/MyOrderQuery.cs
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; }
}
}
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>());
}
}
}
61 changes: 61 additions & 0 deletions microservices/GeekTime.API/Controllers/OrderController.cs
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace GeekTime.API.Extensions
{
public static class ApplicationBuilderExtensions
{

}
}
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;
}
}
}
34 changes: 34 additions & 0 deletions microservices/GeekTime.API/GeekTime.API.csproj
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>
Loading

0 comments on commit 10d9767

Please sign in to comment.