[Twisted-Python] adapt from more than one interface

glyph at divmod.com glyph at divmod.com
Sat Apr 11 20:31:29 EDT 2009


On 11 Apr, 08:41 pm, esteve at sindominio.net wrote:
>factory = IFooFactory(myService)
>factory.bar = "some value"
>
>it works, but I'd rather use this:
>
>factory = IFooFactory(myService, configurationObject)

This is a long and terrible road you're about to start down.  It's hard 
for me to explain exactly why it's so painful, but trust me, it is.

What you're doing now, passing out-of-band arguments to the IFooFactory 
adapter, whose concrete type you are *not supposed to know* at that 
point in the control flow, is bad.  But trying to force adaptation to 
become full-fledged multimethods is worse.  If you want multimethods, I 
believe PEAK provides a package that implements them.

If you want to keep doing what you're doing, you should do this:

factory = MyServiceFooFactory(myService)
factory.configureBar("some value")

i.e. don't ask for an adaptation which might give you an object without 
a "bar" attribute, just create the concrete type that does what you need 
based on the type of myService and configure it with explicitly 
documented methods.  Although if I misunderstand the spec of 
"IFooFactory" and it *does* include a bar attribute, nevermind.

If you want something more magical and adapter-ish, them maybe you want 
this:

IFooFactory(ConfigurationObject(myService, bar="some value"))

i.e. by the time you are getting around to the IFooFactory adaptation, 
you should have an object that fully provides enough information that 
the concrete FooFactory adapter type can be fully initialized when it is 
created.
>In order to implement this, registerAdapter would have to be able to 
>take a
>tuple of interfaces, like this:
>
>components.registerAdapter(
>    AdaptToFactory,
>    (IFooService, IConfigurationDescriptor),
>    IFooFactory)
>
>would that feature make sense? If so, I'll file an issue right now :-)

Unfortunately, much as I've been encouraging people to file tickets 
lately, no :).  I don't think it makes sense.




More information about the Twisted-Python mailing list