From 3509d66e0298a807d431d284d62a1ca6e50ee46d Mon Sep 17 00:00:00 2001 From: Kazuhiro Funakoshi Date: Wed, 1 Jan 2025 14:21:57 -0800 Subject: [PATCH] =?UTF-8?q?Update=20refactored=20sample=20code=20for=20?= =?UTF-8?q?=E4=BE=9D=E5=AD=98=E6=80=A7=E3=81=AE=E6=B3=A8=E5=85=A5=E3=81=AB?= =?UTF-8?q?=E3=82=88=E3=82=8B=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF?= =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 説明では DI の実行後, UserServiceImpl から UserRepositoryImpl への依存性がなくなっ ているとのことであったが, サンプルでは, - UserServiceImpl の実装を UserService に移植し, - UserService に UserRepository への依存性を注入し, - UserServiceImpl はなおも UserRepositoryImpl に依存する となっていた. これに対し,説明に合わせるための修正を実施した. --- .../src/main/scala/domain/UserService.scala | 10 +++++++--- .../src/test/scala/domain/UserServiceSpec.scala | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/example_projects/trait-refactored-example/src/main/scala/domain/UserService.scala b/src/example_projects/trait-refactored-example/src/main/scala/domain/UserService.scala index 290f19a95..aa850b217 100644 --- a/src/example_projects/trait-refactored-example/src/main/scala/domain/UserService.scala +++ b/src/example_projects/trait-refactored-example/src/main/scala/domain/UserService.scala @@ -1,10 +1,16 @@ package domain trait UserService { - self: PasswordService with UserRepository => val maxNameLength = 32 + def register(name: String, rawPassword: String): User + + def login(name: String, rawPassword: String): User + +} + +class UserServiceImpl extends UserService { self: UserRepository with PasswordService => def register(name: String, rawPassword: String): User = { if (name.length > maxNameLength) { throw new Exception("Too long name!") @@ -26,5 +32,3 @@ trait UserService { } } } - -class UserServiceImpl extends UserService with PasswordServiceImpl with UserRepositoryImpl diff --git a/src/example_projects/trait-refactored-example/src/test/scala/domain/UserServiceSpec.scala b/src/example_projects/trait-refactored-example/src/test/scala/domain/UserServiceSpec.scala index 7d5521997..0d8d30f7c 100644 --- a/src/example_projects/trait-refactored-example/src/test/scala/domain/UserServiceSpec.scala +++ b/src/example_projects/trait-refactored-example/src/test/scala/domain/UserServiceSpec.scala @@ -6,7 +6,7 @@ import org.scalatest.wordspec.AnyWordSpec class UserServiceSpec extends AnyWordSpec { val user = User(1, "test_name", "test_password") - val sut = new UserService with PasswordServiceImpl with UserRepository { + val sut = new UserServiceImpl with PasswordServiceImpl with UserRepository { def find(id: Long): Option[domain.User] = Some(user) def find(name: String): Option[domain.User] = Some(user) def insert(user: domain.User): domain.User = user