Transcript
Page 1: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Domain-Driven DesignDomain-Driven Designusing the ADO.NET Entity using the ADO.NET Entity FrameworkFramework

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

Page 2: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

About…About…InterKnowlogy InterKnowlogy ((www.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 using the ADO.NET Entity Framework 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

ADO.NET Entity FrameworkADO.NET Entity Framework

When is DDD appropriate?When is DDD appropriate?

Page 4: Domain-Driven Design using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Choosing the Right Choosing the Right ApproachApproach

Page 6: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Demo: Organizing Demo: Organizing Domain LogicDomain Logic

Page 7: Domain-Driven Design using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 17: Domain-Driven Design using the ADO.NET Entity Framework 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(), FindBy(id), etc.Remove(), FindBy(id), etc.

Persistence Ignorance!Persistence Ignorance!

Page 18: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Pattern: Layered Pattern: Layered ArchitectureArchitecture

Page 19: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Infrastructure Persistence Infrastructure Persistence StrategiesStrategies

Remember, Domain Model is Remember, Domain Model is Persistent Ignorant!Persistent Ignorant!

ApproachesApproachesCustom Manual CodeCustom Manual Code

Code GenerationCode Generation

Metadata Mapping (O/R Mapping)Metadata Mapping (O/R Mapping)

Page 20: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

ADO.NET Entity ADO.NET Entity Framework: GoalsFramework: Goals

Raise abstraction Raise abstraction Level for Data Level for Data Programming Programming

Get Rid of the Get Rid of the PlumbingPlumbing

Page 21: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

ADO.NET Entity ADO.NET Entity FrameworkFramework

ProvidesProvidesMapping from relational data to .NET Mapping from relational data to .NET classesclasses

Auto-generation of both mapping and Auto-generation of both mapping and entitiesentities

Uses a New Data ProviderUses a New Data ProviderThe EntityClient ProviderThe EntityClient Provider

SupportsSupportsQuerying over entitiesQuerying over entities

Relationship navigationRelationship navigation

Entity InheritanceEntity Inheritance

Page 22: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

ADO.NET Entity ADO.NET Entity Framework: Metadata Framework: Metadata StructureStructure

Page 23: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Demo: Entity FrameworkDemo: Entity Framework

Page 24: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

Entity Framework: Entity Framework: ImpressionsImpressions

Great Great HopeHope for the future, but for the future, but PainPain for the presentfor the present

EDM designer not there yet (after EDM designer not there yet (after Orcas)Orcas)

Does not currently support Does not currently support Persistence Ignorance, but the team Persistence Ignorance, but the team is working on itis working on it

Rich change-tracking supportRich change-tracking support

Page 25: Domain-Driven Design using the ADO.NET Entity Framework Tim McCarthy Principal Engineer, InterKnowlogy timm@interknowlogy.com

DDD Revisited…DDD Revisited…

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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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 using the ADO.NET Entity Framework 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.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


Top Related