I'm spiking a sample of an event broker where listeners are
(un)registered to the broker during resolution/destruction. the code
below appears to be working, but I wanted to double check with the
community to make sure I'm not missing something obvious (or not so
obvious).

public class EventBrokerFacility
        : AbstractFacility
{
        protected override void Init()
        {
                Kernel.ComponentModelBuilder.AddContributor(new
EventBrokerContributor());
                Kernel.Register(Component
                                                        
.For<SynchronizationContext>()
                                                        
.ImplementedBy<WindowsFormsSynchronizationContext>()
                                                        .LifeStyle.Singleton,
                                                Component
                                                        .For<IEventPublisher, 
IEventRegister>()
                                                        
.ImplementedBy<EventBroker>()
                                                        .LifeStyle.Singleton);
        }
}
public class EventBrokerContributor
        : IContributeComponentModelConstruction
{
        private readonly Type activator = typeof
(EventBrokerComponentActivator);
        private readonly Type listener = typeof (IListener<>);

        public void ProcessModel(IKernel kernel, ComponentModel model)
        {
                var isAListener = model
                        .Implementation
                        .GetInterfaces()
                        .Where(i => i.IsGenericType)
                        .Select(i => i.GetGenericTypeDefinition())
                        .Any(i => i == listener);

                if (isAListener == false) return;
                model.CustomComponentActivator = activator;
        }
}
public class EventBrokerComponentActivator
        : DefaultComponentActivator
{
        private IEventRegister Broker
        {
                get { return Kernel.Resolve<IEventRegister>(); }
        }

        public EventBrokerComponentActivator(
                ComponentModel model,
                IKernel kernel,
                ComponentInstanceDelegate onCreation,
                ComponentInstanceDelegate onDestruction)
                : base(model, kernel, onCreation, onDestruction)
        {
        }

        public override object Create(CreationContext context)
        {
                var component = base.Create(context);
                Broker.Register(component);
                return component;
        }

        public override void Destroy(object instance)
        {
                Broker.Unregister(instance);
                base.Destroy(instance);
        }
}

as an example this class will use the EventBrokerComponentActivator
class Presenter : IListener<Foo>,IListener<Bar>
{
}

while this class uses the default.
class Service
{
}

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to