It depends on whether you want to test or not.
If you inject UserAuthenticator as a class, you're locked into one
implementation of UserAuthenticator. You forgoe the ability to unit test
using EasyMock (or jMock) because you aren't coding to an interface, but to
a class. Sure, EasyMock has an extention that lets you mock objects, but it
has unexpected limitations. I aways code to an interface, almost
reflexively.
Further, T5 IoC service defined in terms of a class is not proxied: it is
realized on first reference (not on first use) and can't be decorated with
interceptors. There goes easy logging and maybe other desired behaviors in
the future.
On 5/15/07, Marcus <[EMAIL PROTECTED]> wrote:
Hi,
Which is the better approach, or which is the main difference?
Have a service to do Login validation, like:
================
in AppModule.java
public static IUserAuthenticator buildUserAuthenticator()
{
return new UserAuthenticatorImpl();
}
IUserAuthenticator.java
package org.example.hilo.services.interfaces;
public interface IUserAuthenticator
{
public boolean isValid(String userName, String pwd);
}
UserAuthenticatorImpl.java
package org.example.hilo.services;
import org.example.hilo.services.interfaces.IUserAuthenticator;
public class UserAuthenticatorImpl implements IUserAuthenticator
{
public boolean isValid(String userName, String pwd)
{
if (userName.equalsIgnoreCase(pwd))
{ return true; }
return false;
}
}
in Login.java
@Inject
private IUserAuthenticator _authenticator;
public String onSuccess()
{
if (!_authenticator.isValid(_userName, _password))
{
_form.recordError(_passwordField, "Invalid user name or password.");
return null;
}
return "Start";
}
================
or have a class to do that, like:
================
UserAuthenticator.java
package org.example.hilo.beans;
public class UserAuthenticator
{
public boolean isValid(String userName, String pwd)
{
if (userName.equalsIgnoreCase(pwd))
{ return true; }
return false;
}
}
in Login.java
@Inject
private UserAuthenticator _authenticator;
String onSuccess()
{
if (!_authenticator.isValid(_userName, _password))
{
_form.recordError(_passwordField, "Invalid user name or password.");
return null;
}
return "Start";
}
Thank's
Marcus
Related Links:
http://tapestry.apache.org/tapestry5/tapestry-core/index.html
http://elozovan.blogspot.com/2007/05/simpletapestry-5-crud-application-step.html
--
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind
Professional Tapestry training, mentoring, support
and project work. http://howardlewisship.com