Created
February 27, 2011 14:37
-
-
Save debasishg/846226 to your computer and use it in GitHub Desktop.
Justin's example refactored for currying ..
This file contains hidden or 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
// like to keep domain objects pure | |
// hence moved repository injection to the service module | |
case class User(name: String) | |
trait UserRepository{ | |
def save(user: User): Unit | |
} | |
class RiakUserRepository extends UserRepository{ | |
def save(user: User) = println("saves to Riak") | |
} | |
class MongoUserRepository extends UserRepository{ | |
def save(user: User) = println("saves to Mongo") | |
} | |
object UserService { | |
val create: UserRepository => String => Unit = {repo => name => repo.save(User(name))} | |
} | |
object RiakContext { | |
import UserService._ | |
val create_c = create(new RiakUserRepository) // returns a fn (String) => Unit | |
} | |
object MongoContext { | |
import UserService._ | |
val create_c = create(new MongoUserRepository) // returns a fn (String) => Unit | |
} | |
object Main_1 { | |
def run_1 = { | |
import RiakContext._ | |
create_c("justin") | |
} | |
def run_2 = { | |
import MongoContext._ | |
create_c("justin") | |
} | |
} | |
should be a val .. changed ..
This can be done with the above design as well .. I have updated the gist above .. Please review ..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please, could you explain why you chose "createForRiak" to be a def and not a val?