title: Using DTO to transfer data between service layer and UI layer
body: <p>I've been trying to figure this out for days but there seems to be very little info on this particular subject with ASP.NET MVC. I've been Googling around for days and haven't really been able to figure anything out about this particular issue.</p>
  
  <p>I've got a 3 layer project. Business, DAL and UI/Web layer. In the DAL is dbcontext, repository and unit of work. In the business layer is a domain layer with all the interfaces and the EF models. In the business layer there is also a service layer with DTOs for the EF models and a generic repository service that accesses the repository. <a href="http://lh5.ggpht.com/_XHclJS9Jxg0/TKzwre5rfbI/AAAAAAAAALA/VoTXLxXS5pY/s1600-h/Drawing1%5B4%5D.jpg" rel="noreferrer">This</a> picture should help explain it.</p>
  
  <p>My problem is that i just can't seem to figure out how to use the DTOs to transfer data from the business layer.</p>
  
  <p>I've created service classes for the DTOs. I've got a ImageDTO and model and same for image anchors. I've created a service class for each DTO. So i've got a image service and anchor service. These services inherit the repository service and at the moment implement their own services. But thats about as far as i have gotten. Since these services have constructors that receive a IUnitOfWork interface via IoC i've pretty much gotten stranded.</p>
  
  <p>If i reference the service directly from the UI everything works as it should but i just can't get my mind around how to use DTOs to transmit data both from the service layer to the UI layer and the other way around.</p>
  
  <p>My service layer: </p>
  
  <p>Business/Services/DTOs</p>
  
  <pre><code>public class AnchorDto
  {
        public int Id { get; set; }
        public int x1 { get; set; }
        public int y1 { get; set; }
        public int x2 { get; set; }
        public int y2 { get; set; }
        public string description { get; set; }
        public int  imageId { get; set; }
        public int targetImageId { get; set; }
  
        public AnchorDto(int Id, int x1, int y1, int x2, int y2, string description, int imageId, int targetImageId)
        {
            // Just mapping input to the DTO 
        }
  }
  
  public class ImageDto
  {
      public int Id { get; set; }
      public string name { get; set; }
      public string title { get; set; }
      public string description { get; set; }
      public virtual IList&lt;AnchorDto&gt; anchors { get; set; }
  
      public ImageDto(int Id, string name, string title, string description, IList&lt;AnchorDto&gt; anchors )
      {
          // Just mapping input to DTO
      }
  }
  </code></pre>
  
  <p>Business/Services/Services</p>
  
  <pre><code>public class RepoService&lt;TEntity&gt; : IRepoService&lt;TEntity&gt; where TEntity : class
  {
      private IRepository&lt;TEntity&gt; repo;
  
      public RepoService(IUnitOfWork repo)
      {
          this.repo = repo.GetRepository&lt;TEntity&gt;();
      }
  
      public IEnumerable&lt;TEntity&gt; Get(
          Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null,
          Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
          string includeProperties = "")
          {
              return repo.Get(filter, orderBy, includeProperties);
          }
  
          public TEntity GetByID(object id)
          {
              return repo.GetByID(id);
          }
  
          public void Insert(TEntity entity)
          {
              repo.Insert(entity);
          }
  
          public void Delete(object id)
          {
              repo.Delete(id);
          }
  
          public void Delete(TEntity entityToDelete)
          {
              repo.Delete(entityToDelete);
          }
  
          public void Update(TEntity entityToUpdate)
          {
              repo.Update(entityToUpdate);
          }
      }
  </code></pre>
  
  <p>The Image Service, the IImageService interface is currently empty until i figure out what i need to implement.</p>
  
  <pre><code>public class ImageService : RepoService&lt;ImageModel&gt;, IImageService
  {
      public ImageService(IUnitOfWork repo)
          : base(repo)
      {
  
      }
  }
  </code></pre>
  
  <p>At the moment my controllers aren't really working and aren't using the service layer so i decided not to include any of those. I plan to map the DTOs to ViewModels using auto mapper once i've sorted this issue out.</p>
  
  <p>So now, please anyone knowledgeable enough to give me that idea I'm missing so that i can figure this out?</p>