备注

之前把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

http://qi4j.org/

为什么要用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学习总结的更多相关文章

  1. DCI学习链接及文章

    https://www.jianshu.com/u/c1b1137d5886 李永顺 https://www.jianshu.com/users/7386692d5489/timeline 袁英杰 小 ...

  2. arcgis api for js入门开发系列十六迁徙流动图

    最近公司有个arcgis api for js的项目,需要用到百度echarts迁徙图效果,而百度那个效果实现是结合百度地图的,怎么才能跟arcgis api结合呢,网上搜索,终于在github找到了 ...

  3. arcgis api 3.x for js 入门开发系列二十二地图模态层(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  4. arcgis api 3.x for js 入门开发系列十六迁徙流动图

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  5. DataGridView 设置某个列为只能为数字

    public DataGridViewTextBoxEditingControl CellEdit = null; int idx = 1; private void dataGridView1_Ed ...

  6. 自研DCI网络路由交换协议DCIP-白牌交换机时代的企业网络

    一转眼从听华为3Com的路由交换课程到如今已经13年有余了,依稀记得第一节课的时候我带着老婆去听的课(老婆是日语系的.那时还是女朋友,并不懂网络,仅仅是跟着我去上课的).抢了个头排,讲师宋岩老师提问了 ...

  7. DCI架构是如何解决DDD战术建模缺点的?

    摘要:将DCI架构总结成一句话就是:领域对象(Object)在不同的场景(Context)中扮演(Cast)不同的角色(Role),角色之间通过交互(Interactive)来完成具体的业务逻辑. 本 ...

  8. DDD为何叫好不叫座?兼论DCI与业务分析的方法论

    今天,仔细阅读了园子里面的一个朋友写的<一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?>(http://www.cnblogs.com/ ...

  9. DCI架构

    提出的文章:DCI架构:一个面向对象编程的新图景 http://wenku.baidu.com/view/a7b5e401de80d4d8d15a4fed.html http://www.360doc ...

随机推荐

  1. Linux命令之dig命令挖出DNS的秘密

    === [初次见面] 我相信使用nslookup的同学一定比使用dig的同学多,所以还是有必要花些时间给大家介绍一下dig的. dig,和nslookup作用有些类似,都是DNS查询工具. dig,其 ...

  2. centos修改oracle字符集

    1.首先以sysdba的身份登录上去 conn /as sysdba2.关闭数据库shutdown immediate;3.以mount打来数据库,startup mount4.设置session S ...

  3. LoadRunner 使用虚拟IP测试流程

    LoadRunner 使用虚拟IP测试流程 LoadRunner 使用IP欺骗的原因 . 当某个IP的访问过于频繁,或者访问量过大是,服务器会拒绝访问请求,这时候通过IP欺骗可以增加访问频率和访问量, ...

  4. Vsftpd支持SSL加密传输

    ftp传输数据是明文,弄个抓包软件就可以通过数据包来分析到账号和密码,为了搭建一个安全性比较高ftp,可以结合SSL来解决问题   SSL(Secure Socket Layer)工作于传输层和应用程 ...

  5. python3 爬虫教学之爬取链家二手房(最下面源码) //以更新源码

    前言 作为一只小白,刚进入Python爬虫领域,今天尝试一下爬取链家的二手房,之前已经爬取了房天下的了,看看链家有什么不同,马上开始. 一.分析观察爬取网站结构 这里以广州链家二手房为例:http:/ ...

  6. hibernate for循环执行添加操作出错问题

    操作数据库使用hibernate框架 hibernate插入对象的时候,对于id唯一,数据库设置为自增的时候执行完操作后会将id赋予该对象 再次插入就会出现问题. 由于session缓存,得再研究下.

  7. functools.wraps 带参数的装饰器 多个装饰器装饰同一个函数

    装饰器开发原则 : 开放封闭原则装饰器的作用 :在不改变原函数的调用方式的情况下,在函数的前后添加功能装饰器的本质 : 闭包函数 def wrapper(func): def inner(*args, ...

  8. 【BZOJ 3747】 3747: [POI2015]Kinoman (线段树)

    3747: [POI2015]Kinoman Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 830  Solved: 338 Description ...

  9. ubuntu16.04.2安装完后重启报错[sda] Assuming drive cache: write through

    原因:检测主机的物理连接线,发生问题时"已连接"未勾选,重启的时候找不到iso文件 解决办法:勾选"已连接",重启机器成功

  10. PHP 笔记——PDO操作数据库

    一.简介 ​ PHP 5.1可使用轻量级的统一接口 PDO(PHP Data Object,PHP数据对象)来访问各种常见的数据库.而使用PDO只需要指定不同的 DSN(数据源名称)即可访问不同的数据 ...