Example #1
0
        protected IHandler GetSubHandler(CreationContext context, Type genericType)
        {
            IHandler handler;

            if (type2SubHandler.TryGetValue(genericType, out handler))
            {
                return(handler);
            }
            lock (type2SubHandler)
            {
                if (type2SubHandler.TryGetValue(genericType, out handler))
                {
                    return(handler);
                }
                // TODO: we should probably match the requested type to existing services and close them over its generic arguments
                var service  = context.RequestedType;
                var newModel = Kernel.ComponentModelFactory.BuildModel(
                    ComponentModel.ComponentName, new[] { service }, genericType, ComponentModel.ExtendedProperties);

                newModel.ExtendedProperties[ComponentModel.SkipRegistration] = true;
                CloneParentProperties(newModel);

                // Create the handler and add to type2SubHandler before we add to the kernel.
                // Adding to the kernel could satisfy other dependencies and cause this method
                // to be called again which would result in extra instances being created.
                handler = Kernel.HandlerFactory.Create(newModel);
                type2SubHandler[genericType] = handler;

                Kernel.AddCustomComponent(newModel);

                return(handler);
            }
        }
        private void ConfigureFactories(IConfiguration config)
        {
            String id = config.Attributes["id"];

            if (id == null || String.Empty.Equals(id))
            {
                throw new System.Configuration.ConfigurationException("You must provide a valid 'id' attribute for the 'factory' node");
            }

            Configuration cfg = new Configuration();

            ApplyConfigurationSettings(cfg, config.Children["settings"]);
            RegisterAssemblies(cfg, config.Children["assemblies"]);
            RegisterResources(cfg, config.Children["resources"]);

            // Registers the Configuration object

            Kernel.AddComponentInstance(String.Format("{0}.cfg", id), cfg);

            // Registers the ISessionFactory with a custom activator

            ComponentModel model = new ComponentModel(id, typeof(ISessionFactory), null);

            model.ExtendedProperties.Add(ConfiguredObject, cfg);
            model.LifestyleType            = LifestyleType.Singleton;
            model.CustomComponentActivator = typeof(SessionFactoryActivator);
            Kernel.AddCustomComponent(model);
        }
        public void AddTypedFactoryEntry(FactoryEntry entry)
        {
            ComponentModel model = new ComponentModel(entry.Id, entry.FactoryInterface, typeof(Empty));

            model.LifestyleType = LifestyleType.Singleton;
            model.ExtendedProperties["typed.fac.entry"] = entry;
            model.Interceptors.Add(new InterceptorReference(typeof(FactoryInterceptor)));

            Kernel.AddCustomComponent(model);
        }
Example #4
0
        protected virtual IHandler BuildSubHandler(CreationContext context, Type requestedType)
        {
            // TODO: we should probably match the requested type to existing services and close them over its generic arguments
            var newModel = Kernel.ComponentModelBuilder.BuildModel(
                ComponentModel.ComponentName,
                new[] { context.RequestedType },
                requestedType,
                GetExtendedProperties());

            CloneParentProperties(newModel);
            // Create the handler and add to type2SubHandler before we add to the kernel.
            // Adding to the kernel could satisfy other dependencies and cause this method
            // to be called again which would result in extra instances being created.
            return(Kernel.AddCustomComponent(newModel, isMetaHandler: true));
        }
Example #5
0
        private void RegisterAspectEngine()
        {
            String contents = FacilityConfig.Value;

            // By now we're supporting only AspectLanguageEngineBuilder

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);

            ComponentModel model =
                new ComponentModel("aspectsharp.engine",
                                   typeof(AspectEngine), typeof(AspectEngine));

            model.ExtendedProperties.Add("builder", builder);
            model.CustomComponentActivator = typeof(AspectEngineActivator);

            Kernel.AddCustomComponent(model);
        }
        /// <summary>
        /// Adds the specified service to the service container, and optionally
        /// promotes the service to parent service containers.
        /// </summary>
        /// <param name="serviceType">The type of service to add.</param>
        /// <param name="callback">A callback object that is used to create the service.</param>
        /// <param name="promote">true to promote this request to any parent service containers.</param>
        public virtual void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
        {
            if (promote)
            {
                IServiceContainer parentServices = ParentServices;

                if (parentServices != null)
                {
                    parentServices.AddService(serviceType, callback, promote);
                    return;
                }
            }

            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            if (HasService(serviceType))
            {
                throw new ArgumentException(String.Format(
                                                "A service for type '{0}' already exists", serviceType.FullName),
                                            "serviceType");
            }

            String         serviceName = GetServiceName(serviceType);
            ComponentModel model       = new ComponentModel(serviceName, serviceType, null);

            model.ExtendedProperties.Add(ServiceCreatorCallbackActivator.ServiceContainerKey,
                                         GetService(typeof(IServiceContainer)));
            model.ExtendedProperties.Add(ServiceCreatorCallbackActivator.ServiceCreatorCallbackKey, callback);
            model.LifestyleType            = LifestyleType.Singleton;
            model.CustomComponentActivator = typeof(ServiceCreatorCallbackActivator);
            Kernel.AddCustomComponent(model);
        }
Example #7
0
        private void ConfigureFactory(IConfiguration config)
        {
            // A name for this sqlMap
            String id = config.Attributes["id"];

            String fileName = config.Attributes["config"];

            if (fileName == String.Empty)
            {
                fileName = "sqlMap.config";                 // default name
            }

            bool   isEmbedded = false;
            String embedded   = config.Attributes["embedded"];

            if (embedded != null)
            {
                try
                {
                    isEmbedded = Convert.ToBoolean(embedded);
                }
                catch
                {
                    isEmbedded = false;
                }
            }

            ComponentModel model = new ComponentModel(id, typeof(SqlMapper), null);

            model.ExtendedProperties.Add(FILE_CONFIGURATION, fileName);
            model.ExtendedProperties.Add(FILE_CONFIGURATION_EMBEDDED, isEmbedded);
            model.LifestyleType            = LifestyleType.Singleton;
            model.CustomComponentActivator = typeof(SqlMapActivator);

            Kernel.AddCustomComponent(model);
        }