samedi 28 mai 2016

Should the Repository pattern manipulate an object before passing it to the consumer/service layer?

The current solution I am working with is structured with a single Core layer that is connecting to the database whenever it needs to retrieve data.

I suggested to a few colleagues that we create a DAL layer and move all of the database logic into it's own project and call it

Application.DataAccess

This layer will use Entity Framework and the repository pattern as an abstraction between Application.Core and Application.DataAccess (The Repository implementation will sit within Application.DataAccess).

I plan to to consume the repository in a service layer.

public class JobService
{
   private IJobRepository _jobRepository;

   public JobService(IJobRepository repository) 
   {
      _jobRepository = repository;
   }

   public IEnumerable<Job> GetJobs() 
   {
      List<Jobs_AllJobs> allJobs = _jobRepository.GetAllJobs();
      List<Job> result = new List<Job>();

      foreach(job in allJobs) 
      {
        result.Add(new Jobs() { Title = job.Title, OtherEntity = "Internal to the service"; });
      }          

      return result;
  }
}

Job Repository:

public class JobRepository : IRepository<Jobs_AllJobs>
{
     DBContext _jobContext;

     public List<Jobs_AllJobs> GetJobs() 
     {
         return _jobContext.Jobs_AllJobs.ToList();
     }
}

This is where it gets slightly heated between us.

I believe that the JobRepository should return a list of the database entity Jobs_AllJobs rather than manipulate the database entity and construct a new List of Job (This should be handled in the service layer). My colleagues believe that the manipulation should be done at the Repository level and that the JobRepository should pass back a List<Job> or IEnumerable<Job> rather than the database entity.

From my understanding the Repository pattern is meant abstract away the internal database implementation (Entity Framework/nHibernate/File System) and solely perform a CRUD operation upon database (Create, Retrieve, Update or Delete) and that the JobService should perform the manipulation and construct a new object and pass that back down to the consumer.

Which way would be correct? And which way follows the repository pattern the best?

Aucun commentaire:

Enregistrer un commentaire