博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CQRS学习——一个例子(其六)
阅读量:5811 次
发布时间:2019-06-18

本文共 4425 字,大约阅读时间需要 14 分钟。

【先上链接:http://pan.baidu.com/s/1o62AHbc 】

多图杀猫

先用一组图看看实现的功能:

添加一个功能

假定现在要添加一个书本录入的功能,那么执行如下的操作:

1.添加Controller

public class BookController : DpfbMvcController    {        public ActionResult List(int size = 10, int index = 1)        {            throw new NotImplementedException();        }        [Authorize]        public ActionResult Add()        {            return View();        }        [Authorize]        [HttpPost]        public ActionResult Add(BookAddViewModel model)        {            throw new NotImplementedException();        }    }
View Code

2.定义Book模型,command,查询入口,仓储和实现commandHandler

//book

namespace SI.Cqrs.Models.AggreateRoots{    public class Book : AggregateRoot    {        public string Name { get; set; }        public decimal Price { get; set; }    }}
View Code

//QueryEntry

public interface IBookQueryEntry : IQueryEntry
{ }

//Reponsitory

public interface IBookReponsitory:IBasicReponsitory
{ }

//handler,至于command,这里偷个懒,用泛型类解决

public class BookHandler:ICommandHandler
> { [Dependency] internal IBookReponsitory BookReponsitory { get; set; } void ICommandHandler
>.Execute(DpfbItemInsertCommand
command) { BookReponsitory.Insert(command.AggregateRoot); } }
View Code

3.回过头来完成controller未实现的方法

public class BookController : DpfbMvcController    {        [Dependency]        internal IBookQueryEntry BookQueryEntry { get; set; }        public ActionResult List(int size = 10, int index = 1)        {            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
{AggregateRoot = book}; CommandBus.Send(command); return Redirect("List"); } }
View Code

4.实现Storage

//Reponsitory

public class BookReponsitory : SoftDeleteEntityFrameworkReponsitory
, IBookReponsitory { }
View Code

//QueryEntry

public class BookQueryEntry : ReponsitoryBasedQueryEntry
, IBookQueryEntry { public override IBasicReponsitory
BasicReponsitory { get { return BookReponsitory; } } [Dependency] internal IBookReponsitory BookReponsitory { get; set; } }
View Code

5.同步数据库定义

//定义并添加一个Map

public class BookMap:TableMap
{ public BookMap() { /*为Name创建唯一索引*/ Property(i => i.Name).IsRequired() .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAttribute("IU_UserName", 1) {IsUnique = true}) .HasMaxLength(100); } }public class SocialInsuranceContext : DbContext { public SocialInsuranceContext() : base("name=SocialInsuranceContext") { } public DbSet
Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new UserMap()); modelBuilder.Configurations.Add(new BookMap()); } }
View Code

//更新数据库定义

PM> Add-Migration Initial_DatabaseScaffolding 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-DatabaseSpecify 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>
View Code

结果测试

收工。

转载于:https://www.cnblogs.com/lightluomeng/p/4922683.html

你可能感兴趣的文章
使用Openfiler搭建ISCSI网络存储
查看>>
iOS - UIViewController
查看>>
IntPtr 转 string
查看>>
学生名单
查看>>
(转) 多模态机器翻译
查看>>
【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档
查看>>
矩阵常用归一化
查看>>
Oracle常用函数总结
查看>>
【聚能聊有奖话题】Boring隧道掘进机完成首段挖掘,离未来交通还有多远?
查看>>
USNews大学排名遭美国计算机研究学会怒怼,指排名荒谬要求撤回
查看>>
struts1——静态ActionForm与动态ActionForm
查看>>
七大关键数据 移动安全迎来历史转折点
查看>>
在AngularJS中学习javascript的new function意义及this作用域的生成过程
查看>>
盘点物联网网关现有联网技术及应用场景
查看>>
1、下载安装scala编译器(可以理解为scala的jdk),地址:http://www.scala
查看>>
mui 总结2--新建第一个app项目
查看>>
nginx的lua api
查看>>
考研太苦逼没坚持下来!看苑老师视频有点上头
查看>>
HCNA——RIP的路由汇总
查看>>
zabbix监控php状态(四)
查看>>