Created
December 11, 2012 22:46
-
-
Save danveloper/4263045 to your computer and use it in GitHub Desktop.
This is how I implement the Provider pattern in Groovy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danveloper.providers | |
import com.danveloper.support.ApplicationHolder as AH | |
import org.hibernate.SessionFactory as SF | |
public interface Provider<T> { | |
T get() | |
} | |
class Support { | |
private static def _ = [:] | |
public final static def sessionProvider = [get: { | |
_.session ?: { _.session = AH.getBean(SF)?.currentSession; _.session }() | |
}] as Provider<SF> | |
} | |
class ModelDAO extends Support { | |
def save(def domainClass) { | |
def session = sessionProvider.get() | |
def tx = session.beginTransaction() | |
def id = session.saveOrUpdate(domainClass) | |
tx.commit() | |
domainClass | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment