Skip to content

Commit cf618eb

Browse files
committed
Tidy up integration point injection
1 parent c2a3b09 commit cf618eb

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

IntegrationEngine.ConsoleHost/IntegrationJobs/CarReport/CarMailMessageJob.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Net.Mail;
7+
using RazorEngine.Configuration;
78

89
namespace IntegrationEngine.ConsoleHost.Car
910
{
1011
public class CarMailMessageJob : IMailJob, IParameterizedJob
1112
{
1213
public IMailClient MailClient { get; set; }
14+
public IMailClient SecondMailClient { get; set; }
1315
public IDictionary<string, string> Parameters { get; set; }
1416

1517
public CarMailMessageJob()
@@ -24,12 +26,15 @@ public CarMailMessageJob(FooMailClient mailClient)
2426

2527
public void Run()
2628
{
27-
var mailMessage = new MailMessage();
28-
mailMessage.To.Add("ethanhann@gmail.com");
29-
mailMessage.Subject = "Your car report is ready.";
30-
mailMessage.From = new MailAddress("root@localhost");
31-
mailMessage.Body = "Body content about cars.";
32-
MailClient.Send(mailMessage);
29+
var areEqual = MailClient == SecondMailClient;
30+
Console.WriteLine(areEqual);
31+
SecondMailClient = MailClient;
32+
//var mailMessage = new MailMessage();
33+
//mailMessage.To.Add("ethanhann@gmail.com");
34+
//mailMessage.Subject = "Your car report is ready.";
35+
//mailMessage.From = new MailAddress("root@localhost");
36+
//mailMessage.Body = "Body content about cars.";
37+
//MailClient.Send(mailMessage);
3338
}
3439
}
3540
}

IntegrationEngine/EngineHostCompositionRoot.cs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using System.Collections.Generic;
1919
using System.Linq;
2020
using System.Reflection;
21+
using System.Security.Cryptography.X509Certificates;
22+
using Microsoft.Practices.ObjectBuilder2;
2123

2224
namespace IntegrationEngine
2325
{
@@ -88,15 +90,22 @@ public void RegisterIntegrationPoints()
8890
//Container.RegisterType<IMailConfiguration, MailConfiguration>();
8991
foreach (var config in EngineConfiguration.IntegrationPoints.Mail) {
9092
// Container.RegisterInstance<IMailConfiguration>(config.IntegrationPointName, config);
91-
Container.RegisterType<IMailConfiguration, MailConfiguration>(config.IntegrationPointName,
93+
Container.RegisterType<IMailConfiguration, MailConfiguration>(config.IntegrationPointName,
9294
new InjectionConstructor(
9395
new ResolvedParameter<IEngineConfiguration>(),
94-
new ResolvedParameter<string>(config.IntegrationPointName)
96+
config.IntegrationPointName
9597
)
9698
);
99+
100+
//Container.RegisterType<Func<string, IMailConfiguration>>(
101+
// new InjectionFactory(c => new Func<string, IMailConfiguration>(name => c.Resolve<IMailConfiguration>(name))));
102+
97103
Container.RegisterType<IMailClient, MailClient>(config.IntegrationPointName, new InjectionConstructor(config));
98104
}
99105

106+
107+
108+
100109
foreach (var config in EngineConfiguration.IntegrationPoints.Elasticsearch) {
101110
Container.RegisterInstance<IElasticsearchConfiguration>(config.IntegrationPointName, config);
102111
var serverUri = new UriBuilder(config.Protocol, config.HostName, config.Port).Uri;
@@ -119,48 +128,30 @@ public void RegisterIntegrationPoints()
119128

120129
public void RegisterIntegrationJobs()
121130
{
122-
foreach (var jobType in IntegrationJobTypes)
123-
{
124-
// Register the Types with the Container
125-
// Register a constructor with the Container
126-
// Need to know which constructor should be used.
127-
// If it has no constructor, assume default constructor
128-
// If it has one constructor, then use that constructor
129-
// If it has two or more constructors use the first one with arguments
130-
var constructorCount = jobType.GetConstructors().Count();
131-
if (constructorCount <= 1)
132-
{
133-
Container.RegisterType(jobType);
134-
continue;
135-
}
136-
//else if (constructorCount >= 2)
137-
//{
138-
var parameters = jobType.GetConstructors().Single(x => x.GetParameters().Any()).GetParameters();
139-
// Resolve the integration point type (in parameters).
131+
IntegrationJobTypes.ForEach(jobType => {
132+
// Resolve the integration point type (specified in the job's parameters).
140133
// Configure the integration point type with a configuration, based on the parameter name.
141-
var resolvedParameters = new List<ResolvedParameter>();
142-
foreach (var parameterInfo in parameters)
143-
{
144-
var parameterType = parameterInfo.ParameterType; // The type of integration point (e.g. IElasticClient)
145-
var parameterName = parameterInfo.ParameterType.Name; // The name of the configuration endpoint (e.g. "MyElasticClient")
146-
147-
if (typeof(IMailClient).IsAssignableFrom(parameterType))
134+
Func<ParameterInfo[], object[]> resolveParameters = infos => {
135+
var resolvedParameters = new List<object>();
136+
foreach (var parameterInfo in infos)
148137
{
149-
// Register the integration point.
150-
// To register the integration point, the parameters of the type's constructor must be known.
151-
// To make this happen, an integration point must have a constructor that takes a configuration object parameter.
152-
//var configName = parameterType.GetCustomAttribute<IntegrationPointConfigurationAttribute>().Name;
153-
//var config = Container.Resolve<IMailConfiguration>(parameterName);
154-
//Container.RegisterType(parameterType, new InjectionConstructor(config));
155-
156-
//Container.RegisterType<IMailClient, MailClient>(config.IntegrationPointName, new InjectionConstructor(config));
157-
// Container.RegisterType(parameterType, typeof(MailClient));
158-
resolvedParameters.Add(new ResolvedParameter(parameterType, parameterName));
138+
var parameterType = parameterInfo.ParameterType; // The type of integration point (e.g. IElasticClient)
139+
var parameterName = parameterInfo.ParameterType.Name; // The name of the configuration endpoint (e.g. "MyElasticClient")
140+
if (typeof(IMailClient).IsAssignableFrom(parameterType))
141+
resolvedParameters.Add(Activator.CreateInstance(parameterType, Container.Resolve<IMailConfiguration>(parameterName)));
159142
}
143+
return resolvedParameters.Cast<object>().ToArray();
144+
};
145+
var constructors = jobType.GetConstructors();
146+
if (constructors.Count() == 1 && !constructors.Single().GetParameters().Any()) // Handle Default Constructor case.
147+
Container.RegisterType(jobType, new InjectionFactory((c, t, s) => Activator.CreateInstance(jobType)));
148+
else
149+
{
150+
// Use the first constructor with parameters.
151+
var constructor = constructors.First(x => x.GetParameters().Any());
152+
Container.RegisterType(jobType, new InjectionFactory((c, t, s) => Activator.CreateInstance(jobType, resolveParameters(constructor.GetParameters()))));
160153
}
161-
var objectArray = resolvedParameters.Cast<object>().ToArray();
162-
Container.RegisterType(jobType, new InjectionConstructor(objectArray));
163-
}
154+
});
164155
}
165156

166157
public void SetupThreadedListenerManager()

0 commit comments

Comments
 (0)