Fat Controller CQRS Diet

Post on 07-Feb-2017

76 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

Transcript

Fat ControllerCQRS Diet

Derek Comartin@codeopinion

codeopinion.com

Context

WinForm PHP

LOBApplications

WinForm ASP.NET MVC

LOBApplications

Authorization

Authorization

Data Access

Authorization

Data Access

Validation

Authorization

Data Access

Validation

Business Logic

Authorization

Data Access

Validation

Business Logic

Too Many Mixed Responsibilities(aka Turd Pile)

MVCDatabase

Auth DALBLL

Layer All The Things(aka Layered Turd Pile)

MVC

DatabaseAuth DALBLL

WinForm

Layer All The Things(aka Layered Turd Pile)

Minimize Coupling to a Framework

Applications are not 1:1Simple Changes

2 Major Pain PointsLayered

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

Simple ChangesLayered

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

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

Not this…

MVCDatabase

Auth DALBLL

Layered

Controller

Command

Query

Database

CQRS

Controller

Command

QueryDatabase

BLL

DAL

DAL

Auth

Auth

CQRS

Define an object that encapsulates how a set of

objects interact

Mediator Pattern

Object BObject A

Mediator Pattern

Mediator Object BMessage MessageObject A

Mediator Pattern

MediatR

https://github.com/jbogard/MediatR

Query = Message = Request

Mediator Pattern

MediatR Query HandlerQuery

MediatR

MediatR Query HandlerQuery

MediatR Query

MediatR Query

MediatRQuery Query Handler

MediatR Query Handler

Command = Message = Request

MediatR Command

Notification Handler

Notification Handler

Notification Handler

Notification

MediatR Notification

MediatR

Notification Handler

Notification

MVCControllersModelsViewsViewModels

Organize By Layer

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

Organize By Layer

FeaturesShoppingCart

AddToCart.csRemoveFromCart.cs

ViewCart.csViewCart.cshtml

ModelsShoppingCart.csAlbum.csCartItem.cs

Organize By Feature

ASP.NET MVC

Feature Feature Feature Feature Feature

Database

Data Model

Organize By Feature

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!

Segregate our code from Framework

MediatR between integration boundaries

Feature Slices not Layers

Success!

Compile Time Type CheckingTestsAnalyzer

Container & RegistrationsTests

Trade-Offs & Notes

That’s it…Thanks!

Derek Comartin@codeopinion

codeopinion.com

top related