Dashboard > RIFE > ... > Upcoming changes > Add full IoC support
RIFE Log In | Sign Up   View a printable version of the current page.
Add full IoC support


Added by Geert Bevin, last edited by Geert Bevin on May 26, 2005  (view change)
Labels: 
(None)

Full IoC is one of the last missing pieces in RIFE. It should not replace the current mechanisms such as Config and Rep, but rather complement it. The newer term dependency injection is much more approriate. RIFE should focus on providing a central location to inject common data into instantiated objects. These can for example typically be datasources, templates, config values, participants, etc etc.

The current property mechanism is a first step towards IoC, it currently allows the injection of string values only. This has to be extended to the injection of any object reference.
The main focus now has to go to the definition of these object references.

Most IoC frameworks focus for the definition of these references on an XML format. Imho this is a bit limited since ideally you'd like to execute a few statements to obtain or create these object references.

in Java, I'd do:

Datasource myds = Datasources.getRepInstance().getDatasource(Config.getRepInstance().getString("DATASOURCE"));
...
.enterSubsite("PUB")
.setProperty("datasource", myds)
...
.leaveSubsite()
...

but in an XML site declaration this gets trickier. The property declaration can still be used with a <ref> tag instead of literal string. This tag can then contain the id of a declared reference.

<property name="datasource"><ref>myds</ref></property>

A simple bean reference could be easily declared like this:

<ref id="myds"><bean>my.pakkage.MyBean</bean></ref>

It's still blurry though how to best declare complex references.
The 2 approaches that I've been thinking of are:

  1. registering a java reference factory
  2. deferring the reference instantiation to a java class or a scripting language like Groovy

registering a java reference factory

package my.pakkage;
public class MyReferences implements ReferenceFactory
{
    public MyReferences() {};

    public Map<String, Object> getReferences()
    {
        HashMap<String, Object> refs = new HashMap<String, Object>();
        refs.put("myds", Datasources.getRepInstance().getDatasource(Config.getRepInstance().getString("DATASOURCE")));

        return refs;
    }
}
<refs>my.pakkage.MyReferences</refs>
<property name="datasource"><ref>myds</ref></property>

deferring the reference instantiation

package my.pakkage;
public class MyDs implements Reference
{
    public MyDs() {};

    public Object getReference()
    {
        return Datasources.getRepInstance().getDatasource(Config.getRepInstance().getString("DATASOURCE")));
    }
}
<ref id="myds"><java>my.pakkage.MyDs</java></ref>
<property name="datasource"><ref>myds</ref></property>

Besides references, it would also be nice to create shortcut tags to get instances of commonly used objects in RIFE.

Such as:

config parameters

<property name="rowsOnPage"><config>ROWS_ON_PAGE</config></property>

which would be equivalent to defining the following with a plain reference:

package my.pakkage;

public class RowsOnPage implements Reference
{
    public RowsOnPage() {};

    public Object getReference()
    {
        return new Integer(Config.getRepInstance().getInt("ROWS_ON_PAGE"));
    }
}
<ref id="rowsOnPage"><java>my.pakkage.RowsOnPage</java></ref>
<property name="rowsOnPage"><ref>rowsOnPage</ref></property>

templates

<property name="template"><template type="enginehtml">my.template</template></property>

which would be equivalent to defining the following with a plain reference:

package my.pakkage;

public class MyTemplate implements Reference
{
    public MyTemplate() {};

    public Object getReference()
    {
        return TemplateFactory.ENGINEHTML.get("my.template");
    }
}
<ref id="myTemplate"><java>my.pakkage.MyTemplate</java></ref>
<property name="template"><ref>myTemplate</ref></property>

datasources

<property name="datasource"><datasource>postgresql_local</datasource></property>

which would be equivalent to defining the following with a plain reference:

package my.pakkage;

public class MyDatasource implements Reference
{
    public MyDatasource() {};

    public Object getReference()
    {
        return Datasources.getRepInstance().getDatasource("postgresql_local");
    }
}
<ref id="myDatasource"><java>my.pakkage.MyDatasource</java></ref>
<property name="datasource"><ref>myDatasource</ref></property>

These shortcuts would of course also be available for references, like this:

config params

<ref id="rowsOnPage"><config>ROWS_ON_PAGE</config></ref>
<property name="rowsOnPage"><ref>rowsOnPage</ref></property>

templates

<ref id="myTemplate"><template type="enginehtml">my.template</template></ref>
<property name="template"><ref>myTemplate</ref></property>

datasources

<ref id="myDatasource"><datasource>postgresql_local</datasource></ref>
<property name="datasource"><ref>myDatasource</ref></property>

Like this you can still benefit from the shortcut notation and use a defined single reference in different properties.



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators