Difference between revisions of "Domain Driven Architecture"

From Opentaps Wiki
Jump to navigationJump to search
m (Protected "Domain Driven Architecture": Sysop page [edit=sysop:move=sysop])
(What is Domain Driven Design?)
Line 6: Line 6:
  
 
The basic idea behind a domain is that it is used to group together all the "domain expertise," or business knowledge, of an application and separated from the application and its infrastructure.  It is a different way of thinking about how to organize large software applications and complements the popular Model View Controller (MVC) architecture, which we also use in opentaps.  With the Model View Controller architecture, the application's user interface (View) is separated from its business logic (Model), and a Controller directs requests from the view layer to the relevant business logic found in the model layer.  The advantage of doing this is that the same business logic could then be reused elsewhere, either in another view layer page or as part of some other more complex business logic in the model layer.
 
The basic idea behind a domain is that it is used to group together all the "domain expertise," or business knowledge, of an application and separated from the application and its infrastructure.  It is a different way of thinking about how to organize large software applications and complements the popular Model View Controller (MVC) architecture, which we also use in opentaps.  With the Model View Controller architecture, the application's user interface (View) is separated from its business logic (Model), and a Controller directs requests from the view layer to the relevant business logic found in the model layer.  The advantage of doing this is that the same business logic could then be reused elsewhere, either in another view layer page or as part of some other more complex business logic in the model layer.
 +
 +
MVC, however, doesn't really say how your model should be structured.  Should it be object-oriented, or should all be written in procedure languages or even SQL?  Should they reside in separate components and packages, or could you just have one big file, which has all of your business logic?  The domain driven design answers this question by separating the model layer ("M") of MVC into an application tier, a domain tier, and an infrastructure tier.  The infrastructure tier is used to retrieve and store data.  The domain tier is where the business knowledge or expertise is.  The application tier is responsible for using the infrastructure tier to obtain the data, then consult the domain tier to see what should be done, and then use the infrastructure tier again to achieve the results.
 +
 +
For example, let's say that you wanted to assess late charges on all of your customers' outstanding invoices.  MVC would tell you that your application should have a screen which shows you a list of outstanding invoices, and when the user says "Assess Late Charges", the controller would pass the users' input parameters to business logic in the model tier to do the dirty work of assessing those late charges. 
 +
 +
With a domain driven design, we would look more deeply at what that dirty work actually involved.  The application tier would call upon the infrastructure tier to retrieve all the invoices which should ''may'' get assessed charges.  Then, it would present that list of invoices to the domain tier, which has the business expertise to say "Should this invoice get charged?" and if so "How much should this invoice get charged?"  The domain tier would then return the late charges for each invoice to the application tier, the application tier would then called on the infrastructure tier again to store the late charges into the database.

Revision as of 23:48, 16 June 2008

One problem with early versions of opentaps is that the ofbiz framework which we used to develop it is not an object-oriented framework. Instead, it is based on a data model, which is fundamentally relational, and that data model is accessed via a map-like Java Object called GenericValue. Most of the services in the business tier used a GenericDelegator to retrieve GenericValues from the database, performed operations on them, and then stored them back into the database again using the same GenericDelegator.

While this is a very lightweight architecture from a perspective, as opentaps grew it became apparent that some of the application could significantly benefit from an object-oriented architecture. A few months ago, we started down this path and thought about how to write more object-oriented code with the ofbiz framework. More recently, after reading about Domain Driven Design, we realized that what we really needed was not just object-oriented code, but rather a more formal classification of our business tier into domains. This document explains what domain driven architecture is, how we have implemented it, and how it could help you structure your code.

What is Domain Driven Design?

The basic idea behind a domain is that it is used to group together all the "domain expertise," or business knowledge, of an application and separated from the application and its infrastructure. It is a different way of thinking about how to organize large software applications and complements the popular Model View Controller (MVC) architecture, which we also use in opentaps. With the Model View Controller architecture, the application's user interface (View) is separated from its business logic (Model), and a Controller directs requests from the view layer to the relevant business logic found in the model layer. The advantage of doing this is that the same business logic could then be reused elsewhere, either in another view layer page or as part of some other more complex business logic in the model layer.

MVC, however, doesn't really say how your model should be structured. Should it be object-oriented, or should all be written in procedure languages or even SQL? Should they reside in separate components and packages, or could you just have one big file, which has all of your business logic? The domain driven design answers this question by separating the model layer ("M") of MVC into an application tier, a domain tier, and an infrastructure tier. The infrastructure tier is used to retrieve and store data. The domain tier is where the business knowledge or expertise is. The application tier is responsible for using the infrastructure tier to obtain the data, then consult the domain tier to see what should be done, and then use the infrastructure tier again to achieve the results.

For example, let's say that you wanted to assess late charges on all of your customers' outstanding invoices. MVC would tell you that your application should have a screen which shows you a list of outstanding invoices, and when the user says "Assess Late Charges", the controller would pass the users' input parameters to business logic in the model tier to do the dirty work of assessing those late charges.

With a domain driven design, we would look more deeply at what that dirty work actually involved. The application tier would call upon the infrastructure tier to retrieve all the invoices which should may get assessed charges. Then, it would present that list of invoices to the domain tier, which has the business expertise to say "Should this invoice get charged?" and if so "How much should this invoice get charged?" The domain tier would then return the late charges for each invoice to the application tier, the application tier would then called on the infrastructure tier again to store the late charges into the database.