private IContainer CreateIoCContainer(IServiceCollection services)
{
// todo: move these to a proper autofac module
// Register application services.
services.AddSingleton(x => Configuration);
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddTransient<IDetermineIfATaskIsEditable, DetermineIfATaskIsEditable>();
services.AddTransient<IValidateEventEditViewModels, EventEditViewModelValidator>();
services.AddTransient<ITaskEditViewModelValidator, TaskEditViewModelValidator>();
services.AddTransient<IItineraryEditModelValidator, ItineraryEditModelValidator>();
services.AddTransient<IOrganizationEditModelValidator, OrganizationEditModelValidator>();
services.AddTransient<IRedirectAccountControllerRequests, RedirectAccountControllerRequests>();
services.AddSingleton<IImageService, ImageService>();
services.AddTransient<SampleDataGenerator>();
if (Configuration["Geocoding:EnableGoogleGeocodingService"] == "true")
{
//This setting is false by default. To enable Google geocoding you will
//need to override this setting in your user secrets or env vars.
//Visit https://developers.google.com/maps/documentation/geocoding/get-api-key to get a free standard usage API key
services.AddSingleton<IGeocoder>(new GoogleGeocoder(Configuration["Geocoding:GoogleGeocodingApiKey"]));
}
else
{
//implement null object pattern to protect against IGeocoder not being passed a legit reference at runtime b/c of the above conditional
services.AddSingleton<IGeocoder>(new NullObjectGeocoder());
}
if (Configuration["Data:Storage:EnableAzureQueueService"] == "true")
{
// This setting is false by default. To enable queue processing you will
// need to override the setting in your user secrets or env vars.
services.AddTransient<IQueueStorageService, QueueStorageService>();
}
else
{
// this writer service will just write to the default logger
services.AddTransient<IQueueStorageService, FakeQueueWriterService>();
//services.AddTransient<IQueueStorageService, SmtpEmailSender>();
}
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterSource(new ContravariantRegistrationSource());
containerBuilder.RegisterAssemblyTypes(typeof(IMediator).Assembly).AsImplementedInterfaces();
containerBuilder.RegisterAssemblyTypes(typeof(Startup).Assembly).AsImplementedInterfaces();
containerBuilder.Register<SingleInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
containerBuilder.Register<MultiInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
});
//ExternalUserInformationProviderFactory registration
containerBuilder.RegisterType<TwitterExternalUserInformationProvider>().Named<IProvideExternalUserInformation>("Twitter");
containerBuilder.RegisterType<GoogleExternalUserInformationProvider>().Named<IProvideExternalUserInformation>("Google");
containerBuilder.RegisterType<MicrosoftAndFacebookExternalUserInformationProvider>().Named<IProvideExternalUserInformation>("Microsoft");
containerBuilder.RegisterType<MicrosoftAndFacebookExternalUserInformationProvider>().Named<IProvideExternalUserInformation>("Facebook");
containerBuilder.RegisterType<ExternalUserInformationProviderFactory>().As<IExternalUserInformationProviderFactory>();
//Hangfire
containerBuilder.Register(icomponentcontext => new BackgroundJobClient(new SqlServerStorage(Configuration["Data:HangfireConnection:ConnectionString"])))
.As<IBackgroundJobClient>();
//auto-register Hangfire jobs by convention
//http://docs.autofac.org/en/latest/register/scanning.html
var assembly = Assembly.GetExecutingAssembly();
containerBuilder
.RegisterAssemblyTypes(assembly)
.Where(t => t.Namespace == "AllReady.Hangfire.Jobs" && t.IsInterface)
.AsImplementedInterfaces();
containerBuilder.RegisterType<GoogleOptimizeRouteService>().As<IOptimizeRouteService>().SingleInstance();
//Populate the container with services that were previously registered
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return container;
}