CQRS学习——一个例子(其六)
【先上链接:http://pan.baidu.com/s/1o62AHbc 】
多图杀猫
先用一组图看看实现的功能:



添加一个功能
假定现在要添加一个书本录入的功能,那么执行如下的操作:
1.添加Controller
public class BookController : DpfbMvcController
{
public ActionResult List(int size = , int index = )
{
throw new NotImplementedException();
} [Authorize]
public ActionResult Add()
{
return View();
} [Authorize]
[HttpPost]
public ActionResult Add(BookAddViewModel model)
{
throw new NotImplementedException();
}
}
2.定义Book模型,command,查询入口,仓储和实现commandHandler
//book
namespace SI.Cqrs.Models.AggreateRoots
{
public class Book : AggregateRoot
{
public string Name { get; set; }
public decimal Price { get; set; }
}
}
//QueryEntry
public interface IBookQueryEntry : IQueryEntry<Book>
{ }
//Reponsitory
public interface IBookReponsitory:IBasicReponsitory<Book>
{ }
//handler,至于command,这里偷个懒,用泛型类解决
public class BookHandler:ICommandHandler<DpfbItemInsertCommand<Book>>
{
[Dependency]
internal IBookReponsitory BookReponsitory { get; set; } void ICommandHandler<DpfbItemInsertCommand<Book>>.Execute(DpfbItemInsertCommand<Book> command)
{
BookReponsitory.Insert(command.AggregateRoot);
}
}
3.回过头来完成controller未实现的方法
public class BookController : DpfbMvcController
{
[Dependency]
internal IBookQueryEntry BookQueryEntry { get; set; } public ActionResult List(int size = , int index = )
{
var pageInfo = new PageInfo(size, index);
var result = BookQueryEntry.Page(i => i.Name, pageInfo);
return View(result);
} [Authorize]
public ActionResult Add()
{
return View();
} [Authorize]
[HttpPost]
public ActionResult Add(BookAddViewModel model)
{
var book = new Book {Name = model.Name, Price = model.Price};
var command = new DpfbItemInsertCommand<Book> {AggregateRoot = book};
CommandBus.Send(command);
return Redirect("List");
}
}
4.实现Storage
//Reponsitory
public class BookReponsitory : SoftDeleteEntityFrameworkReponsitory<Book>, IBookReponsitory
{ }
//QueryEntry
public class BookQueryEntry : ReponsitoryBasedQueryEntry<Book>, IBookQueryEntry
{
public override IBasicReponsitory<Book> BasicReponsitory
{
get { return BookReponsitory; }
} [Dependency]
internal IBookReponsitory BookReponsitory { get; set; }
}
5.同步数据库定义
//定义并添加一个Map
public class BookMap:TableMap<Book>
{
public BookMap()
{
/*为Name创建唯一索引*/
Property(i => i.Name).IsRequired()
.HasColumnAnnotation(IndexAnnotation.AnnotationName,
new IndexAttribute("IU_UserName", ) {IsUnique = true})
.HasMaxLength();
}
} public class SocialInsuranceContext : DbContext
{
public SocialInsuranceContext()
: base("name=SocialInsuranceContext")
{ } public DbSet<User> Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new UserMap());
modelBuilder.Configurations.Add(new BookMap());
}
}
//更新数据库定义
PM> Add-Migration Initial_Database
Scaffolding migration 'Initial_Database'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Initial_Database' again.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201510300844286_Initial_Database].
Applying explicit migration: 201510300844286_Initial_Database.
Running Seed method.
PM>
结果测试


收工。
CQRS学习——一个例子(其六)的更多相关文章
- 从一个例子学习 instanceof 和 getclass 的区别
判断两个对象是否为同一类型,时常用到getclass 和 instanceof ,而这两个函数又是时常让人混淆.下面从一个例子说明两者的区别: public class Test_drive { pu ...
- CQRS学习——IOC,配置,仓储隔离以及QueryEntry[其三]
从IoC开始说起 博主最早开始用的IoC容器叫AutoFac,那时候用它主要是为了生命周期管理——将EF上下文的生命周期限定为每请求.当然也总是每每听到IoC的好处,但是仍然不能理解其优势.最近在学习 ...
- php部分--面向对象三大特性-封装(另加连续调用的一个例子)、继承(重写、重载的例子)、多态;
一.封装性: 目的:为了使类更加安全. 做法:1设置私有成员 2在类中建方法,访问私有成员 3在方法里边加控制(if) 私有成员访问的两种方法: 方法一:set(可写) get(可读)做方法(可读可写 ...
- CQRS学习——Dpfb以及其他[引]
[Dpfb的起名源自:Ddd Project For Beginer,这个Beginer自然就是博主我自己了.请大家在知晓这是一个入门项目的事实上,怀着对入门者表示理解的心情阅读本系列.不胜感激.] ...
- Webpack入门——使用Webpack打包Angular项目的一个例子
2016.1.22,对大多数人来说,这是一个非常平常的日子,但这却是我决定在博客园写博客的日子.虽然注册博客园的博客已有4年8个月,却一直没有动手写过一篇博客,原因是觉得自己水平不行,写不出好东西,所 ...
- Javascript 进阶 面向对象编程 继承的一个例子
Javascript的难点就是面向对象编程,上一篇介绍了Javascript的两种继承方式:Javascript 进阶 继承,这篇使用一个例子来展示js如何面向对象编程,以及如何基于类实现继承. 1. ...
- [0406]学习一个——Unit 1 Html、CSS与版本控制
前言 最近发现了Github的Student认证,本来想用来注册Digital Ocean搭个梯子,结果注册验证不能用VISA借记卡=~=. 那么在这漫长的清明节假期里,只有学习能满足空虚的内心(划掉 ...
- 通过一个例子了解Ajax
Ajax指的Asyncronous JavaScript and XML Ajax并不是什么新的编程语言, 它是现有一些东西的应用.从它的名称中就可以看出来 假如我们设想, 浏览器展示了一个页面,但需 ...
- 《The art of software testing》的一个例子
这几天一直在看一本书,<The art of software testing>,里面有一个例子挺有感触地,写出来和大家分享一下: [问题] 从输入对话框中读取三个整数值,这三个整数值代表 ...
随机推荐
- html5之meta标签viewport应用
在html5移动页面中,viewport定义必不可少. 首先了解下关于viewport的概念: 先了解移动设备的屏幕尺寸和设备尺寸: iPhone3 设备尺寸 320*480 ; 屏幕尺寸 320* ...
- Sqlserver 关于游标
对于sql来说查询的思维方式的面向集合对于游标来说:思维方式是面向行的 性能上:游标会吃更多内存,减少可见的并发,锁定资源等 当穷尽了while循环,临时表,表变量,自建函数,或其他方式仍然无法实现某 ...
- Office升级到2013版后无法登录微软账号问题
自打office从2010版升级到2013版,就再也无法登录微软账号了.每次点击登录,弹出来的框就显示:this feature has been disabled by your administr ...
- jquery 事件委托绑定click的使用方法
直接绑定ul的click事件 代码如下 复制代码 $("ul").click(function(e) 例子 代码如下 复制代码 $(function(){ //$(" ...
- Oracle工程师技能树
整理了份Oracle工程师的技能树,方便大家在学习的过程中有个大体方向. 欢迎提意见,可以随时更新. 源文件链接地址点此 图片如下: 多媒体插件如下:
- Aisen仿新浪微博客户端项目源码
新浪目前已经限制了第三方微博的很多API接口,加上平常时间不够,所以后续可能不会面向产品的去维护Aisen,不过也有了一些新的方向,例如引入最新Android-support-library,在一个完 ...
- lucene4入门(2)搜索
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440479.html 接着上一篇,这里继续搜索,对于搜索和创建一样,首先你要确定搜索位置,然后用规定的类来读取.还 ...
- pickle模块的基本使用
pickle是python的biult-in模块: python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储 ...
- opencv 手写选择题阅卷 (一)表格设计与识别
(一)答题表格设计与识别 实际设计好的表格如下图 为了图像精确,表格和四角的标记都是由程序生成的,文字和数据是后期排版软件添加上去的. 图中四角的四个黑方块主要用来定位表格,然后就可以切割出每个单元格 ...
- 如何找到Linux下常用命令的源码
Linux系统,常用命令的来源很多,有些命令是shell自带的,比如cd,通过执行help命令,可以查看当前系统所有的内置命令. 用type <cmd_name>来查看一个命令是否为内置命 ...
