<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Mentawai 1.1 vai suportar Inversion of Control (IoC)"]]></title>
		<link>http://forum.mentaframework.org/posts/list/3.page</link>
		<description><![CDATA[Latest messages posted in the topic "Mentawai 1.1 vai suportar Inversion of Control (IoC)"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Mentawai 1.1 vai suportar Inversion of Control (IoC)</title>
				<description><![CDATA[ O que vcs acharam disso:


<span class="genmed"><b>Code:</b></span><br>
		<div style="overflow: auto; width: 100%;">
		<pre>
public interface Talker {
    
    public String saySomething&#40;&#41;;
    
}

public class UnpoliteTalker implements Talker {
    
    private String name;
    
    public UnpoliteTalker&#40;String name&#41; {
        this.name = name;
    }
    
    public String saySomething&#40;&#41; {
        return "from " + name + " &#40;" + toString&#40;&#41; + "&#41;: Hello you #$#&#40;&#40;%$#!";
    }
    
}

public class PoliteTalker implements Talker {
    
    private String name;
    
    public void setName&#40;String name&#41; {
        this.name = name;
    }
    
    public String saySomething&#40;&#41; {
        return "from " + name + " &#40;" + toString&#40;&#41; + "&#41;: Hello Sir!";
    }
    
}

</pre>
		</div>

Para usar esses carinhas com o IoC do Mentawai basta usar o IoCFilter junto com o InjectionFilter.

<span class="genmed"><b>Code:</b></span><br>
		<div style="overflow: auto; width: 100%;">
		<pre>
import org.mentawai.core.*;
import org.mentawai.filter.*;
import org.mentawai.ioc.*;
import examples.ioc.*;

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
    
    private Component c1;
    private Component c2;
    
    public void init&#40;&#41; {
        // IoC Components...
        DefaultComponent c1 = new DefaultComponent&#40;PoliteTalker.class&#41;;
        c1.addProperty&#40;"name", "C1"&#41;;
        
        DefaultComponent c2 = new DefaultComponent&#40;UnpoliteTalker.class&#41;;
        c2.addInitValue&#40;"C2"&#41;; // for constructor...
        
        this.c1 = c1;
        this.c2 = c2;
    }
    
    public void loadActions&#40;&#41; {
    
        ActionConfig ac = new ActionConfig&#40;"/HelloIoC", HelloIoC.class&#41;;
        ac.addConsequence&#40;HelloIoC.SUCCESS, new Forward&#40;"/show.jsp"&#41;&#41;;
        addActionConfig&#40;ac&#41;;
        
        // IoC	filters...	
        ac.addFilter&#40;new IoCFilter&#40;c1, "talker1", IoCFilter.ACTION&#41;&#41;;
        ac.addFilter&#40;new IoCFilter&#40;c2, "talker2", IoCFilter.SESSION&#41;&#41;;
        ac.addFilter&#40;new IoCFilter&#40;c2, "talker3", IoCFilter.APPLICATION&#41;&#41;;
        
        // Injection Filter needs to be used together with the IoCFilter...
        ac.addFilter&#40;new InjectionFilter&#40;true&#41;&#41;;
    }
}

</pre>
		</div>

Os componentes são injetados na action pelo InjectionFilter:

<span class="genmed"><b>Code:</b></span><br>
		<div style="overflow: auto; width: 100%;">
		<pre>
import java.util.*;

import org.mentawai.core.*;

public class HelloIoC extends BaseAction {
    
    private Talker talker1 = null;
    private Talker talker2 = null;
    private Talker talker3 = null;
    
    public void setTalker1&#40;Talker t&#41; {
        this.talker1 = t;
    }
    
    public void setTalker2&#40;Talker t&#41; {
        this.talker2 = t;
    }
    
    // t3 will be injected straight into the private field... &#40;no need for setter&#41;
    
    public String execute&#40;&#41; throws ActionException {
        String s1 = null;
        String s2 = null;
        String s3 = null;
        
        if &#40;talker1 != null&#41; {
            s1 = talker1.saySomething&#40;&#41;;
            output.setValue&#40;"from_t1", s1&#41;;
        }
        if &#40;talker2 != null&#41; {
            s2 = talker2.saySomething&#40;&#41;;
            output.setValue&#40;"from_t2", s2&#41;;
        }
        if &#40;talker3 != null&#41; {
            s3 = talker3.saySomething&#40;&#41;;
            output.setValue&#40;"from_t3", s3&#41;;
        }
        return SUCCESS;
    }
}
</pre>
		</div>

Pontos positivos:

 :arrow: Pode-se configurar as propriedades de cada componente. (WW não permite isso...)

 :arrow: Pode-se usar componentes com construtores. (WW não permite isso...)

 :arrow: Não precisa de interface enablers. (WW precisa de enablers...)

 :arrow: Suporta injection por setter e por field privado ou publico.

 :arrow: Suporta scopes ACTION, REQUEST, SESSION e APPLICATION. (Diferença do ACTION para o REQUEST é que o request atinge todas as actions encadeadas e o ACTION apenas uma action)

Me basiei na comparação feita entre o WW e o Spring aqui: http://www.theserverside.com/news/thread.tss?thread_id=21962

]]></description>
				<guid isPermaLink="true">http://forum.mentaframework.org/posts/list/25.page#111</guid>
				<link>http://forum.mentaframework.org/posts/list/25.page#111</link>
				<pubDate><![CDATA[Wed, 27 Jul 2005 21:46:51]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
			<item>
				<title>Re:Mentawai 1.1 vai suportar Inversion of Control (IoC)</title>
				<description><![CDATA[ Acho que está legal, bastante flexível, porém ainda não utilizo IoC em meus projetos, portanto não tenho muito a contribuir com essa questão.]]></description>
				<guid isPermaLink="true">http://forum.mentaframework.org/posts/list/25.page#117</guid>
				<link>http://forum.mentaframework.org/posts/list/25.page#117</link>
				<pubDate><![CDATA[Thu, 28 Jul 2005 09:23:29]]> GMT</pubDate>
				<author><![CDATA[ andredelorme]]></author>
			</item>
	</channel>
</rss>
