-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the basic work-able flow to demonstrate the event handling.
- Loading branch information
Showing
9 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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
29 changes: 29 additions & 0 deletions
29
src/EdaSample.Services.Customer/Controllers/CustomersController.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,29 @@ | ||
using EdaSample.Common.Events; | ||
using EdaSample.Services.Customer.Events; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace EdaSample.Services.Customer.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
public class CustomersController : Controller | ||
{ | ||
private readonly IEventBus eventBus; | ||
|
||
public CustomersController(IEventBus eventBus) | ||
{ | ||
this.eventBus = eventBus; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Create([FromBody] dynamic model) | ||
{ | ||
var customerName = (string)model.Name; | ||
await this.eventBus.PublishAsync(new CustomerCreatedEvent(customerName)); | ||
return Ok(); | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/EdaSample.Services.Customer/EventHandlers/CustomerCreatedEventHandler.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,24 @@ | ||
using EdaSample.Common.Events; | ||
using EdaSample.Services.Customer.Events; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace EdaSample.Services.Customer.EventHandlers | ||
{ | ||
public class CustomerCreatedEventHandler : IEventHandler<CustomerCreatedEvent> | ||
{ | ||
public bool CanHandle(IEvent @event) | ||
=> @event.GetType().Equals(typeof(CustomerCreatedEvent)); | ||
|
||
public Task<bool> HandleAsync(CustomerCreatedEvent @event, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(true); | ||
} | ||
|
||
public Task<bool> HandleAsync(IEvent @event, CancellationToken cancellationToken = default) | ||
=> CanHandle(@event) ? HandleAsync((CustomerCreatedEvent)@event, cancellationToken) : Task.FromResult(false); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/EdaSample.Services.Customer/Events/CustomerCreatedEvent.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,24 @@ | ||
using EdaSample.Common.Events; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace EdaSample.Services.Customer.Events | ||
{ | ||
public class CustomerCreatedEvent : IEvent | ||
{ | ||
public CustomerCreatedEvent(string customerName) | ||
{ | ||
this.Id = Guid.NewGuid(); | ||
this.Timestamp = DateTime.UtcNow; | ||
this.CustomerName = customerName; | ||
} | ||
|
||
public Guid Id { get; } | ||
|
||
public DateTime Timestamp { get; } | ||
|
||
public string CustomerName { get; } | ||
} | ||
} |
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