You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Damon Childs <mi...@gmail.com> on 2015/09/14 15:35:09 UTC

Custom ValueEncoder & IOC

How do i get a custom ValueEncoder under control of IOC?  

I have a ValueEncoder that translates a id to a Object from a db.  

public class PropertyTypeEncoder implements ValueEncoder<LookupPropertyType> {

    @Inject
    private Session session;

    public  PropertyTypeEncoder(Session session) {
        this.session = session;
    }

    public String toClient(LookupPropertyType value) {
        return String.valueOf(value.getId());
    }

    public LookupPropertyType toValue(String id) {
        Criteria criteria = session.createCriteria(LookupPropertyType.class);
        criteria.add(Restrictions.eq("id", Long.parseLong(id)));
        LookupPropertyType type = (LookupPropertyType)criteria.uniqueResult();
        return type;
    }
}

To get it created of the web form I have:
 @Property
private final PropertyTypeEncoder propertyTypeEncoder = new PropertyTypeEncoder(this.session);

Im passing the Hibernate session via the constructer since this would not be under the IOC’s control. 

Is there a way to create this using the IOC so the inject works?

Thanks,
Damon
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Custom ValueEncoder & IOC

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Mon, 14 Sep 2015 10:41:49 -0300, Chris Poulsen <ma...@nesluop.dk>  
wrote:

> Can't you could just bind it in your app module?

Yep! Or also contribute it to the ValueEncoderSource service, so it's  
automatically picked up by Select and other components. As in any other  
IoC container, in order to have dependencies injected, you must make your  
object/class a service/bean which is instantiated by the IoC container.

-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Custom ValueEncoder & IOC

Posted by Chris Poulsen <ma...@nesluop.dk>.
Can't you could just bind it in your app module?

--
Chris


On Mon, Sep 14, 2015 at 3:35 PM, Damon Childs <mi...@gmail.com> wrote:

> How do i get a custom ValueEncoder under control of IOC?
>
> I have a ValueEncoder that translates a id to a Object from a db.
>
> public class PropertyTypeEncoder implements
> ValueEncoder<LookupPropertyType> {
>
>     @Inject
>     private Session session;
>
>     public  PropertyTypeEncoder(Session session) {
>         this.session = session;
>     }
>
>     public String toClient(LookupPropertyType value) {
>         return String.valueOf(value.getId());
>     }
>
>     public LookupPropertyType toValue(String id) {
>         Criteria criteria =
> session.createCriteria(LookupPropertyType.class);
>         criteria.add(Restrictions.eq("id", Long.parseLong(id)));
>         LookupPropertyType type =
> (LookupPropertyType)criteria.uniqueResult();
>         return type;
>     }
> }
>
> To get it created of the web form I have:
>  @Property
> private final PropertyTypeEncoder propertyTypeEncoder = new
> PropertyTypeEncoder(this.session);
>
> Im passing the Hibernate session via the constructer since this would not
> be under the IOC’s control.
>
> Is there a way to create this using the IOC so the inject works?
>
> Thanks,
> Damon
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>