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 ...
随机推荐
- python 爬图
利用bs库进行爬取,在下载html时,使用代理user_agent来下载,并且下载次数是2次,当第一次下载失败后,并且http状态码是500-600之间,然后会重新下载一次 soup = Beauti ...
- cfg 4 ocl
http://blog.sina.com.cn/s/blog_6c868c470102v15y.html rnnlib真难装 http://sourceforge.net/p/rnnl/wiki/Ho ...
- 再读《Parallel Programming with Python》并作笔记
并发编程,在哪个语言里都属于高端应用,一定得会了才好意思说懂了这门语言. 在工作中用得并不是很多,忘了一些内容,就慢慢看,慢慢补上. 今天一天看了近三分之一(我看外文越来越快了??:)), 实践一下多 ...
- Loadrunner中cookie解释与用法
loadrunner对于cookie的处理loadrunner中与cookie处理相关的常用函数如下: web_add_cookie():添加新的cookie或者修改已经存在的cookie web_r ...
- visible, disable, css绑定
这个例子非常简单,主要演示如何通过属性控制html元素的显示与否(visible),可用性(disable)以及根据属性添加相应的CSS样式. 先简单的看一段代码: <p> <inp ...
- mongoDB学习第二天之常用方法
mongoDB LIMIT 和 SKIP 方法 db.colName.find().limit(num) # limit 方法接收一个数字参数,该参数指定读取的记录条数 (db.colName.fi ...
- Codeforces 722C(并查集 + 思维)
本文链接:http://www.cnblogs.com/Ash-ly/p/5932712.html 题目链接:http://codeforces.com/problemset/problem/722/ ...
- 【面试总结-编程】多行两列数据,实现同key的value求和并输出
一个文件,两列,多行. 第一列是字母,第二列是数字,同列数据之间通过空格分割. 统计首列字母相同的第二列之和. 样例输入: A 5 B 6 OO 7 A 6 A 2 OO 2 输出: A:13 B:6 ...
- AtomicIntegerFieldUpdater用法
一个基于反射的工具类,它能对指定类的指定的volatile字段进行原子更新 下面是netty源码中AbstractReferenceCountedByteBuf类的使用 private static ...
- 深入理解javascript函数系列第一篇
前面的话 函数对任何一门语言来说都是核心的概念.通过函数可以封装任意多条语句,而且可以在任何地方.任何时候调用执行.在javascript里,函数即对象,程序可以随意操控它们.函数可以嵌套在其他函数中 ...