Skip to content

Instantly share code, notes, and snippets.

@JamesRandall
Created December 17, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesRandall/e69199367c7f5abc85dadff1613efed3 to your computer and use it in GitHub Desktop.
Save JamesRandall/e69199367c7f5abc85dadff1613efed3 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding;
using AzureFromTheTrenches.Commanding.Abstractions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RestAspNetCoreExample.Commands;
using RestAspNetCoreExample.Model;
namespace RestAspNetCoreExample
{
public class Startup
{
private IServiceProvider _serviceProvider;
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
CommandingDependencyResolverAdapter adapter = new CommandingDependencyResolverAdapter(
(fromType, toInstance) => services.AddSingleton(fromType, toInstance),
(fromType, toType) => services.AddTransient(fromType, toType),
resolveType => _serviceProvider.GetService(resolveType)
);
ICommandRegistry registry = adapter.AddCommanding();
registry.Discover<Startup>();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
_serviceProvider = app.ApplicationServices;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
[ApiController]
[Route("todo")]
public class CreateTodoItemController : ControllerBase
{
private readonly ICommandDispatcher _dispatcher;
public CreateTodoItemController(ICommandDispatcher dispatcher)
{
_dispatcher = dispatcher;
}
[HttpPost]
public async Task<ActionResult<TodoItem>> Handler(CreateTodoItemCommand command)
{
TodoItem result = await _dispatcher.DispatchAsync(command);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment