Ejemplo n.º 1
0
 /// <summary>
 /// Convert to the Autofac parameter type.
 /// </summary>
 /// <returns>The parameters represented by this collection.</returns>
 public static IEnumerable <Parameter> ToParameters(this List <ParameterElement> parameterElements)
 {
     foreach (var parameter in parameterElements)
     {
         var localParameter = parameter;
         yield return(new ResolvedParameter(
                          (pi, c) => pi.Name == localParameter.Name,
                          (pi, c) => TypeManipulation.ChangeToCompatibleType(localParameter.Value, pi.ParameterType)));
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Convert to the Autofac parameter type.
 /// </summary>
 /// <returns>The parameters represented by this collection.</returns>
 public static IEnumerable <Parameter> ToParameters(this List <PropertyElement> propertyElements)
 {
     foreach (var parameter in propertyElements)
     {
         var localParameter = parameter;
         yield return(new ResolvedParameter(
                          (pi, c) =>
         {
             PropertyInfo prop;
             return pi.TryGetDeclaringProperty(out prop) &&
             prop.Name == localParameter.Name;
         },
                          (pi, c) => TypeManipulation.ChangeToCompatibleType(localParameter.Value, pi.ParameterType)));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Override to add registrations to the container.
        /// </summary>
        /// <param name="builder">The builder.</param>
        protected override void Load(ContainerBuilder builder)
        {
            Enforce.ArgumentNotNull(builder, "builder");

            Assembly defaultAssembly = null;

            if (!string.IsNullOrEmpty(_serviceConfig.DefaultAssembly))
            {
                defaultAssembly = Assembly.Load(_serviceConfig.DefaultAssembly);
            }

            foreach (ModuleElement moduleElement in _serviceConfig.Modules)
            {
                var moduleType      = LoadType(moduleElement.Type, defaultAssembly);
                var moduleActivator = new ReflectionActivator(
                    moduleType,
                    new  BindingFlagsConstructorFinder(BindingFlags.Public),
                    new MostParametersConstructorSelector(),
                    moduleElement.Parameters.ToParameters(),
                    moduleElement.Properties.ToParameters());
                var module = (IModule)moduleActivator.ActivateInstance(Container.Empty, Enumerable.Empty <Parameter>());
                builder.RegisterModule(module);
            }

            foreach (ComponentElement component in _serviceConfig.Components)
            {
                var registrar = builder.RegisterType(LoadType(component.Type, defaultAssembly));

                IList <Service> services = new List <Service>();
                if (!string.IsNullOrEmpty(component.Service))
                {
                    var serviceType = LoadType(component.Service, defaultAssembly);
                    if (!string.IsNullOrEmpty(component.Name))
                    {
                        services.Add(new KeyedService(component.Name, serviceType));
                    }
                    else
                    {
                        services.Add(new TypedService(serviceType));
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(component.Name))
                    {
                        throw new ConfigurationErrorsException(string.Format(
                                                                   "Service Type Must Be Specified:{0}", component.Name));
                    }
                }

                foreach (ServiceElement service in component.Services)
                {
                    var serviceType = LoadType(service.Type, defaultAssembly);
                    if (!string.IsNullOrEmpty(service.Name))
                    {
                        services.Add(new KeyedService(service.Name, serviceType));
                    }
                    else
                    {
                        services.Add(new TypedService(serviceType));
                    }
                }

                foreach (var service in services)
                {
                    registrar.As(service);
                }

                foreach (var param in component.Parameters.ToParameters())
                {
                    registrar.WithParameter(param);
                }

                foreach (var prop in component.Properties.ToParameters())
                {
                    registrar.WithProperty(prop);
                }

                foreach (var ep in component.Metadata)
                {
                    registrar.WithMetadata(
                        ep.Name, TypeManipulation.ChangeToCompatibleType(ep.Value, Type.GetType(ep.Type)));
                }

                if (!string.IsNullOrEmpty(component.MemberOf))
                {
                    registrar.MemberOf(component.MemberOf);
                }

                SetScope(component, registrar);
                SetOwnership(component, registrar);
                SetInjectProperties(component, registrar);
            }

            //foreach (FileElement file in _serviceConfig.Files)
            //{
            //    var section = DefaultSectionName;
            //    if (!string.IsNullOrEmpty(file.Section))
            //        section = file.Section;

            //    var reader = new ConfigurationSettingsReader(section, file.Name);
            //    builder.RegisterModule(reader);
            //}
        }