IoC like SpringFramework.org

Hi,

Was wondering if you gave any thoughts to using Inversion of Control (IoC) like Spring framework for Java.  http://www.theserver…SpringFramework has a nice article on the benefits of IoC.

One of the things I like about the IoC framework was in testing.  The ability to easily mock up objects for testing.

Thanks,

Edward

Could you please give more details about your thought on using IoC to do testing? I'm not very clear about IoC. It seems to me that it is more like event scheme.

Hi,

Well IoC is Inversion of Control and has lot to do with creating objects.

(Design Pattern info http://martinfowler…injection.html)

Best introduction is here…  http://www.theserver…IntrotoSpring25

Benefits of IoC in a Java application (http://appfuse.org). 

Able to utilize various frameworks, OR Mapper, Logging systems, … with little effort.

In a very simple illustration,

Application needs to several Components

  IBizRule  $biz  = Create ComplexBizRule

  IDatabase $db = Create CMySqlDatabase

  ILogger $logger = Create CFileLogger

With Inversion of Control

Use the "Factory" the create the Application object,

  The Factory is configured with the Dependencies for the Application

  like $complexBusinessRule, $db and $logger

The Factory in the process of creating the Application will also create the

  the configured ComplexBizRule, CMySqlDatabase and CFileLogger objects and

  Application->setBizRule(), Application->setDatabase(), Application->setLogger()

Since the application uses the IBizRule, IDatabase and ILogger interfaces,

the configuration can be changed to use stubs for testing.

This is very useful when you want to stub ComplexBizRule.

Hope this makes it little more clear.

It does seem like your are using a "Factory" type object creation in CConfiguration, it maybe better to to use a Factory pattern.

Maybe you can get a more concrete info from a small Java implementation http://www.picocontainer.org/ guys.  I can’t seem to find a PHP IoC implementation.  I’m willing to work on it if you are interested in it.

Thanks,

Edward

I'm not sure if I fully understand it. Based on your description, it seems CApplication is using a pattern very close to IoC: in app config, you declare classes and initial properties for application components (some are core components such as request, session); the application instance will instantiate and initialize these components when needed; and if a component depends on another component, the dependent component may also get created. Developers can use their own classes to replace the default application component classes, as long as they follow the required interfaces or base classes.

Please correct me if I understand wrong.

You are correct…I did mention that by the use of the CConfiguration, CApplication is doing something like that.

Issues are:

1.  Does the current system only only applies to the System components.

    If had an application hierarchy of it own objects, I would have to implement my own system.

2.  I guess dependent components are created but …

    A needs B, and C,  A is created.  A creates B and C as part of Init…

    When A is created, B and C are not automatically created and references passed to A.

    (Q: Can A get it's own instance of the object B and C or does it always share a singleton instance?)

    In the IoC case, Creating A will cause B and C to be created and the reference Injected into A.  Thus the description  "Inversion of Control".  The IoC container has the control and not the objects themselves.  The config will determine if the dependent objects are a singletons or local instances.

3.  A minor point but … Using a well defined patterns (Factory, and IoC) users will have easier time understanding the internals.

I'm not sure if changing the inner workings of the framework at this time is worth the effort.  But, moving toward more standard patterns over time would be.  The only reason I brought it up is because (like you said) CApplication is doing something like that and moving to a standard pattern would be awesome.

Thanks for hearing me out…

Edward

Quote

Maybe you can get a more concrete info from a small Java implementation http://www.picocontainer.org/ guys.  I can't seem to find a PHP IoC implementation.  I'm willing to work on it if you are interested in it.

There is a PHP5 implementation of PicoContainer. Source code is still available in SVN:

http://svn.codehaus…ontainer/trunk/

You can read some posts about this port at SitePoint forum.

Yii Framework draw my attention because of it's high performance, component design and flexibility. It will be great if IoC container became part of core, since basic fundamentals are already there.

Thank you for your information. IoC seems to be gaining attention recently. I have read some materials about it and will think about how to utilize or implement it in Yii. If you have any suggestions, feel free to post it. We may keep this discussion going.

mmm realy smart… like IA…



  function testFailWithCyclicDependency() 


    {                


 $pico = new DefaultPicoContainer();         


 $pico->registerComponentImplementation('A'); 


 $pico->registerComponentImplementation('B');


 $pico->registerComponentImplementation('C');


                                 


try         


{             


 $c2 = $pico->getComponentInstance('A');


 $this->fail();         


}


catch (CyclicDependencyException $e)


         { 


        $this->pass(); 


       


        }     





 } 


Here is a good explanation of IoC/Dependency Injection and a Libaray to utilize.

One of the areas where IoC shines is when used for testing.

You are able to easily replace objects with Mock objects.

For example, replacing the Model objects with mock Model so the tests are not dependent on the state of the database.

http://components.symfony-project.org/dependency-injection/documentation

Besides the picocontainer implementation for PHP that has been trailing at least one version behind the Java version, there are a few other implementations…

The Lion framework is an implementation of Swing written in PHP with IOC and a set of AWT-like widgets. It supports MVC, MVP or a combination of the two.

I believe that the Solar framework has has IOC since one of the earliest versions.

On the phpclasses.org site, there is a PHP implementation of the IOC pattern, but the license may be restrictive depending on your needs.

The pmvc framework (I’m not allowed to post links yet, but it’s called “pmvc-framework” and it’s on google code) is a full fledged IoC container for PHP that closeel models spring. In fact, it even has things like dynamic proxies and a full fledged MVC framework (among other things).