Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

As simple as possible is good; and a collection inside an object is indeed simple. However you should also avoid making your objects dependent on static object. That will create a rigid structure and will make testing more difficult.

I would take a look at the cake-pattern (see How do you do dependency injection with the Cake pattern without hardcoding?How do you do dependency injection with the Cake pattern without hardcoding?). This way you can have the CarManager in an object and mix it into your dependencies through a CarRepositoryComponent.

Non-compiled example:

val someService = new SomeService with MyRepairServiceComponent with CarCollectionRepositoryComponent someService.repairCar(8) trait SomeService extends RepairServiceComponent with CarRepositoryComponent { def repairCar(id: Int): Status = repairService.repair(carRepository.get(id)); } trait CarRepository { abstract def get(id: Int): Car } trait CarRepositoryComponent { abstract def carRepository: CarRepository } object CarManager extends CarRepository { private var cars = Map[Int, Car]() def get(id: Int): Car = cars(id) } trait CarCollectionRepositoryComponent { def carRepository = CarManager } 

As simple as possible is good; and a collection inside an object is indeed simple. However you should also avoid making your objects dependent on static object. That will create a rigid structure and will make testing more difficult.

I would take a look at the cake-pattern (see How do you do dependency injection with the Cake pattern without hardcoding?). This way you can have the CarManager in an object and mix it into your dependencies through a CarRepositoryComponent.

Non-compiled example:

val someService = new SomeService with MyRepairServiceComponent with CarCollectionRepositoryComponent someService.repairCar(8) trait SomeService extends RepairServiceComponent with CarRepositoryComponent { def repairCar(id: Int): Status = repairService.repair(carRepository.get(id)); } trait CarRepository { abstract def get(id: Int): Car } trait CarRepositoryComponent { abstract def carRepository: CarRepository } object CarManager extends CarRepository { private var cars = Map[Int, Car]() def get(id: Int): Car = cars(id) } trait CarCollectionRepositoryComponent { def carRepository = CarManager } 

As simple as possible is good; and a collection inside an object is indeed simple. However you should also avoid making your objects dependent on static object. That will create a rigid structure and will make testing more difficult.

I would take a look at the cake-pattern (see How do you do dependency injection with the Cake pattern without hardcoding?). This way you can have the CarManager in an object and mix it into your dependencies through a CarRepositoryComponent.

Non-compiled example:

val someService = new SomeService with MyRepairServiceComponent with CarCollectionRepositoryComponent someService.repairCar(8) trait SomeService extends RepairServiceComponent with CarRepositoryComponent { def repairCar(id: Int): Status = repairService.repair(carRepository.get(id)); } trait CarRepository { abstract def get(id: Int): Car } trait CarRepositoryComponent { abstract def carRepository: CarRepository } object CarManager extends CarRepository { private var cars = Map[Int, Car]() def get(id: Int): Car = cars(id) } trait CarCollectionRepositoryComponent { def carRepository = CarManager } 
Source Link
thoredge
  • 12.7k
  • 1
  • 48
  • 64

As simple as possible is good; and a collection inside an object is indeed simple. However you should also avoid making your objects dependent on static object. That will create a rigid structure and will make testing more difficult.

I would take a look at the cake-pattern (see How do you do dependency injection with the Cake pattern without hardcoding?). This way you can have the CarManager in an object and mix it into your dependencies through a CarRepositoryComponent.

Non-compiled example:

val someService = new SomeService with MyRepairServiceComponent with CarCollectionRepositoryComponent someService.repairCar(8) trait SomeService extends RepairServiceComponent with CarRepositoryComponent { def repairCar(id: Int): Status = repairService.repair(carRepository.get(id)); } trait CarRepository { abstract def get(id: Int): Car } trait CarRepositoryComponent { abstract def carRepository: CarRepository } object CarManager extends CarRepository { private var cars = Map[Int, Car]() def get(id: Int): Car = cars(id) } trait CarCollectionRepositoryComponent { def carRepository = CarManager }