title: Repository, UnitOfWork along with service provider, what is he right way to inject all these dependencies into my controller?
body: <p>I have an application that is written using c# on the top of asp.net MVC 5. I created a separate repository for each model in my app by implementing the following interface.</p>
  
  <pre><code>public interface IRepository&lt;TModel&gt;
          where TModel : class
  {
      TModel Get(int id);
      IEnumerable&lt;TModel&gt; GetAll();
      IQueryable&lt;TModel&gt; Find(Expression&lt;Func&lt;TModel, bool&gt;&gt; predicate);
      TModel SingleOrDefault(Expression&lt;Func&lt;TModel, bool&gt;&gt; predicate);
      TModel Add(TModel entity);
      IEnumerable&lt;TModel&gt; AddRange(IEnumerable&lt;TModel&gt; entities);
      void Remove(TModel entity);
      void RemoveRange(IEnumerable&lt;TModel&gt; entities);
      void Update(TModel entity);
  }
  </code></pre>
  
  <p>Although, I saw many implementation where each repository implement a Save method, i did not. I didn’t think it is a good practice where each repository save separately. Which is why I implemented a unit-of-work so that all of my repositories work as a cohesive unit. In my mind it should be the unit-of-work responsibility to handle the saving which enables transactions and the ability to rollback or commit changes when needed.</p>
  
  <p>When I implemented the repository pattern, I felt it would be a good idea to extend each repository as needed where I can have custom <code>IQueryable</code> object returned by the repository to reuse query logic and simple call that from my controller or other places like view helpers. So I could do something like this in my repository directly</p>
  
  <pre><code>public IQueryAble&lt;Car&gt; GetCarsByMakerId(int makerId)
  {
         // I have typically would have more logic here, but I stripped it out for simplicity.
         Return DbContext.Cars.Where(x =&gt; x.MakerId == makerId);
  }
  </code></pre>
  
  <p>Then I came to realize that in some cases, I need to write some logic which requires other repositories to handle the business needs. </p>
  
  <p>I came to realize that extending each repository logic with logic other than CRUD operation may not be a good idea.</p>
  
  <p>Instead, it sounds like I should add a service provider for each of my repositories where the service provider providers a method to wrap the business logic/rules. </p>
  
  <p>Using the above example, I would create a <code>CarServiceProvider</code> which accepts an instance of the unit-of-work. Then the service provider will utilize the injected unit-of-work to handle the business need which will allow me to reuse my business logic and separate that out of my repositories. Thus, limiting the repositories to only CRUD operation along with a new method I called it Query() which enables me to create a query.</p>
  
  <p>However, the issue with adding a service provider for each repositories means I have to inject a lot more classes into my controller’s constructor. So my controller constructor my need to look something like this</p>
  
  <pre><code>public class CarController
  {
      public CarController(IUnitOfWork unitOfWork, Ilog log, ICarServiceProvider car, ICarColorService color, ICarMakerService maker, ICarMakerService carMakerService, ICarSellerService seller, IUserServiceProvider user)
      {
          _unitOfWork = unitOfWork;
          _log = log;
          _customer = customer;
          _user = user;
          _car = car;
          _carColor = color;
          _carMaker = maker;
          _seller = seller;
      }
  
      public ActionResult Index()
      {
  
  
      }
  
      public ActionResult Create()
      {
          var viewModel = new CreateCarViewModel();
  
          // This will need to set the car colors, car maker, car sizes....
          viewModel.CreateForm(_carColor, _carMaker, _seller);
  
          return View(viewModel);
      }
  
      [HttpPost]
      public ActionResult Create(CreateCarViewModel viewModel)
      {
          if(ModelState.IsValid)
          {
              using(var batch = new _unitOfWork.StartBatch())
              {
                  try
                  {
                      var car = _car.AddNewCar(viewModel, _user.Id);
                      var sellers = car.AddSellers(viewModel.SellerIds);
  
                      batch.Commit();
  
                      return RedirectAction("Index");
                  } 
                  catch (Exception e)
                  {
                      batch.rollback();
                      ModelState.AddModelError(string.Empty, e.Message);
                      _log.AddError(e.Message);
                  }
              }
          }
  
          // At this point we know something went wrong, so we need to create the dropdown menus before we can render the view
          // This will need to set the car colors, car maker, car sizes....
          viewModel.CreateForm(_carColor, _carMaker, _seller);
  
          return View(viewModel);
      }
  }
  </code></pre>
  
  <p>As you can see now I am stuffing all these dependencies into my constructor to be able to use them which sounds like a new problem I have to work with or solve. The good news that I am using an <code>IoC</code> container which resolves all these dependencies for me.  But the fact that I have all these dependencies in the constructor is bothering me.</p>
  
  <p>My questions are</p>
  
  <ol>
  <li>Is it a good idea to take all of my custom logic out of the repositories and extract that into a separate service provider classes? </li>
  <li>Is injecting these services as needed one at a time is the way to go? Is there a better approach to make this process easy by creating a container for all of my services classes where I would inject one dependency instead of 3+?</li>
  </ol>