How to manage Boot-time state for Snippets?

20 views
Skip to first unread message

James Earl Douglas

unread,
Dec 3, 2024, 3:26:15 PM12/3/24
to Lift
I have a Snippet class that has an external dependency (e.g. a REST client for some third-party service).  Typically, I would inject this via a constructor argument, but since Lift uses reflection to instantiate Snippet classes on request, I'm not sure how best to do this.

1. I want the dependency externalized, so I can instantiate my Snippet class in a test and inject my own mock/stub version for testing.

2. I don't think I can use a constructor argument, since Lift won't be able to instantiate my Snippet class at request time.

Would it be better to have a global object (e.g. `object bootstrap.liftweb.Boot`) with a mutable variable that gets set by the Lift `Boot.boot` procedure?  A major downside to this is that the variable becomes an invisible dependency that I have to remember to set before running my test.

Is there any way for Lift to support a Snippet that has a constructor?  Or are there any better patterns for this?

David Pollak

unread,
Dec 4, 2024, 5:59:29 PM12/4/24
to lif...@googlegroups.com
James,

I tend to put this kind of thing in a SessionVar for stuff that's the duration of a session: https://liftweb.net/api/32/api/net/liftweb/http/SessionVar.html

Also, Lift supports Dependency Injection: https://app.assembla.com/wiki/show/liftweb/Dependency_Injection

Note the above Assembla article references Lift 2.x... there may be API changes in Lift 3.x

Hope this helps.

Thanks,

David


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/liftweb/9738a914-af9b-4887-b082-75fc802579d0n%40googlegroups.com.


--
Lift, the simply functional web framework https://liftweb.net

Reply all
Reply to author
Forward
0 new messages