[CONF] Apache Tapestry IoC Cookbook - Patterns

2013-11-25 Thread Bob Harner (Confluence)







IoC Cookbook - Patterns
Page edited by Bob Harner


Comment:
Fixed the , ,  and  occurrances as reported by basileChandesris


 Changes (16)
 




...
Main Article: [Chain of Command|ChainBuilder Service]  
Letapos;s Lets look at another example, again from the Tapestry code base. The [InjectProvider|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/InjectionProvider.html] interface is used to process the @Inject annotation on the fields of a Tapestry page or component. Many different instances are combined together to form a _chain of command_. 
 The interface has only a single method (this is far from uncommon): 
...
}{code}  
The return type indicates whether the provider was able to do something. For example, the AssetInjectionProvider checks to see if thereapos;s theres an @Path annotation on the field, and if so, converts the path to an asset, works with the ClassTransformation object to implement injection, and returns true to indicate success. Returns true terminates the chain early, and that true value is ultimately returned to the caller. 
 In other cases, it returns false and the chain of command continues down to the next provider. If no provider is capable of handling the injection, then the value false is ultimately returned. 
...
{code} public static void contributeInjectionProvider( 
OrderedConfigurationlt;InjectionProvidergt; OrderedConfigurationInjectionProvider configuration, 
MasterObjectProvider masterObjectProvider, ObjectLocator locator, 
...
AssetSource assetSource) { 
configuration.add(quot;Defaultquot;, configuration.add(Default, new DefaultInjectionProvider(masterObjectProvider, locator)); 
 
configuration.add(quot;ComponentResourcesquot;, configuration.add(ComponentResources, new ComponentResourcesInjectionProvider()); 
 
  configuration.add(   quot;CommonResourcesquot;, 
configuration.add(CommonResources, new CommonResourcesInjectionProvider(), after:Default); 
  quot;after:Defaultquot;); 
 
  configuration.add(   quot;Assetquot;, 
configuration.add(Asset, new AssetInjectionProvider(symbolSource, assetSource), before:Default); 
  quot;before:Defaultquot;); 
 
  configuration.add(quot;Blockquot;, new BlockInjectionProvider(), quot;before:Defaultquot;);   configuration.add(quot;Servicequot;, new ServiceInjectionProvider(locator), quot;after:*quot;); 
  configuration.add(Block, new BlockInjectionProvider(), before:Default);   configuration.add(Service, new ServiceInjectionProvider(locator), after:*); 
}{code}  
...
 {code} 
public InjectionProvider build(Listlt;InjectionProvidergt; build(ListInjectionProvider configuration, ChainBuilder chainBuilder) 
  { return chainBuilder.build(InjectionProvider.class, configuration);   }{code}  
Now, letapos;s lets see how this is used. The InjectWorker class looks for fields with the InjectAnnotation, and uses the chain of command to inject the appropriate value. However, to InjectWorker, there is no chain ... just a _single_ object that implements the InjectionProvider interface. 
 {code} 
...
}{code}  
Reducing the chain to a single object vastly simplifies the code: weapos;ve weve _factored out_ the loop implicit in the chain of command. That eliminates a lot of code, and thatapos;s thats less code to test, and fewer paths through InjectWorker, which lowers its complexity further. We donapos;t dont have to test the cases where the list of injection providers is empty, or consists of only a single object, or where itapos;s its the third object in that returns true: it looks like a single object, it acts like a single object ... but its implementation uses many objects. 
 {scrollbar} 


Full Content

IoC Cookbook - Overriding IoC ServicesIoC cookbookIoC cookbook - Service Configurations
Using Patterns 


Related Articles


 Page:
 ChainBuilder Service





 Page:
 PipelineBuilder Service





 Page:
 StrategyBuilder 

[CONF] Apache Tapestry IoC Cookbook - Patterns

2011-01-22 Thread confluence







IoC Cookbook - Patterns
Page edited by Bob Harner


Comment:
Renamed so it's name makes sense as a stand-alone page, e.g. when appearing in "Related Articles" boxes


 Changes (0)
 




...


Full Content

IoC Cookbook - Overriding IoC ServicesIoC cookbookIoC cookbook - servconf
Using Patterns


Related Articles


 Page:
 ChainBuilder Service





 Page:
 StrategyBuilder Service





 Page:
 PipelineBuilder Service





 Page:
 IoC Cookbook - Patterns




 

Tapestry IoC has support for implementing several of the Gang Of Four Design Patterns. In fact, the IoC container itself is a pumped up version of the Factory pattern.

The basis for these patterns is often the use of service builder methods, where a configuration for the service is combined with a factory to produce the service implementation on the fly.


Chain of Command Pattern

Main Article: Chain of Command

Lets look at another example, again from the Tapestry code base. The InjectProvider interface is used to process the @Inject annotation on the fields of a Tapestry page or component. Many different instances are combined together to form a chain of command.

The interface has only a single method (this is far from uncommon):



public interface InjectionProvider
{
  boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
  ClassTransformation transformation, MutableComponentModel componentModel);
}


The return type indicates whether the provider was able to do something. For example, the AssetInjectionProvider checks to see if theres an @Path annotation on the field, and if so, converts the path to an asset, works with the ClassTransformation object to implement injection, and returns true to indicate success. Returns true terminates the chain early, and that true value is ultimately returned to the caller.

In other cases, it returns false and the chain of command continues down to the next provider. If no provider is capable of handling the injection, then the value false is ultimately returned.

The InjectionProvider service is built up via contributions. These are the contributions from the TapestryModule:



public static void contributeInjectionProvider(
OrderedConfigurationlt;InjectionProvidergt; configuration,
MasterObjectProvider masterObjectProvider,
ObjectLocator locator,
SymbolSource symbolSource,
AssetSource assetSource)
{
  configuration.add(quot;Defaultquot;, new DefaultInjectionProvider(masterObjectProvider, locator));

  configuration.add(quot;ComponentResourcesquot;, new ComponentResourcesInjectionProvider());

  configuration.add(
  quot;CommonResourcesquot;,
  new CommonResourcesInjectionProvider(),
  quot;after:Defaultquot;);

  configuration.add(
  quot;Assetquot;,
  new AssetInjectionProvider(symbolSource, assetSource),
  quot;before:Defaultquot;);

  configuration.add(quot;Blockquot;, new BlockInjectionProvider(), quot;before:Defaultquot;);
  configuration.add(quot;Servicequot;, new ServiceInjectionProvider(locator), quot;after:*quot;);
}


And, of course, other contributions could be made in other modules ... if you wanted to add in your own form of injection.

The configuration is converted into a service via a service builder method:



  public InjectionProvider build(Listlt;InjectionProvidergt; configuration, ChainBuilder chainBuilder)
  {
return chainBuilder.build(InjectionProvider.class, configuration);
  }


Now, lets see how this is used. The InjectWorker class looks for fields with the InjectAnnotation, and uses the chain of command to inject the appropriate value. However, to InjectWorker, there is no chain ... just a single object that implements the InjectionProvider interface.



public class InjectWorker implements ComponentClassTransformWorker
{
  private final ObjectLocator locator;

  // Really, a chain of command

  private final InjectionProvider injectionProvider;

  public InjectWorker(ObjectLocator locator, InjectionProvider injectionProvider)
  {
this.locator = locator;
this.injectionProvider = injectionProvider;
  }

  public final void transform(ClassTransformation 

[CONF] Apache Tapestry IoC cookbook - patterns

2010-12-07 Thread confluence







IoC cookbook - patterns
Page edited by Bob Harner


Comment:
Added scrollbar at top  bottom, fixed broken link


 Changes (3)
 



{scrollbar} 
h1. Using Patterns  Tapestry IoC has support for implementing several of the [Gang Of Four Design Patterns|http://en.wikipedia.org/wiki/Design_pattern_(computer_science)]. In fact, the IoC container itself is a pumped up version of the Factory pattern.  
The basis for these patterns is often the use of _service builder methods_, where a [configuration|#servconf.html] [configuration|IoC cookbook - servconf] for the service is combined with a factory to produce the service implementation on the fly. 
 {anchor:chainofcommand} 
...
 Reducing the chain to a single object vastly simplifies the code: weapos;ve _factored out_ the loop implicit in the chain of command. That eliminates a lot of code, and thatapos;s less code to test, and fewer paths through InjectWorker, which lowers its complexity further. We donapos;t have to test the cases where the list of injection providers is empty, or consists of only a single object, or where itapos;s the third object in that returns true: it looks like a single object, it acts like a single object ... but its implementation uses many objects. 
 {scrollbar} 

Full Content

IoC cookbook - overrideIoC cookbookIoC cookbook - servconf
Using Patterns

Tapestry IoC has support for implementing several of the Gang Of Four Design Patterns. In fact, the IoC container itself is a pumped up version of the Factory pattern.

The basis for these patterns is often the use of service builder methods, where a configuration for the service is combined with a factory to produce the service implementation on the fly.


Chain of Command Pattern

Main Article: Chain of Command

Lets look at another example, again from the Tapestry code base. The InjectProvider interface is used to process the @Inject annotation on the fields of a Tapestry page or component. Many different instances are combined together to form a chain of command.

The interface has only a single method (this is far from uncommon):



public interface InjectionProvider
{
  boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
  ClassTransformation transformation, MutableComponentModel componentModel);
}


The return type indicates whether the provider was able to do something. For example, the AssetInjectionProvider checks to see if theres an @Path annotation on the field, and if so, converts the path to an asset, works with the ClassTransformation object to implement injection, and returns true to indicate success. Returns true terminates the chain early, and that true value is ultimately returned to the caller.

In other cases, it returns false and the chain of command continues down to the next provider. If no provider is capable of handling the injection, then the value false is ultimately returned.

The InjectionProvider service is built up via contributions. These are the contributions from the TapestryModule:



public static void contributeInjectionProvider(
OrderedConfigurationlt;InjectionProvidergt; configuration,
MasterObjectProvider masterObjectProvider,
ObjectLocator locator,
SymbolSource symbolSource,
AssetSource assetSource)
{
  configuration.add(quot;Defaultquot;, new DefaultInjectionProvider(masterObjectProvider, locator));

  configuration.add(quot;ComponentResourcesquot;, new ComponentResourcesInjectionProvider());

  configuration.add(
  quot;CommonResourcesquot;,
  new CommonResourcesInjectionProvider(),
  quot;after:Defaultquot;);

  configuration.add(
  quot;Assetquot;,
  new AssetInjectionProvider(symbolSource, assetSource),
  quot;before:Defaultquot;);

  configuration.add(quot;Blockquot;, new BlockInjectionProvider(), quot;before:Defaultquot;);
  configuration.add(quot;Servicequot;, new ServiceInjectionProvider(locator), quot;after:*quot;);
}


And, of course, other contributions could be made in other modules ... if you wanted to add in your own form of injection.

The configuration is converted into a service via a service builder method:



  public InjectionProvider build(Listlt;InjectionProvidergt; configuration, ChainBuilder chainBuilder)
  {
return chainBuilder.build(InjectionProvider.class, configuration);
  }


Now, lets see how this is used. The InjectWorker class looks for fields with the InjectAnnotation, and uses the chain of command to inject the appropriate value. However, to InjectWorker, there is no chain ... just a single object that implements the InjectionProvider interface.



public class InjectWorker implements ComponentClassTransformWorker
{
  private final ObjectLocator 

[CONF] Apache Tapestry IoC cookbook - patterns

2010-11-25 Thread confluence







IoC cookbook - patterns
Page edited by Christophe Cordenier


Comment:
Fix broken links


 Changes (1)
 



...
h1. Chain of Command Pattern  
Letapos;s look at another example, again from the Tapestry code base. The [InjectProvider|../../apidocs/org/apache/tapestry5/services/InjectionProvider.html] [InjectProvider|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/InjectionProvider.html] interface is used to process the @Inject annotation on the fields of a Tapestry page or component. Many different instances are combined together to form a [chain of command|../command.html]. command|IoC - command]. 
 The interface has only a single method (this is far from uncommon): 
...

Full Content

Using Patterns

Tapestry IoC has support for implementing several of the Gang Of Four Design Patterns. In fact, the IoC container itself is a pumped up version of the Factory pattern.

The basis for these patterns is often the use of service builder methods, where a configuration for the service is combined with a factory to produce the service implementation on the fly.

Chain of Command Pattern

Lets look at another example, again from the Tapestry code base. The InjectProvider interface is used to process the @Inject annotation on the fields of a Tapestry page or component. Many different instances are combined together to form a chain of command.

The interface has only a single method (this is far from uncommon):

Unknown macro: {code|borderStyle=solid} 
public interface InjectionProvider
Unknown macro: {
  boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
  ClassTransformation transformation, MutableComponentModel componentModel);
} 



The return type indicates whether the provider was able to do something. For example, the AssetInjectionProvider checks to see if theres an @Path annotation on the field, and if so, converts the path to an asset, works with the ClassTransformation object to implement injection, and returns true to indicate success. Returns true terminates the chain early, and that true value is ultimately returned to the caller.

In other cases, it returns false and the chain of command continues down to the next provider. If no provider is capable of handling the injection, then the value false is ultimately returned.

The InjectionProvider service is built up via contributions. These are the contributions from the TapestryModule:

public static void contributeInjectionProvider(
OrderedConfigurationInjectionProvider configuration,
MasterObjectProvider masterObjectProvider,
ObjectLocator locator,
SymbolSource symbolSource,
AssetSource assetSource)
Unknown macro: {
  configuration.add(Default, new DefaultInjectionProvider(masterObjectProvider, locator));

  configuration.add(ComponentResources, new ComponentResourcesInjectionProvider());

  configuration.add(
  CommonResources,
  new CommonResourcesInjectionProvider(),
  after} 


And, of course, other contributions could be made in other modules ... if you wanted to add in your own form of injection.

The configuration is converted into a service via a service builder method:

{code|borderStyle=solid}
  public InjectionProvider build(Listlt;InjectionProvidergt; configuration, ChainBuilder chainBuilder)
  {
return chainBuilder.build(InjectionProvider.class, configuration);
  }


Now, lets see how this is used. The InjectWorker class looks for fields with the InjectAnnotation, and uses the chain of command to inject the appropriate value. However, to InjectWorker, there is no chain ... just a single object that implements the InjectionProvider interface.

Unknown macro: {code|borderStyle=solid} 
public class InjectWorker implements ComponentClassTransformWorker
{
  private final ObjectLocator locator;

  // Really, a chain of command

  private final InjectionProvider injectionProvider;

  public InjectWorker(ObjectLocator locator, InjectionProvider injectionProvider)
  Unknown macro: {
this.locator = locator;
this.injectionProvider = injectionProvider;
  } 

  public final void transform(ClassTransformation transformation, MutableComponentModel model)
  {
for (String fieldName : transformation.findFieldsWithAnnotation(Inject.class))
{
  Inject annotation = transformation.getFieldAnnotation(fieldName, Inject.class);

  try
  Unknown macro: {
String fieldType = transformation.getFieldType(fieldName);

Class type = transformation.toClass(fieldType);

boolean success = injectionProvider.provideInjection(
fieldName,
type,
locator,
transformation,
model);

if (success) 

[CONF] Apache Tapestry IoC cookbook - patterns

2010-09-20 Thread confluence







IoC cookbook - patterns
Page  added by Ulrich Stärk

 

 Using Patterns

Tapestry IoC has support for implementing several of the Gang Of Four Design Patterns. In fact, the IoC container itself is a pumped up version of the Factory pattern.

The basis for these patterns is often the use of service builder methods, where a configuration for the service is combined with a factory to produce the service implementation on the fly.

Chain of Command Pattern

Lets look at another example, again from the Tapestry code base. The InjectProvider|../../apidocs/org/apache/tapestry5/services/InjectionProvider.html interface is used to process the @Inject annotation on the fields of a Tapestry page or component. Many different instances are combined together to form a chain of command|../command.html.

The interface has only a single method (this is far from uncommon):

Unknown macro: {code|borderStyle=solid} 
public interface InjectionProvider
{
  boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
  ClassTransformation transformation, MutableComponentModel componentModel);
}




The return type indicates whether the provider was able to do something. For example, the AssetInjectionProvider checks to see if theres an @Path annotation on the field, and if so, converts the path to an asset, works with the ClassTransformation object to implement injection, and returns true to indicate success. Returns true terminates the chain early, and that true value is ultimately returned to the caller.

In other cases, it returns false and the chain of command continues down to the next provider. If no provider is capable of handling the injection, then the value false is ultimately returned.

The InjectionProvider service is built up via contributions. These are the contributions from the TapestryModule:

public static void contributeInjectionProvider(
OrderedConfigurationInjectionProvider configuration,
MasterObjectProvider masterObjectProvider,
ObjectLocator locator,
SymbolSource symbolSource,
AssetSource assetSource)
{
  configuration.add(Default, new DefaultInjectionProvider(masterObjectProvider, locator));

  configuration.add(ComponentResources, new ComponentResourcesInjectionProvider());

  configuration.add(
  CommonResources,
  new CommonResourcesInjectionProvider(),
  after:Default);

  configuration.add(
  Asset,
  new AssetInjectionProvider(symbolSource, assetSource),
  before:Default);

  configuration.add(Block, new BlockInjectionProvider(), before:Default);
  configuration.add(Service, new ServiceInjectionProvider(locator), after:*);
}


And, of course, other contributions could be made in other modules ... if you wanted to add in your own form of injection.

The configuration is converted into a service via a service builder method:

{code|borderStyle=solid}
  public InjectionProvider build(Listlt;InjectionProvidergt; configuration, ChainBuilder chainBuilder)
  {
return chainBuilder.build(InjectionProvider.class, configuration);
  }


Now, lets see how this is used. The InjectWorker class looks for fields with the InjectAnnotation, and uses the chain of command to inject the appropriate value. However, to InjectWorker, there is no chain ... just a single object that implements the InjectionProvider interface.

Unknown macro: {code|borderStyle=solid} 
public class InjectWorker implements ComponentClassTransformWorker
{
  private final ObjectLocator locator;

  // Really, a chain of command

  private final InjectionProvider injectionProvider;

  public InjectWorker(ObjectLocator locator, InjectionProvider injectionProvider)
  Unknown macro: {this.locator = locator;this.injectionProvider = injectionProvider;  } 

  public final void transform(ClassTransformation transformation, MutableComponentModel model)
  {
for (String fieldName : transformation.findFieldsWithAnnotation(Inject.class))
{
  Inject annotation = transformation.getFieldAnnotation(fieldName, Inject.class);

  try
  Unknown macro: {String fieldType = transformation.getFieldType(fieldName);
Class type = transformation.toClass(fieldType);
boolean success = injectionProvider.provideInjection(fieldName,type,locator,transformation,model);
if (success) transformation.claimField(fieldName, annotation);  } 
  catch (RuntimeException ex)
  Unknown macro: {throw new RuntimeException(ServicesMessages.fieldInjectionError(transformation.getClassName(), fieldName, ex), ex);  } 

}
  }
}




Reducing the chain to a single object vastly simplifies the code: weve factored out the loop implicit in the chain of command. That eliminates a lot of code, and thats less code to test, and fewer paths through InjectWorker, which lowers its complexity further. We dont have to test the cases 

[CONF] Apache Tapestry IoC cookbook - patterns

2010-09-20 Thread confluence







IoC cookbook - patterns
Page moved by Ulrich Stärk






From: 

Apache Tapestry
 IoC


To: 

Apache Tapestry
 IoC cookbook





Children moved






   
Change Notification Preferences
   
   View Online
   









[CONF] Apache Tapestry IoC cookbook - patterns

2010-09-20 Thread confluence







IoC cookbook - patterns
Page moved by Ulrich Stärk






From: 

Apache Tapestry



To: 

Apache Tapestry
 IoC





Children moved






   
Change Notification Preferences
   
   View Online