[Logo] Mentawai Forum - Mentawai Web Framework
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 

Forum Read-Only! Check the new site and forum here!

Mentawai 1.1 vai suportar Inversion of Control (IoC)  XML
Forum Index -> Comentários Gerais
Author Message
saoj



Joined: 01/07/2005 09:59:17
Messages: 2846
Location: Rio de Janeiro, RJ
Offline

O que vcs acharam disso:


Code:
 public interface Talker {
     
     public String saySomething();
     
 }
 
 public class UnpoliteTalker implements Talker {
     
     private String name;
     
     public UnpoliteTalker(String name) {
         this.name = name;
     }
     
     public String saySomething() {
         return "from " + name + " (" + toString() + "): Hello you #$#((%$#!";
     }
     
 }
 
 public class PoliteTalker implements Talker {
     
     private String name;
     
     public void setName(String name) {
         this.name = name;
     }
     
     public String saySomething() {
         return "from " + name + " (" + toString() + "): Hello Sir!";
     }
     
 }
 
 


Para usar esses carinhas com o IoC do Mentawai basta usar o IoCFilter junto com o InjectionFilter.

Code:
 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() {
         // IoC Components...
         DefaultComponent c1 = new DefaultComponent(PoliteTalker.class);
         c1.addProperty("name", "C1");
         
         DefaultComponent c2 = new DefaultComponent(UnpoliteTalker.class);
         c2.addInitValue("C2"); // for constructor...
         
         this.c1 = c1;
         this.c2 = c2;
     }
     
     public void loadActions() {
     
         ActionConfig ac = new ActionConfig("/HelloIoC", HelloIoC.class);
         ac.addConsequence(HelloIoC.SUCCESS, new Forward("/show.jsp"));
         addActionConfig(ac);
         
         // IoC	filters...	
         ac.addFilter(new IoCFilter(c1, "talker1", IoCFilter.ACTION));
         ac.addFilter(new IoCFilter(c2, "talker2", IoCFilter.SESSION));
         ac.addFilter(new IoCFilter(c2, "talker3", IoCFilter.APPLICATION));
         
         // Injection Filter needs to be used together with the IoCFilter...
         ac.addFilter(new InjectionFilter(true));
     }
 }
 
 


Os componentes são injetados na action pelo InjectionFilter:

Code:
 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(Talker t) {
         this.talker1 = t;
     }
     
     public void setTalker2(Talker t) {
         this.talker2 = t;
     }
     
     // t3 will be injected straight into the private field... (no need for setter)
     
     public String execute() throws ActionException {
         String s1 = null;
         String s2 = null;
         String s3 = null;
         
         if (talker1 != null) {
             s1 = talker1.saySomething();
             output.setValue("from_t1", s1);
         }
         if (talker2 != null) {
             s2 = talker2.saySomething();
             output.setValue("from_t2", s2);
         }
         if (talker3 != null) {
             s3 = talker3.saySomething();
             output.setValue("from_t3", s3);
         }
         return SUCCESS;
     }
 }
 


Pontos positivos:

Pode-se configurar as propriedades de cada componente. (WW não permite isso...)

Pode-se usar componentes com construtores. (WW não permite isso...)

Não precisa de interface enablers. (WW precisa de enablers...)

Suporta injection por setter e por field privado ou publico.

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


Sergio Oliveira

andredelorme



Joined: 20/07/2005 11:41:46
Messages: 51
Offline

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.


André Delorme
 
Forum Index -> Comentários Gerais
Go to:   
Powered by JForum 2.1.6 © JForum Team