Paul Gielens:ThoughtsService

another Endpoint to my thoughts

News

Syndication

Ads


Favorites

Projects

Applying the Application Layer in Domain-Driven Design

Peter Veentjer published his second post on Domain-Driven Design in which he questions the need for a Service Layer. First, the Service Layer is what Eric Evans calls the Application Layer. Let’s see what Eric has to say about this particular layer in his excellent book Domain-Driven Design.

Application Layer [his name for Service Layer]: Defines the jobs the software is supposed to do and directs the expressive domain objects to work out problems. The tasks this layer is responsible for are meaningful to the business or necessary for interaction with the application layers of other systems. This layer is kept thin. It does not contain business rules or knowledge, but only coordinates tasks and delegates work to collaborations of domain objects in the next layer down. It does not have state reflecting the business situation, but it can have state that reflects the progress of a task for the user or the program.

According to Peter the Application Layer is still very useful for security, transactions, decoupling and as a facility to store application logic. I’m not sure about the security and transactions in the service layer since it leads to the problem of not isolating the domain. The all seeing eye Frans Bouma responded with:

How can the Application Layer coordinate tasks and delegate work if it doesn’t contain business rules?

Interesting question!

Let’s concentrate on DDD and the patterns that play a role in this discussion: Application Layer, Domain Layer, Infrastructure Layer, Application Service, Domain Service, and Infrastructure Service. With these patterns in mind I tend to agree with Eric that it is straightforward to identity Infrastructure Services, but significantly harder to distinguish Application from Domain services. What I find a big help in separating the apples from the oranges is thinking in terms of application workflow. All logic concerning the application workflow typically end up being Application Services factored into the Application Layer, whereas concepts from the domain that don’t seem to fit as model objects end up forming one or more Domain Services. Otherwise you’ll be definitely messing up a real model object which, in my opinion, has been done in Peter’s example. Human Resource Management fires people, since they are the ones that know the details about the “firing policy”, that is, assuming I am the domain expert in this case. To capture this important domain concept I would introduce the HrmService, Domain Service in the Domain Layer. This way the Application Layer does not contain any domain/business related logic and focuses on the application logic only.

Now let’s see what Jimmy Nilsson has to say about the Application Layer in his more down to earth book Applying Domain-Driven Design and Patterns.

The Application Layer isn’t mandatory; it is only there if it really adds value. Since my Domain Layer is so much richer than before, it’s often not interesting with the Application Layer.

Right on the spot! So, the question remains: when do we need an Application Layer and Application Services? Imagine we need to build an application on top of our Domain Model using the HrmService, Domain Service to accept an employee-firing request from this big SAP system. It requires the HrmService to determine whether it’s possible to fire the given employee by interacting with the necessary Employee, EmployeeFile, ContractFile and FirePolicy objects. We also need to communicate the advice by sending e-mails, letters and other communications for which we use the Infrastructure Layer. We now have captured a small part of the application workflow which fits nicely inside the Application Layer. Now part of the Application Layer’s responsibility is to digest the employee-firing request and send a message to the HrmService, Domain Service for fulfillment. It then decides to send notifications to the Infrastructure Service.

It’s important to note that the domain layer isn’t being distracted from representing the important concepts of the business, a healthy heart I'd say!

Comments

Udi Dahan - The Software Simplist said:

What I find important about a service layer is that it encapsulates how external parties interact with "the system". In an SOA, where a system needs to respond to messages, the message handler classes belong in the service layer. They are responsible for taking the results of the handling of a message, building one or more "response" messages, and sending them back to the requester, or possibly publishing them to many subscribers.

If workflow is a large part of your logic, it too should be encapsulated. I'm referring to the application layer here, but I prefer to call it the workflow layer. In this layer, we'd have all the rules that the rules engine runs. If you don't use a rules engine, there is one more case that I've found the workflow layer useful for. And that is to manage long running workflows.

In any and all cases, nothing is taken away from the domain layer.
# June 1, 2006 2:14 AM

Dennis van der Stelt said:

I haven't been reading up on my RSS Feeds, and this seems like an interesting discussion I have to trackback to its root! :)

Anyway, as far as I can tell, I totally agree with you here Paul.

Udi Dahan calls it workflow layer and I have to agree I find that name (or process layer) much easier to use in discussions.

# June 30, 2006 2:50 AM

Paul Gielens:ThoughtsService said:

Apparently I inspired Udi (Microsoft Solutions Architect MVP)with my post on “Applying the application

# July 6, 2006 4:52 PM

Teddy said:

For an application logic itself, IF NECCESSARY, we can use an application layer to separate maybe transactions, or so called application logics.

But, for architecture view, a service layer (really same as application layer?) like contracts of services, by which, external or local comsumers (ready for SOA) may use them. They can decouple the service implementaion from the service interfaces, which means in this kind of service interface definitions, only common data types or public shared data types should be used so that even external comsumer without any knowledge of your service implementaion can use them.

# July 31, 2006 10:00 AM

Paul Gielens:ThoughtsService said:

Recent activity on the Domain-Driven Design user group made me decide to sharethis writing dating November

# September 3, 2006 6:07 AM

Kevin Warner said:

I know this is a late comment, but I want to say that this post helped me tremendously with figuring out how to identify when to use an Application Service. My example is this (simple I know, but useful to illustrate):

I have a web portal that allows different types of users to log in and carry out some admin activities. If a user forgets his/her password, they can ask to have it reset. This involves the following steps...

- Find the user account by email. (using a repository)

- Make sure the account is eligible to have the password reset. (rule that can be checked within the account object.)

- Generate a new password and assign it to the account. (static method on account class)

- Change the account's status to "Pending", which will require them to change their password before being granted access. (using account object)

- Persist changes to the account. (using repository)

- Send an email to the user with the new password and instructions. (using an email service)

So, you can see that the process involves domain repositories, domain entities, and an infrastructure service (for emailing). Also, I want to make sure all of these go off without a problem (so now we have a transaction scope.)

Finally, we have multiple places in our application where this process can be carried out. It can be requested by the user, it can be initiated by an admin within the user management screens, or it can be initiated through a web service call.

In previous days, I would have added a method to a all-encompassing "AccountManager" class that reset the password. After reading DDD and lots of posts online, I am leaning toward an "Application Service" called ResetPasswordService, which handles and coordinates these activities.

Anyway, I would be interested in hearing comments on my thought process and to see if I am on track with this.

Thanks!

Kevin

# July 7, 2007 1:00 PM

cockiest said:

to make Kevin Warner's example more domain driven, i think the policies to reset password should be encapsulated into the Account class resetPassword method.

ResetPasswordService is unnecessary because we ONLY reset password of Account. we will not reset password of Door, Safe, Device, etc using the same ResetPasswordService (which was designed for account).

anyway it's just my opinion.

# July 18, 2007 1:11 AM

Jimit said:

Hi all,

I have been following this thread with interest. Tying in to the debate on the viability of the service layer and whether or not one should have one at all and if one does wheither it should have business rules, I'd like your thoughts on the following scenario. I am currently designing a BPM/SOA application using workflow services. The workflows contain the application logic and the business rules engine, relying on domain objects for implementation which in turn communicate with repositories that encapsulate the persistence logic. I'm using DTOs to pass data between tiers. Does this design violate the tenets of DDD? Does removing the business rules from the domain layer make my domain anaemic?

# February 10, 2008 6:53 PM

Bob said:

In Cockiest's approach, I'm wondering if we put the resetPassword logic on Account (domain model), how can we put the logic to send the email? We obviously don't want to put infrastrucutre logic in Account object.

And by the way, in kevins example, is that application service or domain service?

# July 27, 2008 6:30 PM

Bob (Again) said:

Is repository considered as service? Domain service or application service? And is it sensible for model to *talk* to services?

# July 27, 2008 6:36 PM

Bob (Again) said:

I have seen approaches that, in the example of that employee-firing, the domain service is called from the domain entity. E.g. employee.Fire() would call the EmployeeFiringService to coordinate the firing logic across different domain entities and infrastructure layer. Is it a correct approach? Or it's preferable to rather have one-way service->domain relationship?

# July 27, 2008 6:48 PM

What purpose does the Repository Pattern have? « Cav’s Weblog said:

Pingback from  What purpose does the Repository Pattern have? « Cav’s Weblog

# October 22, 2008 3:49 PM

Cosmetic Surgery said:

Would you get a <a href="http://www.surgery.us.com">cosmetic surgery</a>

# December 11, 2008 3:17 AM

chexyDede said:

Hi!

My name is Jessika!

# December 12, 2008 3:38 PM

Breast Augmentation said:

Excellent site!  Stopping by to show you <a href="http://www.breastaugmentationsource.net">breast augmentation</a> what it is!

# December 13, 2008 5:26 PM

Soaspidioff said:

Bite my shiny metal ass, assholes, [url=http://justnkldkhsjvd.com/]you were joked![/url]

# January 6, 2009 1:50 PM

Liposuction said:

 How is that relevant?

# January 6, 2009 4:47 PM

Temsaismicext said:

fgabftgegefdrfwsihppyijkouoee.com fgabftgegefdrfwsihppyijkouoee

<a href="fgabftgegefdrfwsihppyijkouoee.com ">fgabftgegefdrfwsihppyijkouoee</a>

# February 5, 2009 5:52 PM

icejobjob said:

大変ですよね、<a href="www.pwblog.com/.../a>

就職氷河期、それも超就職氷河期{と言われる時代に突入しようとしています。

この、就職氷河期は並大抵の努力では乗り切ることが困難と言われています。

{米国、アメリカのサブプライムを皮切りに引き起こされた|世界同時不況とも言われている}今回の不況、それこそ数年間にわたって、世界経済に{影響を与えます。|インパクトを与え続けるでしょう。}

この時期の、今のあなたが直面している就職活動というイベントは、あなたの人生、就職人生の大きなウエイトを占めることとなります。

{昨年までの就職活動状況とは180度転換した|就職活動は、技術と知識で乗り切ることが出来ます。}就職氷河期、絶対に後悔しないように全力で戦いましょう。

# February 8, 2009 3:09 PM

AnewFoess said:

Hi  I've been watching this forum for awhile; Decided to introduce myself=)

# August 20, 2009 5:52 PM

Richard Dingwall » I can’t decide if this is an application service, or a domain service! said:

Pingback from  Richard Dingwall  &raquo; I can&#8217;t decide if this is an application service, or a domain service!

# September 6, 2009 12:06 AM

EuPays said:

Your install rates:

Italy: 0,20

Spain: 0,20

France: 0,60

Germany: 0,80

UK: 0,80

CH: 0,20

US: 1,00

CA: 0,80

NL: 0,60

InviteCode: eupays.com/signup.php

# September 7, 2009 9:33 PM

PatShelby said:

Hey, I found your blog in a new directory of blogs. I dont know how your blog came up, must have been a typo, anyway cool blog, I bookmarked you.  :) <A HREF="forums.portlandmercury.com/member.php :) </A>

# October 7, 2009 5:28 PM

Milo Hyson said:

It seems to me the question as to where certain functionality should go (application vs domain layer) is not necessarily a technical one, at least not exclusively. In your HRM example, we see that the process of firing someone includes sending out various notices. Is this always the case or just when using that particular application? The way you've done it, with the application triggering the notices, suggests the latter. Whether or not that implication is correct I would think can only be answered by consulting the business.

# October 19, 2009 6:52 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)