Assembly

Assembly is the name of the boot-strap phase, where the application Structure is declared (programmatically). The Assembly will be consumed by the ApplicationBuilder, which produces an ApplicationInstance. This instance does not contain any custom objects, and is fully serializable. All the application structure has been built, all the layers and modules been wired up, and all the sub-composite structures are in place to quickly instantiate the various parts of the application.

At this point, where an ApplicationInstance exists, it is possible to initialize the application components with instances created in, data computed in or received from, the controlling bootstrap code.

Once the initialization phase is complete, the bootstrap controller will call the ApplicationInstance.activate() method to start things up.

Recap of sequence;

  • Create, obtain or lookup Assemblies.
  • Establish the application structures.
  • Create a Qi4j Runtime instance.
  • Create an ApplicationAssemblyFactory.
  • Create an ApplicationFactory.
  • Call ApplicationFactory.newApplication() to create an ApplicationContext.
  • Call ApplicationContext.newApplication() to create an ApplicationInstance.
  • Do the initialization of the application.
  • Call activate() on the ApplicationInstance.

Example - Singleton Assembly

SingletonAssembly assembly = new SingletonAssembly()
{
    public void configure( ModuleAssembly module ) throws AssemblyException
    {
        module.addServices( MyService.class ).identifiedBy( "Foo" ) );
        module.addServices( MyService.class ).identifiedBy( "Bar" ) );
        module.addObjects( Stuff.class );
    }
};

ObjectBuilderFactory factory = assembly.getObjectBuilderFactory();
Stuff stuff = factory.newObjectBuilder( Stuff.class ).newInstance();

Example - Explicit Assembly


public class Main
{
    private static Main instance;
    private Qi4jRuntime runtime;

    public static void Main( String[] args )
    {
        instance = new Main();
        instance.setUp();
        // assuming some code has a non-daemon thread.
    }

    private void setUp()
    {
        Qi4jRuntime is = new Energy4Java();
        runtime = is;
        ApplicationAssemblyFactory assemblyFactory =
            new ApplicationAssemblyFactory();
        ApplicationFactory factory =
            new ApplicationFactory( runtime, assemblyFactory );

        ApplicationAssembly app = createAssembly( assemblyFactory );

        ApplicationContext context = factory.newApplication( assembly );

        String applicationName = "Example Application";
        ApplicationInstance instance =
            context.newApplicationInstance( applicationName );
    }

    private ApplicationAssembly createAssembly(
        ApplicationAssemblyFactory factory )
    {
        ApplicationAssembly app = factory.newApplicationAssembly();
        LayerAssembly webLayer = createWebLayer( app );
        LayerAssembly domainLayer = createDomainLayer( app );
        LayerAssembly infraLayer = createInfrastructureLayer( app );
        webLayer.uses( domainLayer );
        webLayer.uses( infraLayer );  // Accesses the WebService
        domainLayer.uses( infraLayer ); // For persistence
        return app;
    }

    private LayerAssembly createWebLayer(
        ApplicationAssembly application )
    {
        LayerAssembly layer = application.newLayerAssembly();
        createCustomerWebModule( layer );
        return layer;
    }

    private LayerAssembly createDomainLayer(
        ApplicationAssembly application )
    {
        LayerAssembly layer = application.newLayerAssembly();
        createCustomerDomainModule( layer );
        // :
        // :
        return layer;
    }

    private LayerAssembly createInfrastructureLayer(
        ApplicationAssembly application )
    {
        LayerAssembly layer = application.newLayerAssembly();
        createWebServiceModule( layer );
        createPersistenceModule( layer );
        return layer;
    }

    private void createCustomerWebModule( LayerAssembly layer )
    {
        ModuleAssembly assembly = layer.newModuleAssembly();
        assembly.addComposites( CustomerViewComposite.class );
        assembly.addComposites( CustomerEditComposite.class );
        assembly.addComposites( CustomerListViewComposite.class );
        assembly.addComposites( CustomerSearchComposite.class );
    }

    private void createCustomerDomainModule( LayerAssembly layer )
    {
        ModuleAssembly assembly = layer.newModuleAssembly();
        assembly.addEntities( CustomerEntity.class );
        assembly.addEntities( CountryEntity.class );
        assembly.addComposites( AddressComposite.class );
    }

    private void createWebServiceModule( LayerAssembly layer )
    {
        ModuleAssembly assembly = layer.newModuleAssembly();
        // Someone has created an assembler for a Jetty Web Service.
        JettyAssembler jetty = new JettyAssembler( 8080 );
        assembly.addAssembler( neo );
    }

    private void createPersistenceModule( LayerAssembly layer )
    {
        ModuleAssembly assembly = layer.newModuleAssembly();
        // Someone has created an assembler for the Neo EntityStore
        NeoAssembler neo = new NeoAssembler( "./neostore" );
        assembly.addAssembler( neo );
    }
}

Powered by SiteVisionexternal link.