views:

53

answers:

1

How can I use an IComponentActivator instance for a component, not just specifying a type.

That is, instead of

Component.For<XYZ>.Activator<MyComponentActivator>();

I want to be able say

Component.For<XYZ>.Activator(new MyComponentActivator(someImportantRuntimeInfo));

Also, is there a way I can choose an activator dynamically for a non specifically registered component? That is, I want to have an activator that looks at the type being resolved, decides if it can activate it, and if not, responsibility for activation should be passed on to the default activator.

So basically I want a global IComponentActivator that has the following logic:

Create(Type type){
  if (ShouldActivate(type)){
    DoActivate(type);
  }
  else{
    // do default activation here somehow
  } 
}

Thanks!

A: 

EDIT Just use ILazyComponentLoader


  1. Activators are instantiated by the container, and there is no way to provide instances OOTB.

If you really want to do this, I'd say you can extend the container itself.

  • Put your custom instace activators in ExtendedProperties of your component under a well-known key
  • inherit from DefaultKernel
  • Override CreateComponentActivator method
  • In there return the activator from ExtendedProperties of the component

    1. There's no way to create global activator. You can add IContributeComponentModelCreation implementation that switches the activator to some custom one wen creating your component if you really want to.

But the most important question is this:

What are you trying to achieve by this? Why do you want to do this in the first place?

Krzysztof Koźmic
Specifically, I'm creating a custom WCF channel activator. I don't want to have to register all the WCF services as the WCF facility requires...rather I want my custom activator to: 1. Check if the type (interface) try to be resolved has a ServiceContract Attribute. 2. Check if the app.config or ServiceReferences.ClientConfig has an client endpoint listed in the servicemodel section with a matching Contract name (attribute). 3. If found, instantiate using a ChannelFactory<T>. 4. If not found, instantiate regularly. Thanks!
JeffN825
Use `ILazyComponentLoader` for that if you must
Krzysztof Koźmic
Brilliant! That works perfectly, thanks.
JeffN825