Skip to content

Commit 590e696

Browse files
Merge pull request #60 from StuartFerguson/task/#25_removeautofac
Autofac Removed
2 parents 5cf1412 + 29e9691 commit 590e696

5 files changed

Lines changed: 94 additions & 132 deletions

File tree

TransactionProcessor.Tests/General/BootstrapperTests.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6-
using Autofac;
7-
using Autofac.Extensions.DependencyInjection;
6+
using System.Linq;
87
using Microsoft.AspNetCore.Hosting;
98
using Microsoft.Extensions.Configuration;
109
using Microsoft.Extensions.DependencyInjection;
@@ -31,22 +30,13 @@ public void VerifyBootstrapperIsValid()
3130

3231
IServiceCollection services = new ServiceCollection();
3332
Startup s = new Startup(hostingEnvironment.Object);
34-
s.ConfigureServices(services);
35-
3633
Startup.Configuration = this.SetupMemoryConfiguration();
37-
this.AddTestRegistrations(services, hostingEnvironment.Object);
3834

39-
ContainerBuilder builder = new ContainerBuilder();
40-
builder.Populate(services);
41-
42-
s.ConfigureContainer(builder);
35+
s.ConfigureServices(services);
4336

44-
IContainer container = builder.Build();
37+
this.AddTestRegistrations(services, hostingEnvironment.Object);
4538

46-
using(ILifetimeScope scope = container.BeginLifetimeScope())
47-
{
48-
scope.ResolveAll(new List<String>());
49-
}
39+
services.AssertConfigurationIsValid();
5040
}
5141

5242
private IConfigurationRoot SetupMemoryConfiguration()
@@ -86,4 +76,28 @@ private void AddTestRegistrations(IServiceCollection services,
8676

8777
#endregion
8878
}
79+
80+
public static class ServiceCollectionExtensions
81+
{
82+
public static void AssertConfigurationIsValid(this IServiceCollection serviceCollection,
83+
List<Type> typesToIgnore = null)
84+
{
85+
ServiceProvider buildServiceProvider = serviceCollection.BuildServiceProvider();
86+
87+
List<ServiceDescriptor> list = serviceCollection.Where(x => x.ServiceType.Namespace != null && x.ServiceType.Namespace.Contains("Vme")).ToList();
88+
89+
if (typesToIgnore != null)
90+
{
91+
list.RemoveAll(listItem => typesToIgnore.Contains(listItem.ServiceType));
92+
}
93+
94+
foreach (ServiceDescriptor serviceDescriptor in list)
95+
{
96+
Type type = serviceDescriptor.ServiceType;
97+
98+
//This throws an Exception if the type cannot be instantiated.
99+
buildServiceProvider.GetService(type);
100+
}
101+
}
102+
}
89103
}

TransactionProcessor.Tests/General/ScopeExtensions.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

TransactionProcessor/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace TransactionProcessor
1111
{
1212
using System.Diagnostics.CodeAnalysis;
1313
using System.IO;
14-
using Autofac.Extensions.DependencyInjection;
1514

1615
[ExcludeFromCodeCoverage]
1716
public class Program
@@ -37,7 +36,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
3736
webBuilder.UseStartup<Startup>();
3837
webBuilder.UseConfiguration(config);
3938
webBuilder.UseKestrel();
40-
}).UseServiceProviderFactory(new AutofacServiceProviderFactory());
39+
});
4140
return hostBuilder;
4241
}
4342

TransactionProcessor/Startup.cs

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace TransactionProcessor
1616
using System.IO;
1717
using System.Net.Http;
1818
using System.Reflection;
19-
using Autofac;
2019
using BusinessLogic.OperatorInterfaces;
2120
using BusinessLogic.OperatorInterfaces.SafaricomPinless;
2221
using BusinessLogic.RequestHandlers;
@@ -71,36 +70,34 @@ public void ConfigureServices(IServiceCollection services)
7170
this.ConfigureMiddlewareServices(services);
7271

7372
services.AddTransient<IMediator, Mediator>();
74-
}
7573

76-
public void ConfigureContainer(ContainerBuilder builder)
77-
{
7874
ConfigurationReader.Initialise(Startup.Configuration);
7975
String connString = Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionString");
8076
String connectionName = Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionName");
8177
Int32 httpPort = Startup.Configuration.GetValue<Int32>("EventStoreSettings:HttpPort");
8278

8379
Boolean useConnectionStringConfig = Boolean.Parse(ConfigurationReader.GetValue("AppSettings", "UseConnectionStringConfig"));
8480
EventStoreConnectionSettings settings = EventStoreConnectionSettings.Create(connString, connectionName, httpPort);
85-
builder.RegisterInstance(settings);
86-
87-
Func<EventStoreConnectionSettings, IEventStoreConnection> eventStoreConnectionFunc = (connectionSettings) =>
88-
{
89-
return EventStoreConnection.Create(connectionSettings.ConnectionString);
90-
};
81+
services.AddSingleton(settings);
9182

92-
builder.RegisterInstance<Func<EventStoreConnectionSettings, IEventStoreConnection>>(eventStoreConnectionFunc);
83+
services.AddSingleton<Func<EventStoreConnectionSettings, IEventStoreConnection>>(cont => (connectionSettings) =>
84+
{
85+
return EventStoreConnection.Create(connectionSettings
86+
.ConnectionString);
87+
});
9388

94-
Func<String, IEventStoreContext> eventStoreContextFunc = (connectionString) =>
95-
{
96-
EventStoreConnectionSettings connectionSettings = EventStoreConnectionSettings.Create(connectionString, connectionName, httpPort);
89+
services.AddSingleton<Func<String, IEventStoreContext>>(cont => (connectionString) =>
90+
{
91+
EventStoreConnectionSettings connectionSettings =
92+
EventStoreConnectionSettings.Create(connectionString, connectionName, httpPort);
9793

98-
IEventStoreContext context = new EventStoreContext(connectionSettings, eventStoreConnectionFunc);
94+
Func<EventStoreConnectionSettings, IEventStoreConnection> eventStoreConnectionFunc = cont.GetService<Func<EventStoreConnectionSettings, IEventStoreConnection>>();
9995

100-
return context;
101-
};
96+
IEventStoreContext context =
97+
new EventStoreContext(connectionSettings, eventStoreConnectionFunc);
10298

103-
builder.RegisterInstance<Func<String, IEventStoreContext>>(eventStoreContextFunc);
99+
return context;
100+
});
104101

105102
SafaricomConfiguration safaricomConfiguration = new SafaricomConfiguration();
106103

@@ -114,57 +111,67 @@ public void ConfigureContainer(ContainerBuilder builder)
114111
}
115112
}
116113

117-
builder.RegisterInstance<SafaricomConfiguration>(safaricomConfiguration);
118-
114+
services.AddSingleton<SafaricomConfiguration>(safaricomConfiguration);
115+
119116
if (useConnectionStringConfig)
120117
{
121118
String connectionStringConfigurationConnString = ConfigurationReader.GetConnectionString("ConnectionStringConfiguration");
122-
builder.Register(c => new ConnectionStringConfigurationContext(connectionStringConfigurationConnString)).InstancePerDependency();
123-
builder.RegisterType<ConnectionStringConfigurationRepository>().As<IConnectionStringConfigurationRepository>().SingleInstance();
124-
builder.RegisterType<EventStoreContextManager>().As<IEventStoreContextManager>().UsingConstructor(typeof(Func<String, IEventStoreContext>), typeof(IConnectionStringConfigurationRepository)).SingleInstance();
119+
services.AddSingleton<IConnectionStringConfigurationRepository, ConnectionStringConfigurationRepository>();
120+
services.AddTransient<ConnectionStringConfigurationContext>(c =>
121+
{
122+
return new ConnectionStringConfigurationContext(connectionStringConfigurationConnString);
123+
});
124+
125+
services.AddSingleton<IEventStoreContextManager, EventStoreContextManager>(c =>
126+
{
127+
Func<String, IEventStoreContext> contextFunc = c.GetService<Func<String, IEventStoreContext>>();
128+
IConnectionStringConfigurationRepository connectionStringConfigurationRepository =
129+
c.GetService<IConnectionStringConfigurationRepository>();
130+
return new EventStoreContextManager(contextFunc,
131+
connectionStringConfigurationRepository);
132+
});
125133
}
126134
else
127135
{
128-
builder.RegisterType<EventStoreContextManager>().As<IEventStoreContextManager>().UsingConstructor(typeof(IEventStoreContext)).SingleInstance();
129-
// TODO: Once we have a Read Model
130-
//this.RegisterType<Vme.Repositories.IConnectionStringRepository, ConfigReaderConnectionStringRepository>().Singleton();
136+
services.AddSingleton<IEventStoreContextManager, EventStoreContextManager>(c =>
137+
{
138+
IEventStoreContext context = c.GetService<IEventStoreContext>();
139+
return new EventStoreContextManager(context);
140+
});
141+
//services.AddSingleton<IConnectionStringConfigurationRepository, ConfigurationReaderConnectionStringRepository>();
131142
}
132143

133-
builder.RegisterType<EventStoreContext>().As<IEventStoreContext>();
134-
builder.RegisterType<AggregateRepositoryManager>().As<IAggregateRepositoryManager>().SingleInstance();
135-
builder.RegisterType<AggregateRepository<TransactionAggregate.TransactionAggregate>>().As<IAggregateRepository<TransactionAggregate.TransactionAggregate>>().SingleInstance();
136-
builder.RegisterType<TransactionDomainService>().As<ITransactionDomainService>().SingleInstance();
137-
builder.RegisterType<Factories.ModelFactory>().As<Factories.IModelFactory>().SingleInstance();
138-
builder.RegisterType<EstateClient>().As<IEstateClient>().SingleInstance();
139-
builder.RegisterType<SecurityServiceClient>().As<ISecurityServiceClient>().SingleInstance();
140-
builder.RegisterType(typeof(HttpClient)).SingleInstance();
141-
builder.Register<Func<String, String>>(c => (api) =>
142-
{
143-
Uri uri = ConfigurationReader.GetBaseServerUri(api);
144-
return uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - 1);
145-
});
146-
builder.Register<Func<String, IOperatorProxy>>(c =>
147-
{
148-
IComponentContext cx = c.Resolve<IComponentContext>();
149-
return operatorrIdentifier =>
150-
{
151-
SafaricomConfiguration configuration = cx.Resolve<SafaricomConfiguration>();
152-
HttpClient client = cx.Resolve<HttpClient>();
153-
return new SafaricomPinlessProxy(configuration, client);
154-
};
155-
});
156-
144+
services.AddTransient<IEventStoreContext, EventStoreContext>();
145+
services.AddSingleton<IAggregateRepositoryManager, AggregateRepositoryManager>();
146+
services.AddSingleton<IAggregateRepository<TransactionAggregate.TransactionAggregate>, AggregateRepository<TransactionAggregate.TransactionAggregate>>();
147+
services.AddSingleton<ITransactionDomainService, TransactionDomainService>();
148+
services.AddSingleton<Factories.IModelFactory, Factories.ModelFactory>();
149+
services.AddSingleton<ISecurityServiceClient, SecurityServiceClient>();
150+
services.AddSingleton<Func<String, String>>(container => (serviceName) =>
151+
{
152+
return ConfigurationReader.GetBaseServerUri(serviceName).OriginalString;
153+
});
154+
services.AddSingleton<HttpClient>();
155+
services.AddSingleton<IEstateClient, EstateClient>();
157156
// request & notification handlers
158-
builder.Register<ServiceFactory>(context =>
159-
{
160-
IComponentContext c = context.Resolve<IComponentContext>();
161-
return t => c.Resolve(t);
162-
});
163-
164-
builder.RegisterType<TransactionRequestHandler>().As<IRequestHandler<ProcessLogonTransactionRequest, ProcessLogonTransactionResponse>>().SingleInstance();
165-
builder.RegisterType<TransactionRequestHandler>().As<IRequestHandler<ProcessSaleTransactionRequest, ProcessSaleTransactionResponse>>().SingleInstance();
157+
services.AddTransient<ServiceFactory>(context =>
158+
{
159+
return t => context.GetService(t);
160+
});
161+
162+
services.AddSingleton<IRequestHandler<ProcessLogonTransactionRequest, ProcessLogonTransactionResponse>, TransactionRequestHandler>();
163+
services.AddSingleton<IRequestHandler<ProcessSaleTransactionRequest, ProcessSaleTransactionResponse>, TransactionRequestHandler>();
164+
165+
services.AddTransient<Func<String, IOperatorProxy>>(context => (operatorIdentifier) =>
166+
{
167+
SafaricomConfiguration configuration = context.GetRequiredService<SafaricomConfiguration>();
168+
HttpClient client = context.GetRequiredService<HttpClient>();
169+
return new SafaricomPinlessProxy(configuration, client);
170+
});
166171
}
167172

173+
174+
168175
private void ConfigureMiddlewareServices(IServiceCollection services)
169176
{
170177
services.AddApiVersioning(

TransactionProcessor/TransactionProcessor.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
109
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
1110
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
1211
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />

0 commit comments

Comments
 (0)