Hi,

I'm currently working on an alternative implementation of the 
TypedFactoryComponentResolver.
My problem is, that I'd like to use typed factories, but I need to return 
null, in case the component isn't registered.
The concrete usecase is, that I'm looking for a validator for a specific 
type, and if no validator is registered, it should just return null from 
the factory.
As the default implementation always throws an exception, if no component 
is registered, I've created my own version.

/// <summary>
/// Resolves a component. If the component isn't registered, the resolver 
returns null instead of an exception.
/// </summary>
internal class NullSafeTypedFactoryComponentResolver : 
TypedFactoryComponentResolver
{
public NullSafeTypedFactoryComponentResolver(string componentName, Type 
componentType, IDictionary additionalArguments, bool 
fallbackToResolveByTypeIfNameNotFound, Type actualSelectorType)
: base(componentName, componentType, additionalArguments, 
fallbackToResolveByTypeIfNameNotFound, actualSelectorType) { }

public override object Resolve(IKernelInternal kernel, IReleasePolicy scope)
{
// Check to see if the component is registered
var hasComponent = componentName != null
? kernel.HasComponent(componentName)
: kernel.HasComponent(componentType);

return hasComponent ? base.Resolve(kernel, scope) : null;
}
}

I'm then overriding the BuildFactoryComponent method from the 
DefaultTypedFactoryComponentSelector, to return my own implementation.

Since I'm doing something non-standard, I'd just wanted to make sure, I'm 
not misusing (or misunderstanding) the concept of a typed factory. 
Basically I'm changing the semantics of the container, so I'm just a bit 
uncertain about which implications it may bring.


Thanks,
Kenneth

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/castle-project-users/-/GW3SaUL8FTEJ.
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