320 likes | 556 Vues
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”.
E N D
Domain Driven Design with NHibernate Ben Scheirman Principal Consultant Sogeti www.flux88.com
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
The problem of Inheritance BillingAccount ? CreditCard CheckingAccount
NHibernate in a Nutshell • ISessionFactory • ISession • ITransaction
Demo time Hello World with NHibernate
Key concepts • ISessionFactory per application • ISession per Unit of Work
NHibernate Architecture Domain Model Persistent Classes IInterceptor ILifecycle IUserType IValidatable NHibernate ISessionFactory ISession ITransaction IQuery Configuration ADO.NET
How do you GLUE the objects and the database? Foo.cs .NET Object Foo.hbm.xml Mapping XML
Mapping concepts • <id> • <property> • Associations…
Unidirectional One to many “A Blog has many Posts” Blog IList<Post> Posts; Post 1 * Blog.hbm.xml <bag name="Posts"> <key column="BlogId" /> <one-to-many class="Post" /> </bag>
Bidirectional Many-to-Many A category has items, an item has categories Item ISet Categories Category ISet Items * * 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 Payment * * CashPayment MenuItem TicketLine * * CreditPayment * PrinterStation
Resources • Domain Driven Design (Eric Evans) • ayende.com/blog • Hibernate in Action • NHibernate in Action (pre-release PDFs) • NHibernate Forums • nhusers@googlegroups.com • Castle Project (Active Record)