[Grok-dev] registering a factory as a (global) utility
Darryl Cousins
darryl at darrylcousins.net.nz
Fri Apr 27 21:50:17 EDT 2007
Hi,
On Wed, 2007-04-25 at 22:03 +0200, Martijn Faassen wrote:
> Hi there,
>
> A fairly common pattern is the need to register a factory as a utility.
> I thought that'd be just:
>
> class Item:
> pass
>
> grok.global_utility(Item, provides=IMyFactory)
>
> Unfortunately that's not the case. This registers an *instance* of Item,
> as global utilities actually instantiate Item.
>
> Now we could simply say that's wrong, and people should instantiate
> these themselves.
>
> That would work, and that's fine when considered in isolation, but
> unfortunately that is assymetrical with local utility registration,
> where we can't require this. Local utilities should only instantiate
> from their factories when the object that has these utilities associated
> is actually installed.
>
> It's still useful to have this for global utilities, though. I propose
> the following alternate directive to do this:
>
> grok.global_utility_object(Item, provides=IMyFactory)
>
> I chose a separate directive because making global_utility take either a
> factory or an instance requires all kinds of extra checks and is not as
> readable as having a separate directive. I'm not sure I'm happy about
> the name though.
>
> Comments?
>
> Regards,
>
> Martijn
I'm struggling a bit to get my head around this after a re-read. In
nudgenudge a factory is used in one case (local_utility and not
global_utility):
def role_factory(*args):
def factory():
return LocalRole(*args)
return factory
where if I understand correctly it is being instantiated by the author.
I did try following this for an understanding (I was mostly interested
in when it is called and with what arguments) but I don't have a report.
Hope this is relavent.
Regards,
Darryl
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
More information about the Grok-dev
mailing list