Fat Controller CQRS Diet

56
Fat Controller CQRS Diet Derek Comartin @codeopinion codeopinion.com

Transcript of Fat Controller CQRS Diet

Page 1: Fat Controller CQRS Diet

Fat ControllerCQRS Diet

Derek Comartin@codeopinion

codeopinion.com

Page 2: Fat Controller CQRS Diet

Context

Page 3: Fat Controller CQRS Diet
Page 4: Fat Controller CQRS Diet

WinForm PHP

LOBApplications

Page 5: Fat Controller CQRS Diet

WinForm ASP.NET MVC

LOBApplications

Page 6: Fat Controller CQRS Diet

Authorization

Page 7: Fat Controller CQRS Diet

Authorization

Data Access

Page 8: Fat Controller CQRS Diet

Authorization

Data Access

Validation

Page 9: Fat Controller CQRS Diet

Authorization

Data Access

Validation

Business Logic

Page 10: Fat Controller CQRS Diet

Authorization

Data Access

Validation

Business Logic

Too Many Mixed Responsibilities(aka Turd Pile)

Page 11: Fat Controller CQRS Diet

MVCDatabase

Auth DALBLL

Layer All The Things(aka Layered Turd Pile)

Page 12: Fat Controller CQRS Diet

MVC

DatabaseAuth DALBLL

WinForm

Layer All The Things(aka Layered Turd Pile)

Page 13: Fat Controller CQRS Diet

Minimize Coupling to a Framework

Page 14: Fat Controller CQRS Diet
Page 15: Fat Controller CQRS Diet
Page 16: Fat Controller CQRS Diet

Applications are not 1:1Simple Changes

2 Major Pain PointsLayered

Page 17: Fat Controller CQRS Diet

DAL/ProductModel.csDAL/ProductRepository.csBLL/ProductManager.csMVC/ProductController.cs

Simple ChangesLayered

Page 18: Fat Controller CQRS Diet

CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation, a command is any method that mutates state and a query is any method that returns a value).

– Greg Young

CQRS

Page 19: Fat Controller CQRS Diet

http://www.codeproject.com/Articles/555855/Introduction-to-CQRS

Not this…

Page 20: Fat Controller CQRS Diet

MVCDatabase

Auth DALBLL

Layered

Page 21: Fat Controller CQRS Diet

Controller

Command

Query

Database

CQRS

Page 22: Fat Controller CQRS Diet

Controller

Command

QueryDatabase

BLL

DAL

DAL

Auth

Auth

CQRS

Page 23: Fat Controller CQRS Diet

Define an object that encapsulates how a set of

objects interact

Mediator Pattern

Page 24: Fat Controller CQRS Diet

Object BObject A

Mediator Pattern

Page 25: Fat Controller CQRS Diet

Mediator Object BMessage MessageObject A

Mediator Pattern

Page 26: Fat Controller CQRS Diet

MediatR

https://github.com/jbogard/MediatR

Page 27: Fat Controller CQRS Diet
Page 28: Fat Controller CQRS Diet

Query = Message = Request

Mediator Pattern

Page 29: Fat Controller CQRS Diet

MediatR Query HandlerQuery

MediatR

Page 30: Fat Controller CQRS Diet
Page 31: Fat Controller CQRS Diet
Page 32: Fat Controller CQRS Diet

MediatR Query HandlerQuery

MediatR Query

Page 33: Fat Controller CQRS Diet

MediatR Query

Page 34: Fat Controller CQRS Diet

MediatRQuery Query Handler

MediatR Query Handler

Page 35: Fat Controller CQRS Diet
Page 36: Fat Controller CQRS Diet
Page 37: Fat Controller CQRS Diet

Command = Message = Request

MediatR Command

Page 38: Fat Controller CQRS Diet
Page 39: Fat Controller CQRS Diet
Page 40: Fat Controller CQRS Diet
Page 41: Fat Controller CQRS Diet
Page 42: Fat Controller CQRS Diet
Page 43: Fat Controller CQRS Diet
Page 44: Fat Controller CQRS Diet

Notification Handler

Notification Handler

Notification Handler

Notification

MediatR Notification

MediatR

Notification Handler

Notification

Page 45: Fat Controller CQRS Diet
Page 46: Fat Controller CQRS Diet
Page 47: Fat Controller CQRS Diet
Page 48: Fat Controller CQRS Diet

MVCControllersModelsViewsViewModels

Organize By Layer

Page 49: Fat Controller CQRS Diet

Controllers/ShoppingCartController.csViewModels/ShoppingCartViewModel.csModels/ShoppingCart.csViews/ShopingCart/Index.cshtml

Organize By Layer

Page 50: Fat Controller CQRS Diet

FeaturesShoppingCart

AddToCart.csRemoveFromCart.cs

ViewCart.csViewCart.cshtml

ModelsShoppingCart.csAlbum.csCartItem.cs

Organize By Feature

Page 51: Fat Controller CQRS Diet
Page 52: Fat Controller CQRS Diet

ASP.NET MVC

Feature Feature Feature Feature Feature

Database

Data Model

Organize By Feature

Page 53: Fat Controller CQRS Diet

Move from ASP.NET WebAPI to Nancy

1 file per Command/Query (incl. Controller)

Shared concerns can be changed per feature (EF vs EF Core vs Dapper)

Success!

Page 54: Fat Controller CQRS Diet

Segregate our code from Framework

MediatR between integration boundaries

Feature Slices not Layers

Success!

Page 55: Fat Controller CQRS Diet

Compile Time Type CheckingTestsAnalyzer

Container & RegistrationsTests

Trade-Offs & Notes

Page 56: Fat Controller CQRS Diet

That’s it…Thanks!

Derek Comartin@codeopinion

codeopinion.com