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 ...
随机推荐
- [重磅]Deep Forest,非神经网络的深度模型,周志华老师最新之作,三十分钟理解!
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 深度学习最大的贡献,个人认为就是表征 ...
- JavaScript 兼容性总结
请实现鼠标点击任意标签,alert该标签的名称(注意兼容性) function elementName(evt){ evt = evt|| window.event; var selected = ...
- day5时间复杂度
时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花 ...
- Three.js基础探寻八——法向材质与材质的纹理贴图
4.法向材质 法向材质可以将材质的颜色设置为其法向量的方向,有时候对于调试很有帮助. 法向材质的设定很简单,甚至不用设置任何参数: new THREE.MeshNormalMaterial() 材质的 ...
- chrome浏览器插件开发经验(一)
最近在进行chrome浏览器插件的开发,一些小的经验总结随笔. 1.首先,推荐360的chrome插件开发文档:http://open.chrome.360.cn/extension_dev/over ...
- Visual Studio2017如何设置自动生成的代码不换行
- 使用STL sort对字符串按字典序排序
使用string数组 #include<iostream> #include<string> #include<algorithm> using namespace ...
- bzoj 1853 容斥 + 搜索
思路:先把所有幸运数字找出来, 把没有用的去掉,然后爆搜容斥,因为最多只会搜十几个就超过限制了, 所以是可行的. #include<bits/stdc++.h> #define LL lo ...
- SaltStack--Job管理 (八)
官方文档:https://docs.saltstack.com/en/2016.11/ref/modules/all/salt.modules.saltutil.html [root@7mini-no ...
- 修改MySQL事件
MySQL允许您更改现有事件的各种属性. 要更改现有事件,请使用ALTER EVENT语句,如下所示: ALTER EVENT event_name ON SCHEDULE schedule ON C ...