learning scala dependency injection
println("Step 1: Create a trait which knows how to do create, read, update and delete operations CRUD to a given database")
trait DonutDatabase[A] {
def addOrUpdate(donut: A): Long
def query(donut: A): A
def delete(donut: A): Boolean
}
println("\nStep 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer")
class CassandraDonutStore[A] extends DonutDatabase[A] {
override def addOrUpdate(donut: A): Long = {
println(s"CassandraDonutDatabase-> addOrUpdate method -> donut: $donut")
}
override def query(donut: A): A = {
println(s"CassandraDonutDatabase-> query method -> donut: $donut")
donut
}
override def delete(donut: A): Boolean = {
println(s"CassandraDonutDatabase-> delete method -> donut: $donut")
true
}
}
println("\nStep 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase")
trait DonutShoppingCartDao[A] {
val donutDatabase: DonutDatabase[A] // dependency injection
def add(donut: A): Long = {
println(s"DonutShoppingCartDao-> add method -> donut: $donut")
donutDatabase.addOrUpdate(donut)
}
def update(donut: A): Boolean = {
println(s"DonutShoppingCartDao-> update method -> donut: $donut")
donutDatabase.addOrUpdate(donut)
true
}
def search(donut: A): A = {
println(s"DonutShoppingCartDao-> search method -> donut: $donut")
donutDatabase.query(donut)
}
def delete(donut: A): Boolean = {
println(s"DonutShoppingCartDao-> delete method -> donut: $donut")
donutDatabase.delete(donut)
}
}
println("\nStep 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase")
trait DonutInventoryService[A] {
val donutDatabase: DonutDatabase[A] // dependency injection
def checkStockQuantity(donut: A): Int = {
println(s"DonutInventoryService-> checkStockQuantity method -> donut: $donut")
donutDatabase.query(donut)
}
}
println("\nStep 5: Create a trait which will act as a facade which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService. It also inject the correct DonutDatabase implementation - a CassandraDonutStore")
trait DonutShoppingCartServices[A] extends DonutShoppingCartDao[A] with DonutInventoryService[A] {
override val donutDatabase: DonutDatabase[A] = new CassandraDonutStore[A]()
}
println("\nStep 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart")
class DonutShoppingCart[A] extends DonutShoppingCartServices[A] {
}
println("\nStep 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods")
val donutShoppingCart: DonutShoppingCart[String] = new DonutShoppingCart[String]()
donutShoppingCart.add("Vanilla Donut")
donutShoppingCart.update("Vanilla Donut")
donutShoppingCart.search("Vanilla Donut")
donutShoppingCart.delete("Vanilla Donut")
println("\nStep 8: Call the checkStockQuantity method")
donutShoppingCart.checkStockQuantity("Vanilla Donut")
result:
Step 1: Create a trait which knows how to do create, read, update and delete operations CRUD to a given database Step 2: Create a class which extends trait DonutDatabase and knows how to perform CRUD operations with Apache Cassandra as storage layer Step 3: Create a trait which will define the methods for a data access layer and will require dependency injection for DonutDatabase Step 4: Create a trait which will define the methods for checking donut inventory and will require dependency injection for DonutDatabase Step 5: Create a trait which will act as a facade which extends multiple traits namely trait DonutShoppingCartDao and trait DonutInventoryService. It also inject the correct DonutDatabase implementation - a CassandraDonutStore Step 6: Create a DonutShoppingCart class which extends a single facade named DonutShoppingCartServices to expose all the underlying features required by a DonutShoppingCart Step 7: Create an instance of DonutShoppingCart and call the add, update, search and delete methods
DonutShoppingCartDao-> add method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
DonutShoppingCartDao-> update method -> donut: Vanilla Donut
CassandraDonutDatabase-> addOrUpdate method -> donut: Vanilla Donut
DonutShoppingCartDao-> search method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
DonutShoppingCartDao-> delete method -> donut: Vanilla Donut
CassandraDonutDatabase-> delete method -> donut: Vanilla Donut Step 8: Call the checkStockQuantity method
DonutInventoryService-> checkStockQuantity method -> donut: Vanilla Donut
CassandraDonutDatabase-> query method -> donut: Vanilla Donut
learning scala dependency injection的更多相关文章
- Scala 深入浅出实战经典 第57讲:Scala中Dependency Injection实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- [LINK]List of .NET Dependency Injection Containers (IOC)
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx I'm trying to expand my ...
- Ninject学习(一) - Dependency Injection By Hand
大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
- 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)
控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...
- [转载][翻译] IoC 容器和 Dependency Injection 模式
原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...
- Dependency Injection
Inversion of Control - Dependency Injection - Dependency Lookup loose coupling/maintainability/ late ...
- Inversion of Control Containers and the Dependency Injection pattern(转)
In the Java community there's been a rush of lightweight containers that help to assemble components ...
- 【译】Dependency Injection with Autofac
先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...
随机推荐
- SSM整合所需的maven配置文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 立体像对空间前方交会-点投影系数法(python实现)
一.原理 二.步骤 a.用各自像片的角元素计算出左右像片的旋转矩阵R1和R2. b.根据左右像片的外方位元素计算摄影基线分量Bx,By,Bz. c.逐点计算像点的空间辅助坐标. d.计算投影系数. e ...
- oracle-3-Linux-11g安装-静默安装
oracle下载地址:https://www.oracle.com/database/technologies/112010-linx8664soft.html 系统是最小化安装的Centos7.2 ...
- Kettle部署笔记
1.启动脚本(启动job) /u02/www/data-integration/kitchen.sh -file:/u02/www/data-integration/job.kjb -logfile= ...
- 正整数序列 Help the needed for Dexter ,UVa 11384
题目描述 Description 给定正整数n,你的任务是用最少的操作次数把序列1, 2, …, n中的所有数都变成0.每次操作可从序列中选择一个或多个整数,同时减去一个相同的正整数.比如,1,2,3 ...
- WPF打印控件内容
当我们想打印控件内容时,如一个Grid中的内容,可以用WPF中PrintDialog类的PrintVisual()方法来实现 界面如下: XAML代码如下 <Grid> <Grid. ...
- AdventureWorks 安装和配置[转自 微软msdn]
AdventureWorks 安装和配置 2018/06/19 适用对象:SQL ServerAzure SQL 数据库Azure SQL 数据仓库并行数据仓库 AdventureWorks 下载链接 ...
- .NET Core 使用swagger进行分组显示
其实,和swagger版本管理类似;只是平时接口太多;不好供前端人员进行筛选. 下面进入主题: 首先: //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddSwa ...
- 关于Vue-elementUI中,给input手动赋值之后无法修改的问题解决
方案一:在data中给input的值赋一个初始值 方案二:在给input赋值时,使用this.$set
- bootstrap 模态窗口 多重/多个弹窗滚动条补丁
由于bootstrap的模态窗口默认不支持多次弹出, 在关闭的时候会有滚动条消失的问题. 经过观察和查看源码, 发现在开启和关闭的时候会在body上增加/减少一个"modal-open&qu ...