码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-10-30 18:44:02      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

【先上链接: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<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);
        }
    }
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<Book> {AggregateRoot = book};
            CommandBus.Send(command);
            return Redirect("List");
        }
    }
View Code

4.实现Storage
//Reponsitory

技术分享
public class BookReponsitory : SoftDeleteEntityFrameworkReponsitory<Book>, IBookReponsitory
    {

    }
View Code

//QueryEntry

技术分享
public class BookQueryEntry : ReponsitoryBasedQueryEntry<Book>, IBookQueryEntry
    {
        public override IBasicReponsitory<Book> BasicReponsitory
        {
            get { return BookReponsitory; }
        }

        [Dependency]
        internal IBookReponsitory BookReponsitory { get; set; }
    }
View Code

5.同步数据库定义
//定义并添加一个Map

技术分享
public class BookMap:TableMap<Book>
    {
        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<User> 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

结果测试
技术分享

技术分享

技术分享

收工。

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

标签:

原文地址:http://www.cnblogs.com/lightluomeng/p/4922683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!