domain driven design with nhibernate

32
Domain Driven Design with NHibernate Ben Scheirman Principal Consultant Sogeti www.flux88.com

Upload: gavin

Post on 23-Feb-2016

73 views

Category:

Documents


1 download

DESCRIPTION

Domain Driven Design with NHibernate. Ben Scheirman Principal Consultant Sogeti www.flux88.com. Start with the Database?. Ok, start with the Model. Domain Driven Design. Focus on Core Domain first Work closely with Domain Experts Learn & Use the “Ubiquitous Language”. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Domain Driven Design with NHibernate

Domain Driven Design with NHibernate

Ben ScheirmanPrincipal Consultant

Sogeti

www.flux88.com

Page 2: Domain Driven Design with NHibernate

Start with the Database?

Page 3: Domain Driven Design with NHibernate

Ok, start with the Model

Page 4: Domain Driven Design with NHibernate

Domain Driven Design

• Focus on Core Domain first• Work closely with Domain Experts• Learn & Use the “Ubiquitous Language”

Ignore Persistence for Now

Page 5: Domain Driven Design with NHibernate

Unit Tests

• We’re POCO, baby!

I love NUnit!

Page 6: Domain Driven Design with NHibernate

Now… what about persistence?

• Pure POCO Model• Ultimate Flexibility

Page 7: Domain Driven Design with NHibernate

Barriers?

• Granularity• Inheritance• Associations

Page 8: Domain Driven Design with NHibernate

The problem of Granularity

User

Address

UsersUserIdFirstNameLastNameAddressLine1AddressLine2

CityStateZip

?

Page 9: Domain Driven Design with NHibernate

The problem of Inheritance

BillingAccount

CreditCard

BillingAccounts

BillingAcountIdcc_number (NULL)exp_date (NULL)TypeRouting_number (NULL)Account_number (NULL)

?CheckingAccount

Page 10: Domain Driven Design with NHibernate

NHibernate in a Nutshell

• ISessionFactory

• ISession

• ITransaction

Page 11: Domain Driven Design with NHibernate

HELLO WORLD WITH NHIBERNATEDemo time

Page 12: Domain Driven Design with NHibernate

Key concepts

• ISessionFactory per application

• ISession per Unit of Work

Page 13: Domain Driven Design with NHibernate

NHibernate Architecture

ILifecycle

IValidatable

IInterceptor

IUserType

ISession ITransaction IQuery

ISessionFactory

Configuration

Persistent Classes

Domain Model

NHibernate

ADO.NET

Page 14: Domain Driven Design with NHibernate

Foo.cs.NET

Object

Foos

PK

Column 1

Column 2

Foo.hbm.xmlMapping

How do you GLUE the objects and the database?

XML

Page 15: Domain Driven Design with NHibernate

Did I just say XML?

Page 16: Domain Driven Design with NHibernate

Mapping concepts

• <id>

• <property>

• Associations…

Page 17: Domain Driven Design with NHibernate

Unidirectional One to many

“A Blog has many Posts”

BlogIList<Post> Posts; Post1 *

Blog.hbm.xml

<bag name="Posts"> <key column="BlogId" /> <one-to-many class="Post" /></bag>

BlogsBlogId

BlogName

Author

DateCreated

PostsPostId

BlogId

Title

Body

DatePosted

Page 18: Domain Driven Design with NHibernate

Bidirectional Many-to-Many

A category has items, an item has categoriesItem

ISet CategoriesCategoryISet Items* *

Items

ItemId

Name

Price

Item_Categories

ItemId

CategoryId

Categories

CategoryId

Name

Item.hbm.xml

<set name="Categories" table="Item_Categories"> <key column="ItemId" /> <many-to-many class="Category" /> </set>

Category.hbm.xml

<set name=“Items" table="Item_Categories“ inverse=“true”> <key column=“CategoryId" /> <many-to-many class=“Item" /> </set>

What is INVERSE?

Page 19: Domain Driven Design with NHibernate

Understanding INVERSE

Post p = new Post();Category cat = new Category();p.Categories.Add(cat);cat.Posts.Add(p);

INSERT Post_Categories(PostId, CategoryId) VALUES(3, 15)

INSERT Post_Categories(PostId, CategoryId) VALUES(3, 15)

NHibernate needs to “ignore” one of the collections

Page 20: Domain Driven Design with NHibernate

A DOMAIN DRIVEN DESIGN EXPERIENCE

• A restaurant owner wants us to build him some point-of-sale software for his chain of restaurants.

Page 21: Domain Driven Design with NHibernate

Can you tell me about how the process works?

Sure, a waiter takes an order for a table. Then he rings it up at

the computer system…

You

Domain Expert

Page 22: Domain Driven Design with NHibernate

Can you tell me about how the process works?

Sure, a waiter takes an order for a table. Then he rings it up at

the computer system.

You

Domain Expert

Page 23: Domain Driven Design with NHibernate

Can you elaborate on “rings it up? “ What exactly would he do?

Well, he enters the table number and a ticket opens. Actually, if the there was already a ticket for that

table open, then it would be displayed. Otherwise a new ticket

is created.

We need to record how many guests are at the table when the

ticket is created.

You

Domain Expert

Page 24: Domain Driven Design with NHibernate

What goes on the ticket?

Oh yeah, the waiter punches in the order for the table. Like what

drinks everyone had, what meal selection. Any menu item really.

Along with the item the price is shown and a running total is

displayed at the bottom.

You

Domain Expert

Page 25: Domain Driven Design with NHibernate

So the Ticket is like the printed ticket you would receive when

you’re ready to pay the bill?

Yeah, pretty much.

You

Domain Expert

Page 26: Domain Driven Design with NHibernate

So what happens then?

The waiter will review what he rung up and then send the order.

You

Domain Expert

Page 27: Domain Driven Design with NHibernate

What happens when the order gets “sent?”

It prints up at the station that prepares the item.

On these “chitters” it has the time the item was ordered, the waiter’s

name, and the table number.

You

Domain Expert

Page 28: Domain Driven Design with NHibernate

So, each item needs to specify what printers it prints at, correct?

Yes, we should be able to pick from any number of printers, for

example: Bar Printer, Line Printer, Grill Printer, and Dessert Printer.

You

Domain Expert

Page 29: Domain Driven Design with NHibernate

Say the waiter opens up an existing ticket.

If they ring up additional items and click send, you only want the new

items to get sent, right?

Yeah, that’s correct. I’m thinking that the items that were already

sent would be in a lighter grey font so that it would be easy to see.

You

Domain Expert

Page 30: Domain Driven Design with NHibernate

So what happens when the guests are ready to pay?

The waiter can take cash or credit payments.

The recorded payments need to satisfy the total before the ticket

can be closed.

You

Domain Expert

Page 31: Domain Driven Design with NHibernate

Ticket

MenuItem TicketLine

Payment

PrinterStation

CashPayment

CreditPayment

*

*

*

*

*

Page 32: Domain Driven Design with NHibernate

Resources

• Domain Driven Design (Eric Evans)• ayende.com/blog• Hibernate in Action• NHibernate in Action (pre-release PDFs)• NHibernate Forums• [email protected]

• Castle Project (Active Record)