SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA...

58
Command/Query Responsibility Segregation SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist

Transcript of SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA...

Page 1: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Command/Query Responsibility Segregation

SESSION CODE: ARC302

Udi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist

Page 2: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Agenda

History Arch. UI Design

Domain Model

Page 3: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

History

Page 4: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Paper ruled the world

Page 5: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

And then came the computer

Page 6: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

And with it data entry

Page 7: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

And efficient UIs were designed for it

OrdersID Total Date Shipped Account etc etc

317 $37.87 Sep 1 2010 Yes A17T5

318 $99.99 July 3 2010 Yes A17T5

319 $100.11 Aug 4 2010 Yes P313Z

320 $69.47 Sep 9 2010 No P599Z

CancelSave

Page 8: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Source of truth

Page 9: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Gone…

Page 10: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

But the UI lingers to this day

Page 11: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Since then…

Commands

QueriesUsers

Page 12: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Distributed Systems Architecture

Page 13: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Common Distributed Architecture

UI

Services

BL

DAL

DB

Page 14: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Collaboration is good – isn’t it ?

Get dataGet data

Change dataUser is looking at stale data

Page 15: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

So why go through all the layers?

UI

Services

BL

DAL

DB

Cache

Page 16: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Why transform between tiers?

Use EF to map from tables to domain objects

Map from DTOs and WS to domain object

Map from DTOs & WS to view model

DBWS

UI

Cache

Page 17: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

What was the point of all this again?

Page 18: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Queries – showing data to the user

?

Page 19: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

SearchFirst think about what the user wants to find

Avoid Excel-like combo-boxes above each columnDesign some custom widgets/screens for that

Eg. Amazon recommendations

Separate UI talking to different data store

SQL Server Full Text SearchLucene.net

Page 20: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Why not be upfront about staleness?

Data correct as of 10 minutes ago

List of customers

Page 21: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Keep it simple - 2-Tier

Persistent View Model

UI

Query only

For each view in the UI, have a view/table in the DB

2 Layers == 2 Tiers

SELECT * FROM MyTable (WHERE ID = @ID)

Page 22: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Data Duplicated, No Relationships, Data Pre-Calculated

List of customers

Customer Service Rep view

ID Name Phone

List of customers

Supervisor view

ID Name Phone Lifetime value

Rep_Customers_TableID Name Phone

Supervisor_Customers_TableID Name Phone Lifetime Value

Page 23: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Deployment and SecurityDeploy the persistent view model DB to the web tier (only SELECT is permitted)

Don’t have to go through the firewall – faster

Role-based securityDifferent screens for different roles go to different tables – SELECT permissions per role

Just as secure as in-memory cachesIf not more so

Page 24: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Use for preliminary validationBefore going to submit data, check if it already exists in the persistent view model

UniquenessCan expose to user (Yahoo user signup)

Related Entity ExistenceAddress validation – existence of street name

Results in less commands being rejected

Page 25: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Commands – accepting user input

!

Page 26: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Validation and Business RulesValidation: Is the input potentially good?

Structured correctly?Ranges, lengths, etc

Rules: Should we do this?Based on the current system stateWhat the user saw is irrelevant

Page 27: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Command Processing Layers

Input from User

Validation

Rules

Persistence

DB

Page 28: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Transaction

Command Processing Tiers

DBWSInput

Validation Validation

Command

Get current state

Rules

Persist

Page 29: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Should we do what the user asks?

Page 30: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

User Interface Design

Page 31: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Traditional User Interfaces

OrdersID Total Date Shipped Account etc etc

317 $37.87 1/9/09 Yes A17T5

318 $99.99 3/7/09 Yes A17T5

319 $100.11 4/8/09 Yes P313Z

320 $69.47 9/9/09 No P599Z

CancelSave

Page 32: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Capturing user intentWhy is it important?

Differentiating between:

Correcting a mistyped shipping addressThe user moved – reroute shipments underway

Sometimes users accidentally modify fields when tabbing between columns

Page 33: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Reservation systems

Page 34: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Reservation systems

Page 35: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Not capturing user intentIn a traditional UI – what we have is the checkbox

Why do users select multiple seats?Because they’re reserving for a family / friends

But then, concurrency happensSomebody else got in first on one of the seats

Try to find a block of seats somewhere else

Page 36: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Capturing user intentGroup reservation:

Small group – sitting togetherLarge group – several small groups

Enter number of peopleEnter preferred seat type – indicates cost

System emails back when reservation can be filledInclude waiting list functionality

Page 37: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Usability benefitsMore users get what they want with less clicks

Page 38: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Scalability benefitsThousands of seats, hundreds of thousands of requests

No need to show actual status

Page 39: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Revisiting the command

!

Page 40: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

What’s a good command?The kind you can reply with:

“Thank you. Your confirmation email will arrive shortly”

Inherently asynchronousNot really related to an entity

Page 41: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

You need a queue

MSMQOutgoing Incoming

Server

Client

MSMQOutgoing Incoming

Page 42: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Eg. Posting a comment to a blogAfter the browser submits the comment, immediately shows it on the page using AJAX

Page 43: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Eg. Withdrawing cash from an ATMUsing the account information retrieved(Account type, Max single withdrawal)

Decides to send the command (or not)

If so, dispenses cash not waiting for a response

Page 44: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Domain Models

Page 45: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

What aren’t they for?Validation

Commands are validated before the domain model is called

Queries

Entity relationships for reading are unnecessary

Page 46: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

What are they for?In addition to doing what the command said, doing other things as well

Eg. When a new order is submitted, if the customer has ordered more than X in the past, give them a discount

Page 47: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Only persist what they needThe domain model isn’t responsible for persisting all data

only what it needs for its purposes

The rest of the world is using the data in the query store anyway

Page 48: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Putting it all together

Page 49: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

For better scalability, add pub/sub

Queries

CommandsDB

WSInput

Validation Validation

Rules

QueriesView Model

View ModelUpdater

Publish

Client

Data from input immediately overlaid on queries

Page 50: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Free OSS .NET Framework

www.NServiceBus.com

Page 51: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

In Closing

Page 52: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

SummaryKeep queries simple – 2-Tier if possible

No data transfer objects

Have commands reflect user intentAdjust UI design to capture intent

Get the benefits of asynchronous programmingSimple validation, focused business rules

Page 53: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist

SESSION CODE: ARC302

[email protected]

Page 54: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

Page 55: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Complete an evaluation on CommNet and enter to win!

Page 56: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st

http://northamerica.msteched.com/registration

You can also register at the

North America 2011 kiosk located at registrationJoin us in Atlanta next year

Page 57: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to

be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 58: SESSION CODE: ARC302 Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist.

JUNE 7-10, 2010 | NEW ORLEANS, LA