Esempio n. 1
0
        private void ComponentContainer_NoComponentRegisted(object sender, NoComponentRegistedEventArgs e)
        {
            if (!e.ServiceType.IsInterface)
            {
                return;
            }
            IEnumerable <AttachedRuntimeInfo> matchedInfos = this.attachedImplementorInfo
                                                             .Where(info => info.Attachment.CanAttach(e.ServiceType));
            int count = matchedInfos.Count();

            if (count == 0)
            {
                return;
            }
            if (count > 1)
            {
                throw new ApplicationException();
            }

            var implementorType = matchedInfos.First().AttachedType;

            e.ComponentProvider = cm =>
            {
                var implementor = (IImplementor)cm.CreateComponent(implementorType);
                ImplementorAttributeExecuteInterceptor interceptor = new ImplementorAttributeExecuteInterceptor(e.ServiceType, implementor);
                var rlt = proxyGenerator.CreateInterfaceProxyWithoutTarget(
                    e.ServiceType,
                    interceptor
                    );
                return(rlt);
            };
        }
Esempio n. 2
0
        public IEnumerable <IComponentRegistration> RegistrationsFor(Service service, Func <Service, IEnumerable <IComponentRegistration> > registrationAccessor)
        {
            if (service.Description.StartsWith("Decorator ("))
            {
                return(Enumerable.Empty <IComponentRegistration>());
            }
            var swt = service as IServiceWithType;

            if (swt == null)
            {
                return(Enumerable.Empty <IComponentRegistration>());
            }

            var registrations = registrationAccessor(service);

            Debug.WriteLine("RegistrationsFor : {0}", swt.ServiceType);
            if (registrations.Any())
            {
                Debug.WriteLine("\tRegisted : {0}", swt.ServiceType);
                foreach (var registration in registrationAccessor(service))
                {
                    var trigger = new ComponentRegistrationActivateEventTrigger(registration, swt.ServiceType);

                    trigger.ComponentCreated  += (sender, e) => this.ComponentCreated?.Invoke(this, e);
                    trigger.ComponentCreating += (sender, e) => this.ComponentCreating?.Invoke(this, e);
                }
                return(Enumerable.Empty <IComponentRegistration>());
            }

            Debug.WriteLine("\tNo Registed : {0}", swt.ServiceType);
            NoComponentRegistedEventArgs noComponentRegistedEventArgs = new NoComponentRegistedEventArgs(swt.ServiceType);

            this.NoComponentRegisted?.Invoke(this, noComponentRegistedEventArgs);
            if (noComponentRegistedEventArgs.ComponentProvider == null)
            {
                return(Enumerable.Empty <IComponentRegistration>());
            }

            var newRegistration = new ComponentRegistration(
                Guid.NewGuid(),
                new DelegateActivator(swt.ServiceType, (c, p) =>
            {
                return(noComponentRegistedEventArgs.ComponentProvider(new ComponentContextComponentManager(c)));
            }),
                new CurrentScopeLifetime(),
                InstanceSharing.Shared,
                InstanceOwnership.OwnedByLifetimeScope,
                new[] { service },
                new Dictionary <string, object>()
                );

            return(new IComponentRegistration[] { newRegistration });
        }