eShopOnContainers 学习之--Ordering的中介者模式
2017-12-13 18:21
806 查看
前言
学习目的:MediatR是什么?
为什么他要用MediatR?
怎么使用MediatR?
MediatR是什么?
MediatR 官方网站:https://github.com/jbogard/MediatR。官方介绍:
Simple mediator implementation in .NET
什么是【mediator/中介者模式】?
PS:其实之前学习23种设计模式中学过他,用的少就快忘记完了。以前碰到的类似问题基本上都用观察者模式去解决(特别是出了ReactiveX 出现了以后),没有考虑用使用这种设计模式。
这儿有个详细解释:23种设计模式(7):中介者模式
直接抄下他的优点:
适当地使用中介者模式可以避免同事类之间的过度耦合,使得各同事类之间可以相对独立地使用。
使用中介者模式可以将对象间一对多的关联转变为一对一的关联,使对象间的关系易于理解和维护。
使用中介者模式可以将对象的行为和协作进行抽象,能够比较灵活的处理对象间的相互作用。
为什么他要用MediatR?
在 Ordering.API 中主要干了下面的事情:数据校验;
实现队列请求(Request 、Order);
与 Dapper 配合代替 EF;
同事类交互(Paid、Buyer、Order State)
怎么使用MediatR?
注册:public IServiceProvider ConfigureServices(IServiceCollection services) { //... //configure autofac var container = new ContainerBuilder(); container.Populate(services); container.RegisterModule(new MediatorModule()); // 把 MediatR 注入到 Autofac 中 container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"])); // 注册本项目用到的同事类和 handler,因为要使用 Dapper,所以把数据库连接字符串传进去了。 return new AutofacServiceProvider(container.Build()); }
MediatorModule:
public class MediatorModule : Autofac.Module { protected override void Load(ContainerBuilder builder) { builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly) .AsImplementedInterfaces(); // Register all the Command classes (they implement IAsyncRequestHandler) in assembly holding the Commands builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly) .AsClosedTypesOf(typeof(IAsyncRequestHandler<,>)); // Register the DomainEventHandler classes (they implement IAsyncNotificationHandler<>) in assembly holding the Domain Events builder.RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly) .AsClosedTypesOf(typeof(IAsyncNotificationHandler<>)); // Register the Command's Validators (Validators based on FluentValidation library) builder .RegisterAssemblyTypes(typeof(CreateOrderCommandValidator).GetTypeInfo().Assembly) .Where(t => t.IsClosedTypeOf(typeof(IValidator<>))) .AsImplementedInterfaces(); builder.Register<SingleInstanceFactory>(context => { var componentContext = context.Resolve<IComponentContext>(); return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; }; }); builder.Register<MultiInstanceFactory>(context => { var componentContext = context.Resolve<IComponentContext>(); return t => { var resolved = (IEnumerable<object>)componentContext.Resolve(typeof(IEnumerable<>).MakeGenericType(t)); return resolved; }; }); builder.RegisterGeneric(typeof(LoggingBehavior<,>)).As(typeof(IPipelineBehavior<,>)); builder.RegisterGeneric(typeof(ValidatorBehavior<,>)).As(typeof(IPipelineBehavior<,>)); } }
看注释就能看明白,后续就不贴代码。
待续,抽时间用 MediatR 写个项目。
相关文章推荐
- [eShopOnContainers 学习系列] - Index - 开篇索引
- [eShopOnContainers 学习系列] - 00 - 开发环境需求
- [eShopOnContainers 学习系列] - 01 - Roadmap and Milestones for future releases
- [eShopOnContainers 学习系列] - 03 - 在远程 Ubuntu 16.04 上配置开发环境
- [eShopOnContainers 学习系列] - 02 - vs 2017 开发环境配置
- 设计模式学习—Mediator(中介者)
- 设计模式学习日志七:中介者模式(原文转载)
- 终于把eShopOnContainer部署成功了。
- 设计模式学习笔记--中介者模式Mediator pattern(Java版)
- Java设计模式学习记录-中介者模式
- javascript设计模式学习之十四——中介者模式
- 中介者模式 - 设计模式学习
- VB.net 学习设计模式(中介者模式)
- 再起航,我的学习笔记之JavaScript设计模式23(中介者模式)
- 【HeadFirst 设计模式学习笔记】17 中介者(Mediator)模式拾零
- Java设计模式学习记录-中介者模式
- java学习笔记-设计模式23(中介者模式)
- HeadFir st 设计模式学习笔记18--中介者(M ediator)模式拾零
- 2017.01.12 学习日记-中介者模式
- 设计模式学习--中介者模式