Skip to content

Commit

Permalink
Adding the basic work-able flow to demonstrate the event handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
daxnet committed Dec 19, 2017
1 parent 8ab4351 commit b2f5bb9
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 2 deletions.
Binary file added docs/class_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/design.asta
Binary file not shown.
2 changes: 0 additions & 2 deletions src/EdaSample.EventBus.Simple/PassThroughEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void Subscribe()

#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls

void Dispose(bool disposing)
{
if (!disposedValue)
Expand All @@ -47,7 +46,6 @@ void Dispose(bool disposing)
disposedValue = true;
}
}

public void Dispose() => Dispose(true);
#endregion
}
Expand Down
29 changes: 29 additions & 0 deletions src/EdaSample.Services.Customer/Controllers/CustomersController.cs
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
Expand All @@ -16,4 +20,9 @@
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EdaSample.Common\EdaSample.Common.csproj" />
<ProjectReference Include="..\EdaSample.EventBus.Simple\EdaSample.EventBus.Simple.csproj" />
</ItemGroup>

</Project>
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 src/EdaSample.Services.Customer/Events/CustomerCreatedEvent.cs
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; }
}
}
9 changes: 9 additions & 0 deletions src/EdaSample.Services.Customer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EdaSample.Common.Events;
using EdaSample.EventBus.Simple;
using EdaSample.Services.Customer.EventHandlers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand All @@ -24,11 +27,17 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddTransient<IEventHandler, CustomerCreatedEventHandler>();
services.AddSingleton<IEventBus, PassThroughEventBus>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand Down
3 changes: 3 additions & 0 deletions src/EdaSample.Services.Customer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"Default": "Warning"
}
}
},
"mssql": {
"connectionString": "Server=localhost\\sqlexpress;Database=EdaSample;User Id=eda;Password=eda;"
}
}

0 comments on commit b2f5bb9

Please sign in to comment.