JCii
JCii

Reputation: 327

Should I bring two entities together at the Service layer, the Repository layer, or make a repository for the join of the two?

I am developing a web app for a non-profit. (The app registers laborers for the day, tracks their personal info, takes work orders, and provides an interface to assign registered workers to a work order) I have about 8 tables in SQL Server. I started with Sanderson's book on MVC and attempted to implement the patterns that he recommends: IoC, separation of concerns, DI...

Here are my Context definitions:

    public DbSet<Person> Persons { get; set; }
    public DbSet<Worker> Workers { get; set; } //All workers are persons, but not all persons are workers.       
    public DbSet<WorkerSignin> WorkerSignins { get; set; } //1 entry per worker, per day (barcode scanner)
    public DbSet<Image> Images { get; set; }
    public DbSet<Employer> Employers { get; set; }  //1 employer has many work orders
    public DbSet<WorkOrder> WorkOrders { get; set; }  //1 work order has many work assignments
    public DbSet<WorkAssignment> WorkAssignments { get; set; } //1 assignment -> space for worker
    public DbSet<WorkerRequest> WorkerRequests { get; set; }
    public DbSet<Lookup> Lookups { get; set; }    //configuration, dropdowns, other stuff

The app has been in production for a few months--the non-profit has temps entering past orders and data for reporting purposes, so the record volume is growing rapidly. They're hitting some performance bottlenecks, and as I fix them, I'd like to fix them at the correct layer.

In multiple instances, I need to display information from multiple tables. Examples:

  1. The display of a Worker pulls first and last name from Person (using EF Collections)
  2. The display of a WorkerSignin pulls from Worker and Person
  3. The display of a WorkAssignment pulls from WorkOrder

So, in the case of #1, is it better to have my Worker service pull in the Person Service, or should the Worker Repository pull in the Person repository? Or, should I make a new Repository, like 'CompleteWorker' that combines information from both the Person and Worker table?

Upvotes: 2

Views: 1207

Answers (2)

marvelTracker
marvelTracker

Reputation: 4969

In side of your service Layer use many repositories build up logic with Service Layer. Keep your repository Layer clean. i.e Service A may have Repository A, Repository B, Repository C. Use dependency injection to inject repositories to your service.

public class ServiceA
{

 public PersonWorkerDTO GetPersonWorker()
 {
      RepositoryA.GetSomething();
      RepositoryB.GetSomething();
      RepositoryC.GetSomething();
 }

}

Upvotes: 0

Charles Graham
Charles Graham

Reputation: 1157

Your repositories should not have knowledge of any entity other than the entity they operate on. Personally I have found the best way to handle this sort of scenario is to have my repository expose a method or property that returns an IQueryable so that I can perform joins in my service layer.

So to answer your question, your Worker service would depend on both the Worker repository and the Person repository.

Alternatively are your database and model setup such that you could simply use a projection or the Include method to retrieve the specific data you are looking for?

Upvotes: 1

Related Questions