title: Can I shape data into ViewModels in my Service Layer?
body: <p>I have a logic placement dilemma, but to begin here are my app layers:</p>
  
  <ol>
  <li>Presentation/UI Layer (Web API or MVC, really could be anything)</li>
  <li>Service Layer (this layer is lean overall and acts as a conductor for Domain Model and Repository layers)</li>
  <li>Domain Model (this layer is not anemic, and has both data and behavior in the model classes)</li>
  <li>Repository</li>
  </ol>
  
  <p>As it stands, the layers are designed well and flow is intuitive. However I have a requirement to shape the pure model data into several specific XML documents to be validated via a .xsd schema (model classes do <em>not</em> map 1:1 for XML document or I could just serialize object directly to XML). To me the model layer should have <em>no</em> understanding of being shaped or structured beyond it's current design; that's the responsibility of a higher layer.</p>
  
  <p>My 1st thought is to provide the shaping and logic to create these XML documents from the domain model data in the <em>Service</em> layer via ViewModel classes to be returned up a level. I use the term here 'ViewModel' to purely mean shaped data for a higher layer or presentation. However, everything I read says the Service layer <em>should not</em> contain ViewModels: <a href="https://stackoverflow.com/questions/3002168/should-a-service-layer-return-view-models-for-an-mvc-application">Should a service layer return view models for an MVC application?</a> and <a href="https://stackoverflow.com/questions/5053248/how-to-pass-complex-viewmodel-to-service-layer-in-asp-net-mvc">How to pass complex ViewModel to Service Layer in ASP.NET MVC?</a></p>
  
  <p>Ok follow this for a minute and make my MVC layer contain the ViewModels. I have a problem with this. The <em>whole reason</em> for having a Service layer, is the ability to act as a facade or entry point into the application, thus allowing multiple UI types (WinForms, WPF, WebForms, MVC, WCF, etc.). I <strong>need</strong> that XML shaping logic available to <em>any</em> UI I hook up to the Service layer. If I put it in the MVC/UI layer, it doesn't exist anymore if I hook up a new UI. </p>
  
  <p>Now I'm back to pushing the XML shaping classes back down to the Service Layer. I also don't think these classes should be on the domain layer as this is not business logic and indicates a format type: XML, which I do not want. Lastly, I can't introduce this logic in a vertical layer like an Infrastructure layer because now this layer has to understand my domain model and be carried around like luggage.</p>
  
  <p>According to the book <strong>Professional ASP.NET Design Patterns</strong>, the following is stated when using this architecture: <em>"The role of the service layer is to act as an entry point into the application; sometimes this is known as a facade. <strong>The service layer provides the presentation layer with a strongly typed view model</strong>, sometimes called the presentation model."</em>  I would like to place the shaped classes in my Services layer as this seems the best fit and I have seen examples of it done this way.</p>
  
  <p>Where does the logic belong? I want to take 1..n of my domain model classes, build a XML document using various data, and then allowing it to be available to the UI/Presentation layer? Thanks!</p>