domain driven design with nhibernate
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 PresentationTRANSCRIPT
![Page 1: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/1.jpg)
Domain Driven Design with NHibernate
Ben ScheirmanPrincipal Consultant
Sogeti
www.flux88.com
![Page 2: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/2.jpg)
Start with the Database?
![Page 3: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/3.jpg)
Ok, start with the Model
![Page 4: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/5.jpg)
Unit Tests
• We’re POCO, baby!
I love NUnit!
![Page 6: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/6.jpg)
Now… what about persistence?
• Pure POCO Model• Ultimate Flexibility
![Page 7: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/7.jpg)
Barriers?
• Granularity• Inheritance• Associations
![Page 8: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/8.jpg)
The problem of Granularity
User
Address
UsersUserIdFirstNameLastNameAddressLine1AddressLine2
CityStateZip
?
![Page 9: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/10.jpg)
NHibernate in a Nutshell
• ISessionFactory
• ISession
• ITransaction
![Page 11: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/11.jpg)
HELLO WORLD WITH NHIBERNATEDemo time
![Page 12: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/12.jpg)
Key concepts
• ISessionFactory per application
• ISession per Unit of Work
![Page 13: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/15.jpg)
Did I just say XML?
![Page 16: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/16.jpg)
Mapping concepts
• <id>
• <property>
• Associations…
![Page 17: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/31.jpg)
Ticket
MenuItem TicketLine
Payment
PrinterStation
CashPayment
CreditPayment
*
*
*
*
*
![Page 32: Domain Driven Design with NHibernate](https://reader035.vdocuments.net/reader035/viewer/2022062310/56816456550346895dd6240f/html5/thumbnails/32.jpg)
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)