Last active
November 3, 2017 18:50
-
-
Save gyuwon/7be3c2cb31404c364ae2d286090ef84b to your computer and use it in GitHub Desktop.
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
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