1818using System . Collections . Generic ;
1919using System . Linq ;
2020using System . Reflection ;
21+ using System . Security . Cryptography . X509Certificates ;
22+ using Microsoft . Practices . ObjectBuilder2 ;
2123
2224namespace 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