jeudi 23 février 2017

Repositories design confusion

I have the following repositories:

  • EmployeeRepository
  • DocumentRepository
  • CourseRepository
  • LeaveRepository

We have a requirement now to add a new business object called BusinessTripBidding which allows employees to bid for different business trips and so. Anyway, The problem is when returning the list of Bidders I need to include information from the all above mentioned repositories as the business requires, and then I generate a new object and return a list (in my business object), something like this:

IEnumerable<BidderInfo> GetBiddersInfo(int tripId)
{
    List<Bidder> bidders = _bidderRepository.GetListByTripId(tripId);

    List<Course> courses = _courseRepository
         .GetListByEmployeeId(bidders.Select(b => b.EmployeeId).AsEnumerable());

    List<Document> passports = _documentRepository
         .GetListByEmployeeId(bidders.Select(b => b.EmployeeId).AsEnumerable(), DocumentType.Passport);

    List<Leave> leaves = ...........

    var biddersInfo = new List<BidderInfo>();
    foreach(Bidder b in bidders)
    {
        var bi = new BidderInfo();
        bi.Courses = courses.Where(c => c.EmployeeId == b.EmployeeId).ToList();
        bi.Passport = passports.FirstOrDefault(p => p.EmployeeId == b.EmployeeId);

        bi.ComingLeave = .........

        // the same for the rest of the repositories

        biddersInfo.Add(bi);
    }
    return biddersInfo;
}

Beside the multiple calls to the db, and beside the loop, it would be much easier if I create a new repository only responsible to create this BidderInfo in one single query, let's call it BidderInfoGeneratorRepository then inject this repository in the constructor of the business object.

Now, should I keep thing as I am doing currently (multiple db calls) but things look right; or should I create another repository and pass it to the business object to make things a bit faster? what is the best practice in this case?

Aucun commentaire:

Enregistrer un commentaire