Domain Driven Design with NHibernate
Ben ScheirmanPrincipal 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”
Ignore Persistence for Now
Unit Tests
• We’re POCO, baby!
I love NUnit!
Now… what about persistence?
• Pure POCO Model• Ultimate Flexibility
Barriers?
• Granularity• Inheritance• Associations
The problem of Granularity
User
Address
UsersUserIdFirstNameLastNameAddressLine1AddressLine2
CityStateZip
?
The problem of Inheritance
BillingAccount
CreditCard
BillingAccounts
BillingAcountIdcc_number (NULL)exp_date (NULL)TypeRouting_number (NULL)Account_number (NULL)
?CheckingAccount
NHibernate in a Nutshell
• ISessionFactory
• ISession
• ITransaction
HELLO WORLD WITH NHIBERNATEDemo time
Key concepts
• ISessionFactory per application
• ISession per Unit of Work
NHibernate Architecture
ILifecycle
IValidatable
IInterceptor
IUserType
ISession ITransaction IQuery
ISessionFactory
Configuration
Persistent Classes
Domain Model
NHibernate
ADO.NET
Foo.cs.NET
Object
Foos
PK
Column 1
Column 2
Foo.hbm.xmlMapping
How do you GLUE the objects and the database?
XML
Did I just say XML?
Mapping concepts
• <id>
• <property>
• Associations…
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
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?
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
A DOMAIN DRIVEN DESIGN EXPERIENCE
• A restaurant owner wants us to build him some point-of-sale software for his chain of restaurants.
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
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
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
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
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
So what happens then?
The waiter will review what he rung up and then send the order.
You
Domain Expert
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
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
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
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
Ticket
MenuItem TicketLine
Payment
PrinterStation
CashPayment
CreditPayment
*
*
*
*
*
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)
Top Related