DCI:DCI学习总结
备注
之前把DCI的Role和四色原型的Role给弄混了,本文也不会比较这两种Role的区别(后面有机会再说),这里简单的记录一下对DCI的理解。
参考文章:http://www.cnblogs.com/happyframework/p/3302238.html。
什么是DCI?
Context 选择 Data,让 Data 扮演 Role 执行 Interaction。

Data
用户模型(只包含数据和本地方法)。
如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V7
{
public partial class People
{
public string Name { get; set; } public TRole Act<TRole>()
where TRole : class
{
return this as TRole;
}
}
}
Context
面向用例设设计,职责为:选择对象,让对象扮演角色,让角色执行交互。
如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V7.Company
{
public class CompanyContext
{
public void Execute()
{
//选择对象。
var steven = new People { Name = "Steven" }; //扮演角色。
var developer = steven.Act<IDeveloper>(); //执行交互。
developer.Coding();
}
}
}
Interaction
角色的行为驱动用例的执行。
如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using DCIStudy.V7.Company; namespace DCIStudy.V7
{
public partial class People : IDeveloper
{
void IDeveloper.Coding()
{
Console.WriteLine(string.Format("{0},快乐的编程中!",this.Name));
}
}
}
如何将Role注入到Data中?
开发期注入
字节码增强
下文的语法是AspectJ吗?我没有验证,有懂的朋友给我留言,我感觉字节码增强是可以实现的。

Mixin
http://www.cnblogs.com/happyframework/archive/2013/04/25/3040461.html。
Trait
Trait本质上是一种Mixin的实现,Scala和Php5.4在语法级别支持了trait。
http://php.net/manual/en/language.oop5.traits.php。
Template
http://www.cnblogs.com/stephen-liu74/archive/2012/08/12/2635583.html。
T4 + 部分类 + 显式接口实现 + 扩展类型,C#专用
后面会给出示例,因为T4 + 扩展类型都是为了复用的,后文只给出显示接口实现 + 部分类的代码,如果有复用需求,可以引入T4 + 扩展类型。
运行期注入
Mixin
Mixin也分开发期间Mixin和运行期间Mixin。
凡是支持OpenClass的语言都支持运行期间Mixin,如:Ruby、Python和Javascript。OpenClass的本质是运行期间可以修改类型系统,也叫“动态类型”,像Php这种静态类型语言就没有这个特性,虽然Php是弱类型和解释执行的。
http://www.cnblogs.com/happyframework/archive/2013/04/25/3040461.html(重点看Ruby)。
动态代理
http://www.cnblogs.com/happyframework/p/3295853.html
为什么要用DCI?
如果将DCI作为一种编程模式或设计模式的话,我是比较认可的,作为一种架构模式,还有待考量,等有机会用一下再做评价。
DCI在C#种的两种实现
第一种:显式接口实现 + 部分类
项目结构

代码(给出一个上下文的代码)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V7.Home
{
public class HomeContext
{
public void Execute()
{
//选择对象。
var steven = new People { Name = "Steven" }; //扮演角色。
var player = steven.Act<IPlayer>(); //执行交互。
player.Play();
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V7.Home
{
public interface IPlayer
{
void Play();
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using DCIStudy.V7.Home; namespace DCIStudy.V7
{
public partial class People : IPlayer
{
void IPlayer.Play()
{
Console.WriteLine(string.Format("{0},疯狂的游戏中!",this.Name));
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V7
{
public partial class People
{
public string Name { get; set; } public TRole Act<TRole>()
where TRole : class
{
return this as TRole;
}
}
}
第二种实现:组合
项目结构

代码(给出一个上下文的代码)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V8.Company
{
public class CompanyContext
{
public void Execute()
{
//选择对象。
var steven = new People { Name = "Steven" }; //扮演角色。
var developer = steven.Act<Developer>(); //执行交互。
developer.Coding();
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V8.Company
{
public class CompanyContext
{
public void Execute()
{
//选择对象。
var steven = new People { Name = "Steven" }; //扮演角色。
var developer = steven.Act<Developer>(); //执行交互。
developer.Coding();
}
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCIStudy.V8
{
public class People
{
public string Name { get; set; } public TRole Act<TRole>()
where TRole : class
{
var role = Activator.CreateInstance<TRole>(); (role as dynamic).People = this; return role;
}
}
}
备注
相对于DDD,DCI给出的模式显得过于泛化了,如:分层、分区(BondedContext)、每个层有哪些元素、如何交互等,DCI、四色原型和DDD应该可以以某种形式融合,有待慢慢思考。
DCI:DCI学习总结的更多相关文章
- DCI学习链接及文章
https://www.jianshu.com/u/c1b1137d5886 李永顺 https://www.jianshu.com/users/7386692d5489/timeline 袁英杰 小 ...
- arcgis api for js入门开发系列十六迁徙流动图
最近公司有个arcgis api for js的项目,需要用到百度echarts迁徙图效果,而百度那个效果实现是结合百度地图的,怎么才能跟arcgis api结合呢,网上搜索,终于在github找到了 ...
- arcgis api 3.x for js 入门开发系列二十二地图模态层(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- arcgis api 3.x for js 入门开发系列十六迁徙流动图
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- DataGridView 设置某个列为只能为数字
public DataGridViewTextBoxEditingControl CellEdit = null; int idx = 1; private void dataGridView1_Ed ...
- 自研DCI网络路由交换协议DCIP-白牌交换机时代的企业网络
一转眼从听华为3Com的路由交换课程到如今已经13年有余了,依稀记得第一节课的时候我带着老婆去听的课(老婆是日语系的.那时还是女朋友,并不懂网络,仅仅是跟着我去上课的).抢了个头排,讲师宋岩老师提问了 ...
- DCI架构是如何解决DDD战术建模缺点的?
摘要:将DCI架构总结成一句话就是:领域对象(Object)在不同的场景(Context)中扮演(Cast)不同的角色(Role),角色之间通过交互(Interactive)来完成具体的业务逻辑. 本 ...
- DDD为何叫好不叫座?兼论DCI与业务分析的方法论
今天,仔细阅读了园子里面的一个朋友写的<一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?>(http://www.cnblogs.com/ ...
- DCI架构
提出的文章:DCI架构:一个面向对象编程的新图景 http://wenku.baidu.com/view/a7b5e401de80d4d8d15a4fed.html http://www.360doc ...
随机推荐
- JavaScript 兼容性总结
请实现鼠标点击任意标签,alert该标签的名称(注意兼容性) function elementName(evt){ evt = evt|| window.event; var selected = ...
- day4迭代器&生成器&正则表达式
一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不能后退,不过这也没什么,因为人们很少在迭代途中后退.另外,迭代器的一大优点 ...
- ref:Struts2 命令执行系列回顾
ref:http://www.zerokeeper.com/vul-analysis/struts2-command-execution-series-review.html Struts2 命令执行 ...
- STL容器 -- Set
核心: set 是一个数学含义上的集合-----保证了每个数的确定性, 互异性, 不仅如此, set 中的元素还是有序的. 头文件: #include <set> 拓展:由于 set 内的 ...
- 2017 计蒜之道 初赛 第五场 D. UCloud 的安全秘钥(困难)
小数据打表,大数据暴力. 导致超时的主要原因是$m$小的询问次数太多,可以把$m≤10$的答案直接暴力打表存起来,$m>10$的用$C$题的方法即可. #include <iostream ...
- 洛谷P2323 [HNOI2006] 公路修建问题 [二分答案,生成树]
题目传送门 公路修建问题 题目描述 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Associa ...
- GDB 调试工具高级用法
解决core核心文件转出问题 ulimit -c #查看core文件的生成开关,若为0则关闭 ulimit -c unlimited #打开开关,只在当前shell生效 sudo sh -c 'ech ...
- Xcode代码提示里的字母含义
P -- 协议 M -- 成员方法 C -- 类 K -- 枚举 .常量 V -- 成员变量 T -- typedef类型 G -- 全局变量 f -- 函数 # -- #define指令
- SpringBoot学习(六)
1.pom 文件 <?xml version="1.0" encoding="utf-8"?> <dependencies> <d ...
- MySQL笔记(三)之数据插入更新与删除
INSERT INTO INSERT INTO 语句用于向表格中插入新的行. 语法: INSERT INTO 表 VALUES (值1, 值2,....) # 列数必须和值的个数匹配 INSERT I ...