domain-driven design tim mccarthy principal engineer, interknowlogy [email protected]

30
Domain-Driven Design Domain-Driven Design Tim McCarthy Tim McCarthy Principal Engineer, Principal Engineer, InterKnowlogy InterKnowlogy [email protected] [email protected]

Upload: jack-sherman-hunter

Post on 29-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Domain-Driven DesignDomain-Driven Design

Tim McCarthyTim McCarthyPrincipal Engineer, Principal Engineer, [email protected]@interknowlogy.com

Page 2: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

About…About…InterKnowlogy InterKnowlogy ((www.InterKnowlogy.comwww.InterKnowlogy.com))

Tim McCarthy, Principal EngineerTim McCarthy, Principal EngineerCustom App Dev / Consulting / Software Custom App Dev / Consulting / Software Engineering Firm headquartered in Carlsbad, CAEngineering Firm headquartered in Carlsbad, CAMicrosoft Gold Partner managed in SoCal and Microsoft Gold Partner managed in SoCal and RedmondRedmondDesign, Architect, Build and Deploy enterprise Design, Architect, Build and Deploy enterprise class applications class applications Industry Experts:Industry Experts:

90% of the company is published90% of the company is publishedMicrosoftMicrosoft®® .NET Application development for 5+ years! .NET Application development for 5+ years!MicrosoftMicrosoft® ® .NET Smart Client pioneers / industry leaders.NET Smart Client pioneers / industry leadersInformation Worker SolutionsInformation Worker SolutionsIntegration / Messaging, B2B / B2C, Wireless / Mobility Integration / Messaging, B2B / B2C, Wireless / Mobility MicrosoftMicrosoft®® SharePoint, Microsoft SharePoint, Microsoft®® BizTalk BizTalk®® Web Services, Web Services, MicrosoftMicrosoft®® Active Directory Active Directory®®, Security, SSO, Authorization, , Security, SSO, Authorization, AuthenticationAuthenticationSolutions on the emerging Microsoft serversSolutions on the emerging Microsoft serversLargest Client: MicrosoftLargest Client: Microsoft

Page 3: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

AgendaAgenda

Organizing Domain LogicOrganizing Domain Logic

What is DDD?What is DDD?

Why Domain-Driven Design (DDD)?Why Domain-Driven Design (DDD)?

DDD Definitions/PatternsDDD Definitions/Patterns

When is DDD appropriate?When is DDD appropriate?

Page 4: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Organizing Domain LogicOrganizing Domain Logic

Transaction ScriptTransaction Script

Domain ModelDomain Model

Table ModuleTable Module

Page 5: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Choosing the Right Choosing the Right ApproachApproach

Page 6: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Demo: Organizing Demo: Organizing Domain LogicDomain Logic

Page 7: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

What is DDD?What is DDD?

encapsulate with

MODEL-DRIVEN

DESIGN

express model with

isolate domain with

encapsulate with

ENTITIES

VALUE OBJECTS

LAYERED

ARCHITECTURE

AGGREGATES

REPOSITORIES

act as root of

SMART UI

X

FACTORIES

encapsulate with

express model with

encapsulate with

mutually exclusivechoices

access with

maintain integrity with

access withSERVICES

express model with

Page 8: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Why Domain-Driven Why Domain-Driven Design (DDD)?Design (DDD)?

Most development time is still spent Most development time is still spent writing plumbing code instead of writing plumbing code instead of business logicbusiness logic

Typically, the UI will change a LOT Typically, the UI will change a LOT more than the business logicmore than the business logic

The model is a great tool for The model is a great tool for communication between developers communication between developers and usersand users

.NET has good support for it!.NET has good support for it!

Page 9: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Ubiquitous LanguageUbiquitous Language

Common terms between business Common terms between business experts and development teamexperts and development team

Use the language in your codeUse the language in your codeNamespacesNamespaces

Class, property, method, variable namesClass, property, method, variable names

Page 10: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Communicating the Communicating the LanguageLanguage

Create whiteboard drawingsCreate whiteboard drawingsFavor whiteboarding over VisioFavor whiteboarding over Visio

Use digital camera to capture, then Use digital camera to capture, then paste into Wordpaste into Word

Use Visual Studio class diagrams for Use Visual Studio class diagrams for core parts of the domaincore parts of the domain

Code and diagram are kept in syncCode and diagram are kept in sync

No wasted time diagramming, your No wasted time diagramming, your diagram is the codediagram is the code

Page 11: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Definition: EntitiesDefinition: Entities

An object defined primarily by its An object defined primarily by its identityidentity

Not its attributesNot its attributes

Could be a person, a customer, an Could be a person, a customer, an order, etc.order, etc.

Not all objects have meaningful Not all objects have meaningful identities…identities…

In some systems, a class may be an In some systems, a class may be an ENTITY, in others maybe notENTITY, in others maybe not

Page 12: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Definition: Value ObjectsDefinition: Value Objects

Represent an aspect of the domain Represent an aspect of the domain with NO conceptual identitywith NO conceptual identity

Use when you care about Use when you care about whatwhat something is, not something is, not whowho they are they are

Same thing as a DTO [Fowler PoEAA]Same thing as a DTO [Fowler PoEAA]

Simple, immutable objectsSimple, immutable objects

Page 13: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Definition: AggregatesDefinition: Aggregates

A cluster of associated objects A cluster of associated objects treated as a unit for the purpose of treated as a unit for the purpose of data changesdata changes

They have a root and a boundaryThey have a root and a boundaryBoundary = what is inside the Boundary = what is inside the AGGREGATEAGGREGATE

Root = single ENTITY inside the Root = single ENTITY inside the AGGREGATEAGGREGATE

Most important concept to Most important concept to understand in modelling!understand in modelling!

Page 14: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Aggregate RulesAggregate Rules

The root ENTITY has global identityThe root ENTITY has global identity

ENTITIES inside the boundary have ENTITIES inside the boundary have local identitylocal identity

Nothing outside the AGGREGATE Nothing outside the AGGREGATE boundary can hold a reference to boundary can hold a reference to anything inside, except to the root anything inside, except to the root ENTITY ENTITY

Objects in the AGGREGATE can hold Objects in the AGGREGATE can hold references to other AGGREGATE references to other AGGREGATE rootsroots

……a few morea few more

Page 15: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Definition: ServicesDefinition: Services

An operation offered as an interface An operation offered as an interface that stands alone in the modelthat stands alone in the model

Does not fit into any of the objects in Does not fit into any of the objects in the modelthe model

StatelessStateless

To be used judiciously (do not turn To be used judiciously (do not turn your app into a Transaction Script)your app into a Transaction Script)

Use when an operation is an Use when an operation is an important domain conceptimportant domain concept

Page 16: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: RepositoryPattern: Repository

Provide access to AGGREGATE rootsProvide access to AGGREGATE roots

Represents all objects of a certain Represents all objects of a certain type as a conceptual set (usually type as a conceptual set (usually emulated)emulated)

Behaves like a collection, e.g. Add(), Behaves like a collection, e.g. Add(), Remove()Remove()

Persistence Ignorance!Persistence Ignorance!

Page 17: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: Layered Pattern: Layered Supertype Supertype [Fowler 475][Fowler 475]

Type that acts as the supertype for Type that acts as the supertype for all types in its layer, i.e. a base class!all types in its layer, i.e. a base class!

Factors out common features, such Factors out common features, such as handling the identity of ENTITIESas handling the identity of ENTITIES

Example: a common ID propertyExample: a common ID property

Helps eliminate duplicate codeHelps eliminate duplicate code

Page 18: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Demo: Creating a DDD Demo: Creating a DDD FrameworkFramework

Page 19: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: Specifications - Pattern: Specifications - ProblemProblem

You need to select a subset of objects You need to select a subset of objects based on some criteria, and to based on some criteria, and to refresh the selection at various timesrefresh the selection at various times

You need to check that only suitable You need to check that only suitable objects are used for a certain roleobjects are used for a certain role

You need to describe what an object You need to describe what an object might do, without explaining the might do, without explaining the details of how the object does it, but details of how the object does it, but in such a way that a candidate might in such a way that a candidate might be built to fulfill the requirementbe built to fulfill the requirement

Page 20: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: Specifications - Pattern: Specifications - DefinitionDefinition

A predicate that determines if an A predicate that determines if an object meets certain criteriaobject meets certain criteria

Explicitly separates the statement of Explicitly separates the statement of how to match a candidate from the how to match a candidate from the candidate object that it is matched candidate object that it is matched againstagainst

States a constraint on the state of States a constraint on the state of another object, which may or may another object, which may or may not be presentnot be present

Keeps the rules in the domain layer!Keeps the rules in the domain layer!The rules themselves become objectsThe rules themselves become objects

Page 21: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: Specifications - Pattern: Specifications - SolutionSolution

Create a specification that is able to Create a specification that is able to tell if a candidate object matches tell if a candidate object matches some criteria. The specification has a some criteria. The specification has a method isSatisfiedBy (candidate) : method isSatisfiedBy (candidate) : Boolean that returns "true" if all Boolean that returns "true" if all criteria are met by the candidate.criteria are met by the candidate.

Page 22: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: Specifications - Pattern: Specifications - Implementation TypesImplementation Types

Hard-codedHard-codedCode the selection criteria into the isSatisfied Code the selection criteria into the isSatisfied method as a block of codemethod as a block of code

ParameterizedParameterizedCreate attributes in the specification for values Create attributes in the specification for values that commonly vary. Code the isSatisfiedBy that commonly vary. Code the isSatisfiedBy method to combine these parameters to make method to combine these parameters to make the testthe test

CompositeCompositeCreate a particular form of interpreter for the Create a particular form of interpreter for the specification. Create leaf elements for the specification. Create leaf elements for the various kinds of tests. Create composite nodes various kinds of tests. Create composite nodes for the and, or, and not operatorsfor the and, or, and not operators

Page 23: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Uses of SpecificationsUses of Specifications

Validating an object against a Validating an object against a constraintconstraint

Select objects from a collectionSelect objects from a collection

Specify the creation of a new object Specify the creation of a new object to fit some needto fit some need

Building to order – you define the specs, Building to order – you define the specs, the builders build to your spec!the builders build to your spec!

Page 24: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Demo: Using Demo: Using SpecificationsSpecifications

Page 25: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

This is DDD…This is DDD…

encapsulate with

MODEL-DRIVEN

DESIGN

express model with

isolate domain with

encapsulate with

ENTITIES

VALUE OBJECTS

LAYERED

ARCHITECTURE

AGGREGATES

REPOSITORIES

act as root of

SMART UI

X

FACTORIES

encapsulate with

express model with

encapsulate with

mutually exclusivechoices

access with

maintain integrity with

access withSERVICES

express model with

Page 26: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

When is DDD AppropriateWhen is DDD Appropriate

“If an unsophisticated team with a simple project decides to try a model-driven design with layered architecture, it will face a difficult learning curve. ... The overhead of managing infrastructure and layers makes very simple tasks take longer. Simple projects come with short time lines and modest expectations.”

Domain-Driven Design by Eric Evans, p76

Page 27: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

ResourcesResources

[Evans]: Domain-Driven Design: [Evans]: Domain-Driven Design: Tackling Complexity in the Heart of Tackling Complexity in the Heart of Software, Evans, Addison-Wesley Software, Evans, Addison-Wesley (2003)(2003)

[Fowler]: Patterns of Enterprise [Fowler]: Patterns of Enterprise Application Architecture, Fowler, Application Architecture, Fowler, Addison-Wesley (2003)Addison-Wesley (2003)

[Nilsson]: Applying Domain Driven [Nilsson]: Applying Domain Driven Design and Patterns: using .NETDesign and Patterns: using .NET

Page 28: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Looking for Outstanding Looking for Outstanding DevelopersDevelopers

Application Development PracticeApplication Development Practice.NET 2.0 and .NET 3.0.NET 2.0 and .NET 3.0

ASP.NET 2.0 and ASP.NET AJAX 1.0ASP.NET 2.0 and ASP.NET AJAX 1.0

WinForms 2.0 / Windows Presentation WinForms 2.0 / Windows Presentation FoundationFoundation

ASMX / Windows Communication ASMX / Windows Communication FoundationFoundation

BizTalkBizTalk

Windows Workflow FoundationWindows Workflow Foundation

SQL ServerSQL Server

Page 29: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Looking for Outstanding Looking for Outstanding DevelopersDevelopers

Information Worker PracticeInformation Worker PracticeSharePoint 2003 / 2007SharePoint 2003 / 2007

Office 2003 / 2007Office 2003 / 2007

SQL Analysis ServicesSQL Analysis Services

SQL Reporting ServicesSQL Reporting Services

Page 30: Domain-Driven Design Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Tim McCarthy, Tim McCarthy, InterKnowlogyInterKnowlogy

Get the Demos & PPT at:Get the Demos & PPT at:Blogs.InterKnowlogy.com/TimMcCarthyBlogs.InterKnowlogy.com/TimMcCarthy

More info on InterKnowlogy:More info on InterKnowlogy:www.InterKnowlogy.comwww.InterKnowlogy.com

Contact me: Tim McCarthyContact me: Tim McCarthy

E-mailE-mail: : [email protected]@InterKnowlogy.com

Phone:Phone: 760-930-0075 x205 760-930-0075 x205

About Tim McCarthyAbout Tim McCarthy.NET Architect/Dev Lead/Trainer.NET Architect/Dev Lead/TrainerAuthor / SpeakerAuthor / SpeakerMCSD, MCSD.NET, MCDBA, MCT, MCSD, MCSD.NET, MCDBA, MCT, IEEE CSDPIEEE CSDP