title: DDD - communication between the presentation and domain layer
body: <p>I am having trouble connecting the dots from many posts I have read (and coming to realize I probably need to buy Eric Evans book instead of relying on loads of internet material) I am trying to adhere to domain driven design, but I am having trouble on the best method to communication from the presentation layer to my domain before saving my data back to the database. To keep it simple, right now I have a user class that can have a list of jobs. When a user opens the application their active directory information is queried and this class is populated with their data as long as they have the app open. Then, the user can edit an existing job if they own it or create a new one. I started creating a service class that uses my UnitOfWork class thinking this would act as my communication, but that is where I am stuck.</p>
  
  <p>Current Setup:</p>
  
  <p>DAL - EF 6 generated POCOs and DbContext, Repository, Unit of Work</p>
  
  <p>Domain - Entities, Repository &amp; Unit of Work interface, domain interface services I refer to in this post?</p>
  
  <p>Presentation - MVC (intranet), concrete service?</p>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>Is the service class the best class to implement for this type of communication (and for creating new instances of my domain classes (e.g. a method to create a new job)? I realize I could use a factory pattern, but I did not want to get too complicated yet)? Would the service reside in the domain layer or do I create a separate project for an application layer? Then would the interface go in the domain layer and the concrete implementation go into the application layer? I feel like that could be over complicating the app. Also, is the service WCF or can I just create my own classes?</p></li>
  <li><p>How do I map the ViewModel in my MVC layer back to the domain using this service (if that is the best way) without the presentation layer leaking in to the service? I've read up on DTOs, but then is that overkill in the service layer? Or is it okay to expose my domain entities as the ViewModel? I must be thinking about this incorrectly. I cannot picture how this interaction would look in a controller without some leakage. Sorry for all the questions, and thanks.</p>
  
  <pre><code>public class User
  {
      public int Id{ get; set; }
      public string WindowsId{ get; private set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string FullName { get { return FirstName + " " + LastName; } }
      public string Email { get; set; }
      public string WorkPhone { get; set; }
  
      public List&lt;Job&gt; Jobs { get; private set; }
  
      public void AddJob(Job job)
      {
          if(job == null)
          throw new Exception("Job is null");
  
          var newJob = new Job(this) //tell the job that this user is creating the job
          {
              Description = job.Description,
              DueDate = job.DueDate,
              Name = job.Name,
              Reason = job.Reason
          };
  
      Jobs.Add(newJob);
      }
  
  }
  </code></pre></li>
  </ol>
  
  <p>UPDATE: My Solution</p>
  
  <p>I ended up taking a little bit from everybody's answers. </p>
  
  <p>@granadaCoder - I used my EF POCOs as the classes to pass around as DTOs because most of the time that is the data I was displaying to the user in my ViewModel. So it allowed me to use the DTOs as the ViewModels</p>
  
  <p>@ChrisPratt - you are correct. Doing this prevented a lot of extra work. All I did was create a service that had all the queries I needed. And if I ever had to change the EF it would still not bother my other layers. All I would have to do is create the repository and UOW</p>
  
  <p>@MilivojMilani - I did have repeating logic in the controller so the service layer kept me aligned with the DRY principal</p>
  
  <p>@Yorro - I used your points and references to reinforce the design since I was still unsure about the service layer setup. Since this is a smaller project, I setup another folder with my service and interface that I created, no WCF</p>