CQRS学习——一个例子(其六)

【先上链接: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_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> 
View Code

结果测试

收工。

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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部