Drawing the Line Between MVC Controllers and Application Services

Frequently, when we adapt new technologies or techniques, the new fix ends up introducing new "yucky" stuff or simply pushes the previous yucky stuff to another area.  Since "yucky" just might be a bit subjective here, I consider yucky stuff to include any bit of code that has no clear home or which is overly complicated for the sake of separtion of concerns.  If you encounter code such as this, it doesn't necessarily mean that you have a weak understanding of the domain or task at hand, but it may be a sign that you have a grey area between application layers which needs to be better defined.

These grey areas are frequently found between where the presentation layer ends and the controller and/or business logic begins.  For example, when we moved from Active Server Pages to ASP.NET, we liberated our views of business logic only to find our newly embraced code-behinds to be a bear to maintain as they intertwined abstracted presentation concerns with logic.  Adding a pattern such as M V P certainly made it all testable but then moved much of the "yucky" stuff to either the presenter class or added new yucky stuff in the form of complicated event communications and/or fudgy controller logic between the code-behind and presenter classes.  (If you've used MVP in a major project, you'll likely know what I'm talking about.)  So now with the dawn of ASP.NET MVC with a DDD approach (nicely encapsulated within S#arp Architecture, of course), our concerns are over, right?  Well, not exactly...we now have a new yucky bit to deal with:  the grey area between the controllers and application services (not to be confused with domain services) .  This is exactly the grey area that I've struggled with since adopting ASP.NET MVC with an eye for DDD.  Where exactly do we draw the line between what goes into controllers and what goes into application services?

Joe Wilson recently posted a review on S#arp Architecture and had a useful critique of the framework which IMO provides a very clear explanation of where the line should be drawn between the MVC controller and DDD application service layers:

I've borrowed a metaphor from the SQL Server installer that talk about "surface area". I think of the MyProject.ApplicationServices layer as a wrapper around all the app logic. The methods in this layer are the surface area of my application, and they encapsulates all the implementation code my domain is dealing with. Calls into this layer can kick off multi-step processes, handle workflow, and usually have long, explicit method names. But this is the only part of my domain's surface area that is exposed externally.

To put it another way, if you're putting any logic into your controller class that you would have to copy and paste, almost verbatim, into, e.g., a WCF service class to replicate the same sort of behavior, but for a different client interface (e.g., WCF vs. MVC), then it's likely a sign that your controller is doing too much and that the code may instead belong in an application service.  In a different light, if you're putting anything into the application service layer which would preclude a different client type (e.g., WCF vs. MVC) to leverage it, then you may be introducing controller concerns into the application services.  Following these rules of thumb, your controllers remain more appropriately paired with the web tier of your application while your application services provide the true gateway to the rest of the application.  Keeping a close watch on the application service layer's "surface area" (or more appropriately stated, bounded context), and making sure it doesn't leak into the controller layer or vice versa, is a good approach for drawing a firm line between these two layers and keeping the yucky stuff at bay.

Billy McCafferty
http://www.itsamuraischool.com


Posted 09-16-2009 11:29 AM by Billy McCafferty

[Advertisement]

Comments

9eFish wrote Drawing the Line Between MVC Controllers and Application Services - Billy McCafferty - Devlicio.us
on 09-16-2009 10:45 PM

9efish.感谢你的文章 - Trackback from 9eFish

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - September 16-18, 2009
on 09-17-2009 4:41 AM

Daily tech links for .net and related technologies - September 16-18, 2009 Web Development Reports in

James Broome wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-17-2009 8:18 AM

Hey Billy,

We've been using S# Arch for the last 6 months and have been having this conversation on and off for the whole time amongst our dev team. We basically came to the same conclusion as you have here. One thing that might help people is an idea borrowed from JP Boodhoo, which is renaming the App Services layer to the Tasks Layer.  A simple and subtle change, but removes the confusion over the difference between App Services and Domain Services. DDD is all about language, and we found that as soon as we started talking about our App Services layer as a Tasks Layer, then things seemed to start to make more sense. The Tasks Layer can be used by any calling client to perform checkout related operations, the controllers being the first client to use this layer. We now have e.g. a CheckoutTasks which exposes all the checkout related functionality that the CheckoutController needs.

Cheers

James

Billy McCafferty wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-17-2009 11:20 AM

@James, that's a great idea.  "Application services" is just about the most vague name you could call a layer and likely part of the source of frustration in determining exactly what it does. ;)

lilikindsli wrote re: Drawing the Line Between MVC Controllers and Application Services
on 10-01-2009 8:00 AM

aD9H6o I want to say - thank you for this!

#.think.in wrote #.think.in infoDose #43 (11th September - 22nd September)
on 10-02-2009 9:43 AM

#.think.in infoDose #43 (11th September - 22nd September)

Globals wrote re: Drawing the Line Between MVC Controllers and Application Services
on 10-03-2009 7:18 AM

all good things

thrag wrote re: Drawing the Line Between MVC Controllers and Application Services
on 10-17-2009 1:24 PM

Could you comment on how well suited  S#arp Architecture is to writing Desktop apps as well s web apps.  Are there any reference apps or tutorials for WPF?

Thanks,  it looks like a note architecure I'm looking forward to giving it a try.

Billy McCafferty wrote re: Drawing the Line Between MVC Controllers and Application Services
on 10-19-2009 1:06 PM

thrag, the S#arp Architecture Contrib project has been taking strides to bridging this gap with WinForm apps and WPF.  You can read more about the Contrib project at wiki.sharparchitecture.net/SharpArchContrib.ashx

Pharme206 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 04-23-2010 8:19 PM

Hello! ddbdfkd interesting ddbdfkd site!

Pharmb428 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 04-23-2010 9:12 PM

Hello! cdaeefa interesting cdaeefa site!

Pharmf505 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 05-09-2010 3:12 PM

Hello! bkeggdb interesting bkeggdb site!

Pharma191 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 05-21-2010 10:43 PM

Hello! adkdeek interesting adkdeek site!

EAWYRN wrote re: Drawing the Line Between MVC Controllers and Application Services
on 06-19-2010 8:05 AM

DYSpTR

Pharmb389 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 07-02-2010 2:14 AM

Hello! dccfbkd interesting dccfbkd site!

Pharmc206 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 07-02-2010 2:30 AM

Hello! egceedg interesting egceedg site!

buy cheap viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 07-07-2010 7:53 AM

buy cheap viagra online

Pharmg403 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-04-2010 6:12 AM

Hello! bcfccga interesting bcfccga site!

Pharmg63 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-04-2010 6:49 AM

Hello! ddkbdfe interesting ddkbdfe site!

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-06-2010 11:53 AM

best no deposit bonus casino

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-06-2010 12:27 PM

instant no deposit casino bonus

online casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-08-2010 4:09 AM

no deposit casino forum

buy cheap viagra online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-08-2010 1:58 PM

online cheap viagra buy

cheap viagra tablets wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-08-2010 2:16 PM

cheap drugs viagra cialas

buy cheap viagra online uk wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-08-2010 2:34 PM

cheap viagra online without prescription

viagra cheap wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-08-2010 2:51 PM

cheap viagra search generic

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 4:42 AM

silver oak casino no deposit bonus codes

casino online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 5:00 AM

casino download

casino online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 5:17 AM

online bingo play

casino online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 5:34 AM

no deposit casino coupon codes

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 6:16 AM

casino no deposit code

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 6:46 AM

rtg casino no deposit bonus

casino online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 7:16 AM

online casino no deposit bonus codes

casino online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 7:45 AM

new online no deposit casino

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 8:15 AM

online slots bonuses

no deposit casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 8:44 AM

free no deposit online casino bonuses

online casino wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 9:14 AM

play slot game

camaras de seguridad ip wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-09-2010 9:47 PM

camaras y equipos de seguridad

buy cheap viagra online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 5:03 AM

cheap generic viagra deals

cheap generic viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 6:03 AM

cheap viagra in india

buy cheap viagra online uk wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 6:34 AM

kamagra cheap viagra

viagra uk cheap purchase buy wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 7:04 AM

viagra cheap free shipping

buy cheap viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 7:34 AM

cheap soft viagra

viagra uk cheap purchase buy wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 8:34 AM

cheap inexpensive viagra

viagra cheap wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 9:04 AM

viagra generic cheap

buy cheap viagra online uk wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 10:04 AM

where can you buy viagra cheap

viagra online cheap wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-10-2010 10:34 AM

cheap foreign generic viagra

buy viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-13-2010 2:36 PM

viagra to buy cheap

buy viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-13-2010 3:08 PM

cheap generic india viagra

buy viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-13-2010 4:09 PM

viagra cheap price

viagra buy wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 12:31 PM

cheap kamagra generic viagra videos

buy viagra meds online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 12:48 PM

cheap viagra search generic

buy viagra meds online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 1:05 PM

viagra cheap express

buy viagra online at wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 1:23 PM

kamagra cheap viagra

buy viagra online wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 1:40 PM

target google viagra order cheap

buy viagra online at wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 1:58 PM

cheap viagra prices

buy viagra cheap wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 2:16 PM

cheap pfizer viagra

how to buy viagra wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 2:33 PM

viagra find buy cheap search generic

viagra buy wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 2:51 PM

cheap online viagra

buy viagra cheap wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-19-2010 3:08 PM

target google viagra order cheap

knee pain teenager wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 8:49 AM

knee pain when jumping

reduce knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 9:07 AM

knee pain sleep

exercise with knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 9:25 AM

fix knee pain

right knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 9:42 AM

knee pain ligaments

knee pain after wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 10:00 AM

pediatric knee pain

knee pain bursitis wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 10:18 AM

knee pain while biking

how to treat knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 10:36 AM

knee pain and popping

grinding knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 10:54 AM

lupus knee pain

knee pain above patella wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 11:11 AM

knee pain from sitting

behind knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 11:29 AM

joint and knee pain

knee pain band wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 11:47 AM

how to diagnose knee pain

sudden knee pain causes wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 12:05 PM

sever knee pain

back of knee pain after running wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 12:23 PM

sharp knee pain

knee pain pressure wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 12:41 PM

hyperextended knee pain

knee pain and burning wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 12:59 PM

alcohol knee pain

managing knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 1:16 PM

golf knee pain

knee pain left side wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 1:34 PM

knee pain popping sound

causes of knee pain and stiffness wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 1:51 PM

rear knee pain

golf knee pain wrote re: Drawing the Line Between MVC Controllers and Application Services
on 09-20-2010 2:09 PM

knee pain during exercise

Pharmg171 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 10-23-2010 9:28 AM

Hello! dbdddcc interesting dbdddcc site!

Pharme80 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 11-10-2010 2:04 PM

Hello! dekadeg interesting dekadeg site!

Pharmd716 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 11-14-2010 9:56 PM

Hello! bkeddae interesting bkeddae site!

Pharmb675 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 11-26-2010 3:39 AM

Hello! ekddfed interesting ekddfed site!

Pharmb546 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 12-10-2010 5:51 PM

Hello! dggkbdf interesting dggkbdf site!

Pharmd848 wrote re: Drawing the Line Between MVC Controllers and Application Services
on 12-16-2010 6:41 PM

Hello! edeekdd interesting edeekdd site!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)