-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
229 additions
and
45 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
.idea | ||
.idea_modules | ||
demo/.idea | ||
demo/.idea_modules | ||
target | ||
project/boot | ||
*.swp | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=0.13.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sbt._ | ||
import sbt.Keys._ | ||
|
||
object build extends Build { | ||
|
||
lazy val demo = Project("demo", file("."), settings = buildSettings) | ||
|
||
val buildSettings = Defaults.defaultSettings ++ Seq( | ||
organization := "com.yourcompany", | ||
version := "0.4.1", | ||
scalaVersion := "2.10.3", | ||
scalacOptions := Seq("-unchecked", "-deprecation"), | ||
resolvers ++= Seq(Resolver.sonatypeRepo("releases")), //, Resolver.sonatypeRepo("snapshots")), | ||
libraryDependencies ++= Seq( | ||
"com.marcgrue" %% "scaladci" % "0.4.1" | ||
), | ||
addCompilerPlugin("org.scalamacros" % "paradise" % "2.0.0-M2" cross CrossVersion.full) | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import scaladci._ | ||
|
||
/* | ||
Simple example of DCI in action - the money transfer example | ||
1. Import scaladci._ | ||
2. Annotate a class with @context - that DCI-enables it. | ||
3. Define roles names (like Actors in a use case) | ||
4. Define role methods (what will those actors do...) | ||
5. Define the trigger that starts off the chain of interactions between the roles | ||
Normally we lowercase parameter names. "Source" and "Destination" of the MoneyTransfer | ||
DCI context are an exception since we want to associate them with our role definitions. | ||
A role definition has to match an object identifier in the context scope. Not necessarily | ||
a class parameter, any val/var will do as long as the identifier name matches a defined role. | ||
Compared to this school-book example, Data classes, DCI Contexts and the runtime | ||
environment would of course normally be in separate tiers. | ||
More info - http://github.com/dci/scaladci | ||
Official website - http://fulloo.info | ||
Discussions - https://groups.google.com/forum/#!forum/object-composition | ||
*/ | ||
|
||
object MoneyTransferApp extends App { | ||
|
||
// Data - knows nothing of transfers... | ||
case class Account(name: String, var balance: Int) { | ||
def increaseBalance(amount: Int) { balance += amount } | ||
def decreaseBalance(amount: Int) { balance -= amount } | ||
} | ||
|
||
// DCI Context - encapsulates a specific "process"/"use case"/"network of interactions" etc | ||
@context | ||
class MoneyTransfer(Source: Account, Destination: Account, amount: Int) { | ||
|
||
Source.withdraw // Trigger method setting off the use case | ||
|
||
role Source { // Role definition | ||
def withdraw() { // Role method | ||
Source.decreaseBalance(amount) // Instance method | ||
Destination.deposit // Role interacts with other role | ||
} | ||
} | ||
|
||
role Destination { | ||
def deposit() { | ||
Destination.increaseBalance(amount) | ||
} | ||
} | ||
} | ||
|
||
|
||
// Runtime environment | ||
|
||
// Instantiate Data objects | ||
val salary = Account("Salary", 3000) | ||
val budget = Account("Budget", 1000) | ||
|
||
// Instantiate Context with data objects and run use case | ||
new MoneyTransfer(salary, budget, 700) | ||
|
||
// Confirm that amount has transferred | ||
assert(salary.balance == 3000 - 700, s"Salary balance should have been 3000 - 700 = 2300. Is now ${salary.balance}") | ||
assert(budget.balance == 1000 + 700, s"Budget balance should have been 1000 + 700 = 1700. Is now ${budget.balance}") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import sbt._ | ||
import sbt.Keys._ | ||
|
||
object build extends Build with BuildSettings { | ||
|
||
lazy val root = Project( | ||
"root", | ||
file("."), | ||
settings = buildSettings ++ Seq(packagedArtifacts := Map.empty) | ||
) aggregate(core, examples) | ||
|
||
lazy val core = Project( | ||
"scaladci", | ||
file("core"), | ||
settings = buildSettings | ||
) | ||
|
||
lazy val examples = Project( | ||
"scaladci-examples", | ||
file("examples"), | ||
settings = buildSettings ++ Seq( | ||
libraryDependencies ++= Seq( | ||
"org.jscala" %% "jscala-macros" % "0.3", | ||
"org.jscala" %% "jscala-annots" % "0.3" | ||
), | ||
packagedArtifacts := Map.empty | ||
) | ||
) dependsOn core | ||
} | ||
|
||
trait BuildSettings extends Publishing { | ||
|
||
lazy val majorVersion = "0.4.1" | ||
// lazy val versionFormat = "%s-SNAPSHOT" | ||
lazy val versionFormat = "%s" | ||
|
||
val buildSettings = Defaults.defaultSettings ++ publishSettings ++ Seq( | ||
organization := "com.marcgrue", | ||
version := versionFormat format majorVersion, | ||
scalaVersion := "2.10.3", | ||
scalacOptions := Seq("-unchecked", "-deprecation", "-feature"), | ||
resolvers ++= Seq(Resolver.sonatypeRepo("releases"), Resolver.sonatypeRepo("snapshots")), | ||
libraryDependencies ++= Seq( | ||
"org.scala-lang" % "scala-reflect" % scalaVersion.value, | ||
"org.specs2" %% "specs2" % "2.3.7" % "test" | ||
), | ||
addCompilerPlugin("org.scalamacros" % "paradise" % "2.0.0-M2" cross CrossVersion.full) | ||
) | ||
} | ||
|
||
trait Publishing { | ||
val snapshots = "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/" | ||
val releases = "Sonatype OSS Staging" at "https://oss.sonatype.org/service/local/staging/deploy/maven2/" | ||
|
||
lazy val publishSettings = Seq( | ||
publishMavenStyle := true, | ||
publishTo <<= version((v: String) => Some(if (v.trim endsWith "SNAPSHOT") snapshots else releases)), | ||
publishArtifact in Test := false, | ||
pomIncludeRepository := (_ => false), | ||
pomExtra := projectPomExtra | ||
) | ||
|
||
val projectPomExtra = | ||
<url>https://github.com/dci/scaladci</url> | ||
<licenses> | ||
<license> | ||
<name>Apache License</name> | ||
<url>http://www.apache.org/licenses/</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
<scm> | ||
<url>git@github.com:dci/scaladci.git</url> | ||
<connection>scm:git:git@github.com:dci/scaladci.git</connection> | ||
</scm> | ||
<developers> | ||
<developer> | ||
<id>marcgrue</id> | ||
<name>Marc Grue</name> | ||
<url>http://marcgrue.com</url> | ||
</developer> | ||
</developers> | ||
} |