Introduction

After reading Eric Evans' book Domain-Driven Design, Tackling Complexity in the Heart of Software, my way of designing software systems completely changed. Before that, I used to design software object models in a very data-centric way, and I did not really focus on how to combine behavior and data in objects. I was so inspired with this new way of thinking that I started trying to find any code samples I could get my hands on that demonstrated the concepts from Eric's awesome book. I did the usual Googling for answers to my Domain-Driven Design (DDD) questions, and I usually did find something that would help me, but I still thirsted for more knowledge on the subject.

I had to search for DDD answers in .NET because Eric's book is technology-agnostic. The main point of the book was the architectural concepts. There were code samples here and there in Java and Smalltalk, but that was about it. Then along came Jimmy Nilsson's book Applying Domain-Driven Design and Patterns, and it was then that I started to see a lot more of the patterns that could be used in conjunction with the DDD concepts. Jimmy tied together some of the concepts from Martin Fowler's excellent book Patterns of Enterprise Application Architecture and showed how they could help with good DDD design principles. Jimmy also did a great job providing lots of good .NET code examples in his book, as well as leading the reader down several paths to accomplish things the DDD way. Right after I finished Jimmy's book, I started subscribing to the DDD RSS Group feed on Yahoo! Groups, and this also helped me a great deal. One of the things I discovered while on the DDD group was that people kept asking for a .NET reference application that would showcase the DDD principles. After reading these posts for a while, I decided that I should write this book and give the developer community my take on how to build .NET applications using DDD techniques. I guess I probably felt a little bit guilty because I read so many other people's posts on the group, and I never posted very often. So now, instead of posting, I have written a book! Maybe this will be my catalyst to get more involved with the group.

My main goal in writing this book was to take the ideas and patterns from Eric's, Martin's, and Jimmy's books and use the ideas and concepts from them to build an actual end-to-end .NET application. I really wanted to show some of my ideas of how to build a domain model in .NET using DDD principles, but, I did not want to build just any old .NET application; I also wanted to try out some of the latest technologies from Microsoft in building the application, such as Visual Studio 2008 and the .NET 3.5 Framework.

Who This Book Is For

This book is targeted at experienced .NET developers who are looking to hone their object-oriented design skills and learn about DDD. If you are not at that level, that is okay, but I recommend that you at least have some experience writing .NET code or even Java code. If you have not written any .NET code before, this book may be a little bit hard to follow.

I also recommend that you read the books that I mentioned earlier from Eric Evans, Jimmy Nilsson, and Martin Fowler. You do not have to do this, but I highly recommend it, as it will help you understand better many of the designs and patterns in this book.

Since each Chapter in this book builds upon the previous chapter, I recommend that you read the book in Chapter order.

What This Book Covers

Chapter 1, "Introducing the Project: The SmartCA Application"— This Chapter introduces you to the application that I am building, the SmartCA application. I outline the problems of the legacy application and the requirements for the new application, as well as what technologies and designs I plan to use to satisfy all of the requirements.

Chapter 2, "Designing the Layered Architecture"—This Chapter covers the architectural foundations that will be used in the rest of the book. Several patterns are introduced in the chapter, which include the Layered Supertype pattern, the Separated Interface pattern, and the Model-View-ViewModel pattern. I also identify and explain several important DDD concepts. This also is the first Chapter where I start to write the application code, with a focus on the infrastructure layer.

Chapter 3, "Managing Projects"—In this chapter, I start implementing the functionality for managing Projects in the application. I also discuss the concept of Contractors and how they relate to Projects as well as introducing the first iteration of code for the Model-View-ViewModel pattern.

Chapter 4, "Companies and Contacts"—In this chapter, I define and model Companies, Contacts, and Project Contacts. I also show how I deal with saving Entities that are not their own Aggregate Root. This was demonstrated by the techniques I used to save Project Contacts within the Project Aggregate. Last, I show a technique I came up with for displaying and editing Value objects in the UI.

Chapter 5, "Submittal Transmittals"—In this chapter, I introduce the concept of a Submittal Transmittal as used in the construction industry, and then I use the concept to model the Submittal Aggregate. I add a new concept to both the domain layer and the infrastructure layer, illustrating how to deal with saving child collections from the Entity Root repository. I also cover building User Controls that use the Xceed Data Grid Control.

Chapter 6, "Requests for Information"—In this chapter, I introduce the construction industry concept of a Request for Information (RFI). I also introduce a new pattern to the domain called the Specification pattern. I also do some major refactoring in this Chapter on the Repositories and View Models for dealing with Transmittals.

Chapter 7, "Proposal Requests"—In this chapter, I introduce the concept of a Proposal Request in the construction industry. In this chapter, I start adding more behavior to the domain model and demonstrating richer Domain Model classes. I also cover handling broken business rules inside of my Domain Model classes, and tie in the Specification functionality.

Chapter 8, "Change Orders"—In this chapter, I introduce the concept of a Change Order in the construction industry. I continue to add more behavior to my Domain Model classes in this chapter, and continue to develop richer Domain Model classes. Two important interfaces are introduced in this chapter, the IEntity interface and the IAggregateRoot interface. This causes quite a bit of good refactoring throughout the domain model. Last, I create some more advanced Specification classes.

Chapter 9, "Construction Change Directives"—In this chapter, I introduce the concept of a Construction Change Directive in the construction industry. I do a lot of refactoring in this chapter, mostly focused on the various ViewModel classes. In this chapter, I demonstrate the power of combining interfaces with Generics.

Chapter 10, "Synchronizing with the Server"—In this chapter, I design and implement how to synchronize the client's offline data with the server. I show how to store transaction messages on the client, and also show how to synchronize those messages on the client with the messages on the server. I also show how to make sure that all of the synchronization logic is implemented in the domain model.

Chapter 11, "The Client Membership System"—In this chapter, I show you how to allow users to be able to perform membership-related tasks in an offline scenario by creating what I call my Client Membership System. This involves a very rich domain model for representing the Users and their membership data, as well as a new concept of using a Provider instead of a Repository for interacting with the data store. I also show how to take advantage of the Synchronization code from Chapter 10.

How This Book Is Structured

This book is essentially a very large case study. Throughout the chapters, a complete application is built from start to finish. The structure for each Chapter is the same; it is generally a self-contained module with a problem, design, and solution that adds some new aspect of functionality to the application that is being built, followed by a summary at the end of the chapter.

Most of the time, the Problem sections are fairly short, whereas the Design and Solution sections make up most of the bulk of the chapters. The Solution section will always contain the code that implements what was designed in the Design section.

What You Need to Use This Book

You will need Visual Studio 2008 (which includes the .NET 3.5 Framework in its installation) in order to run all of the code samples in the book. I highly recommend using Visual Studio 2008 Professional Edition so that you can run all of the unit tests I have written as part of the code base.

In addition, you will need to install the following applications and components:

  • SQL Server Compact 3.5 (SQL CE)—This is freely downloadable from the Microsoft SQL Server web site (www.microsoft.com/sql/editions/compact/downloads.mspx).

  • Version 1.3 of the Xceed DataGrid Control for WPF—This also freely downloadable from the Xceed web site (http://xceed.com/Grid_WPF_New.html).

  • One of the available versions of SQL Server 2008—This is necessary if you want to be able to use the SQL Server Management Studio to make changes to the SQL CE database. The Express Edition is freely downloadable from www.microsoft.com/sql/2008/prodinfo/download.mspx.

Source Code

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for downloading at www.wrox.com. Once at the site, simply locate the book's title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book.

Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-0-470-14756-6.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Also, if you are interested in seeing how the code continues to iterate and grow after you finish the book, please visit my CodePlex site for this book's code at www.codeplex.com/dddpds. Here you will find new code that was written after the book was published.

Errata

We make every effort to ensure that there are no errors in the text or in the code. Nevertheless, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration, and at the same time you will be helping us provide even higher-quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book's errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot "your" error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to email you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.

  2. Read the terms of use and click Agree.

  3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

  4. You will receive an email with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages that other users post. You can read messages at any time on the web. If you would like to have new messages from a particular forum emailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.190.167