Skip to content

Instantly share code, notes, and snippets.

@gyuwon
Last active November 3, 2017 18:50
Show Gist options
  • Save gyuwon/7be3c2cb31404c364ae2d286090ef84b to your computer and use it in GitHub Desktop.
Save gyuwon/7be3c2cb31404c364ae2d286090ef84b to your computer and use it in GitHub Desktop.
public static class HostFunctions
{
public static void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) => services
.AddSingleton<IMessageSerializer, JsonMessageSerializer>()
.AddSingleton(container => GetDbContextOptions(context.Configuration))
.AddTransient<EventStoreDbContext, IdentityEventStoreDbContext>()
.AddSingleton<Func<EventStoreDbContext>>(container => container.GetRequiredService<EventStoreDbContext>)
.AddSingleton<ISqlEventStore, SqlEventStore>()
.AddSingleton<ISqlEventPublisher, SqlEventPublisher>()
.AddSingleton<ISqlEventSourcedRepository<User>, UserRepository>()
.AddSingleton(GetMessageHandler)
.AddMvcCore().AddJsonFormatters();
private static DbContextOptions GetDbContextOptions(IConfiguration configuration)
{
return new DbContextOptionsBuilder().UseSqlServer(configuration.GetConnectionString("EventStore")).Options;
}
private static IMessageHandler GetMessageHandler(IServiceProvider container)
{
return new UserCommandHandler(container.GetRequiredService<ISqlEventSourcedRepository<User>>());
}
public static void ConfigureApp(IApplicationBuilder app)
{
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
// Production
public class Program
{
public static void Main(string[] args)
{
IWebHost webHost = WebHost
.CreateDefaultBuilder(args)
.ConfigureServices(HostFunctions.ConfigureServices)
.Configure(HostFunctions.ConfigureApp)
.Build();
IServiceProvider serviceProvider = webHost.Services;
using (var dbContext = serviceProvider.GetRequiredService<EventStoreDbContext>())
{
dbContext.Database.Migrate();
}
webHost.Run();
}
}
// In-memory testing
public abstract class given_in_process_test_server : GivenWhenThen
{
protected TestServer InProcessServer { get; private set; }
protected DbContextOptions EventStoreDbContextOptions =>
new DbContextOptionsBuilder()
.UseInMemoryDatabase(typeof(given_in_process_test_server).FullName)
.ConfigureWarnings(warnings => warnings.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.Options;
protected InProcessMessageLogger InProcessMessageBus { get; } = new InProcessMessageLogger();
protected override Task Given()
{
IWebHostBuilder builder = new WebHostBuilder()
.ConfigureServices(HostFunctions.ConfigureServices)
.ConfigureServices(ConfigureInProcessServices)
.Configure(HostFunctions.ConfigureApp);
InProcessServer = new TestServer(builder);
return Task.CompletedTask;
}
private void ConfigureInProcessServices(IServiceCollection services) => services
.AddSingleton(EventStoreDbContextOptions)
.AddSingleton<IMessageBus>(InProcessMessageBus);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment