Custom ValueEncoder & IOC

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom ValueEncoder & IOC

Damon
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Custom ValueEncoder & IOC

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

--
Chris


On Mon, Sep 14, 2015 at 3:35 PM, Damon Childs <[hidden email]> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Custom ValueEncoder & IOC

Thiago H de Paula Figueiredo
On Mon, 14 Sep 2015 10:41:49 -0300, Chris Poulsen <[hidden email]>  
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: [hidden email]
For additional commands, e-mail: [hidden email]