scala-ddd-base provides traits to support for ddd repositories and aggregates.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
core/src/main/scala/com/github/j5ik2o/dddbase
example
flyway/src/test/resources/rdb-migration
jdbc
nosql
project
.gitignore
.scalafmt.conf
README.md
build.sbt
release.sbt
scalastyle-config.xml
version.sbt

README.md

scala-ddd-base

CircleCI Maven Central Scaladoc License: MIT

scala-ddd-base provides traits to support for ddd repositories and aggregates.

Installation

Add the following to your sbt build (Scala 2.11.x, 2.12.x):

resolvers += "Sonatype OSS Release Repository" at "https://oss.sonatype.org/content/repositories/releases/"

libraryDependencies ++= Seq(
  "com.github.j5ik2o" %% "scala-ddd-base-core" % "1.0.12",
  // Please set as necessary
  // "com.github.j5ik2o" %% "scala-ddd-base-slick" % "1.0.12"
  // "com.github.j5ik2o" %% "scala-ddd-base-skinny" % "1.0.12"
  // "com.github.j5ik2o" %% "scala-ddd-base-redis" % "1.0.12"
  // "com.github.j5ik2o" %% "scala-ddd-base-memcached" % "1.0.12"
  // "com.github.j5ik2o" %% "scala-ddd-base-memory" % "1.0.12" 
)

Core traits

The following provides basic abstract methods.

  • AggregateSingleReader
  • AggregateSingleWriter
  • AggregateMultiReader
  • AggregateMultiWriter
  • AggregateSingleSoftDeletable
  • AggregateSingleHardDeletable
  • AggregateMultiSoftDeletable
  • AggregateMultiHardDeletable

Support traits

The following provides an implementation for each ORM/KVS.

  • AggregateSingleReadFeature
  • AggregateSingleWriteFeature
  • AggregateMultiReadFeature
  • AggregateMultiWriteFeature
  • AggregateSingleSoftDeleteFeature
  • AggregateSingleHardDeleteFeature
  • AggregateMultiSoftDeleteFeature
  • AggregateMultiHardDeleteFeature

The supported ORM/KVS/Cache is below.

Example

Please mix in the core and support traits to your implementation. Slick, SkinnyORM, Memcached, Redis, Memory etc. You can also choose the implementation as you like.

trait UserAccountRepository[M[_]]
    extends AggregateSingleReader[M]
    with AggregateMultiReader[M]
    with AggregateSingleWriter[M]
    with AggregateMultiWriter[M]
    with AggregateSingleSoftDeletable[M]
    with AggregateMultiSoftDeletable[M] {
  override type IdType        = UserAccountId
  override type AggregateType = UserAccount
}

object UserAccountRepository {

  type OnRedis[A]     = ReaderT[Task, RedisConnection, A]
  type OnMemcached[A] = ReaderT[Task, MemcachedConnection, A]
  type OnMemory[A]    = Task[A]
  type BySlick[A]     = Task[A]
  type BySkinny[A]    = ReaderT[Task, DBSession, A]
  type ByFree[A]      = Free[UserRepositoryDSL, A]

  def bySlick(profile: JdbcProfile, db: JdbcProfile#Backend#Database): UserAccountRepository[BySlick] =
    new UserAccountRepositoryBySlick(profile, db)

  def bySkinny: UserAccountRepository[BySkinny] = new UserAccountRepositoryBySkinny

  def onRedis(
      expireDuration: Duration
  )(implicit actorSystem: ActorSystem): UserAccountRepository[OnRedis] =
    new UserAccountRepositoryOnRedis(expireDuration)

  def onMemcached(
      expireDuration: Duration
  )(implicit actorSystem: ActorSystem): UserAccountRepository[OnMemcached] =
    new UserAccountRepositoryOnMemcached(expireDuration)

  def onMemory(minSize: Option[Int] = None,
               maxSize: Option[Int] = None,
               expireDuration: Option[Duration] = None,
               concurrencyLevel: Option[Int] = None,
               maxWeight: Option[Int] = None): UserAccountRepository[OnMemory] =
    new UserAccountRepositoryOnMemory(minSize, maxSize, expireDuration, concurrencyLevel, maxWeight)
    
}

Usage

  • for Slick3
val userAccountRepository: UserAccountRepository[BySlick] = UserAccountRepository.bySlick(dbConfig.profile, dbConfig.db)
val resultTask: Task[UserAccount] = for {
  _ <- userAccountRepository.store(userAccount)
  result <- userAccountRepository.resolveBy(userAccount.id)
} yield result

val resultFuture: Future[UserAccount] = resultTask.runAsync
  • for SkinnyORM
val userAccountRepository: UserAccountRepository[BySkinny] = UserAccountRepository.bySkinny
val resultTask: Task[UserAccount] = for {
  _ <- userAccountRepository.store(userAccount)
  result <- userAccountRepository.resolveBy(userAccount.id)
} yield result

val resultFuture: Future[UserAccount] = resultTask.run(AutoSession).runAsync
  • for Memcached
val userAccountRepository: UserAccountRepository[OnMemcached] = UserAccountRepository.onMemcached(expireDuration = 5 minutes)
val resultFuture: Future[UserAccount] = connectionPool
  .withConnectionF { con =>
    (for {
      _ <- userAccountRepository.store(userAccount)
      r <- userAccountRepository.resolveById(userAccount.id)
    } yield r).run(con)
  }
  .runAsync
  • for Redis
val userAccountRepository: UserAccountRepository[OnRedis] = UserAccountRepository.onRedis(expireDuration = 5 minutes)
val resultFuture: Future[UserAccount] = connectionPool
  .withConnectionF { con =>
    (for {
      _ <- userAccountRepository.store(userAccount)
      r <- userAccountRepository.resolveById(userAccount.id)
    } yield r).run(con)
  }
  .runAsync
  • for Memory(Guava Cache)
val userAccountRepository: UserAccountRepository[nMemory] = UserAccountRepository.onMemory(expireAfterWrite = Some(5 minutes))
val resultFuture: Future[UserAccount] = (for {
  _ <- repository.store(userAccount)
  r <- repository.resolveById(userAccount.id)
} yield r).runAsync
  • for Free
val free: UserAccountRepository[ByFree] = UserAccountRepository[ByFree]
val program: Free[UserRepositoryDSL, UserAccount] = for {
  _      <- free.store(userAccount)
  result <- free.resolveById(userAccount.id)
} yield result

val slick = UserAccountRepository.bySlick(dbConfig.profile, dbConfig.db)
val resultTask: Task[UserAccount] = UserAccountRepositoryOnFree.evaluate(slick)(program)
val resultFuture: Future[UserAccount] = evalResult.runAsync

// if evaluation by skinny 
// val skinny     = UserAccountRepository.bySkinny
// val resultTask: Task[UserAccount] = UserAccountRepositoryOnFree.evaluate(skinny)(program)
// val resultFuture: Future[UserAccount] = evalResult.run(AutoSession).runAsync