title: Registering IUnitOfWork As service in .net core
body: <p>I am going to implement repository pattern in my asp.net core mvc application , for that i am trying my hands on a simple demo application which include repository and Unit of Work concept.
  <strong>My First Repository</strong></p>
  
  <pre><code>public  interface ICustomerRepository
  {
      bool Add();
  
      bool Update();
  
      bool Delete();
  
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code> public class CustomerRepository:ICustomerRepository
  {
      public bool Add()
      {
          return true;
      }
  
      public bool Update()
      {
          return true;
      }
  
      public bool Delete()
      {
          return true;
      }
  
  }
  </code></pre>
  
  <p><strong>Second Repository</strong></p>
  
  <pre><code>public interface IOrderRepository
      {
          bool Add();
  
          bool Update();
  
          bool Delete();
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class OrderRepository:IOrderRepository
      {
          public bool Add()
          {
              return true;
          }
  
          public bool Update()
          {
              return true;
          }
  
          public bool Delete()
          {
              return true;
          }
      }
  </code></pre>
  
  <p><strong>IUnit Of Work</strong></p>
  
  <pre><code>public interface IUnitOfWork
  {
      IOrderRepository Order {get;}
  
      ICustomerRepository Customer { get; }
  
      void Save();
      void Cancel();
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class UnitOfWork:IUnitOfWork
  {
      public UnitOfWork(IOrderRepository order, ICustomerRepository customer)
      {
          Order = order;
          Customer = customer;
      }
  
      public  IOrderRepository Order { get; }
  
      public  ICustomerRepository Customer { get; }
      public void Save() { }
      public void Cancel() { }
  }
  </code></pre>
  
  <p>And in my controller ,</p>
  
  <pre><code>   public class HomeController : Controller
      {
          IUnitOfWork UW { get; }
  
          public HomeController(IUnitOfWork uw)
          {
              UW = uw;
          }
  
          public IActionResult Index()
          {
              UW.Customer.Add();
              UW.Order.Update();
              UW.Save();
  
              return View();
          }
  }
  </code></pre>
  
  <p>I will add more code later for dapper , but at least it should work wiyhout any error , but it give me error</p>
  
  <blockquote>
    <p>InvalidOperationException: Unable to resolve service for type 'CoreTS.Repository.UnitOfWork.IUnitOfWork' while attempting to activate 'CoreTS.Controllers.HomeController'.</p>
  </blockquote>
  
  <p>Someone suggested me to add IUnitOfWork as service in startup.cs under ConfigureService Method, as </p>
  
  <pre><code>services.AddSingleton&lt;IUnitOfWork, UnitOfWork&gt;();
  </code></pre>
  
  <p>And After Adding this another error</p>
  
  <blockquote>
    <p>InvalidOperationException: Unable to resolve service for type 'CoreTS.Repository.Order.IOrderRepository' while attempting to activate 'CoreTS.Repository.UnitOfWork.UnitOfWork'.</p>
  </blockquote>
  
  <p>To make it work i had to add other two repository also in startup.cs also </p>
  
  <pre><code>services.AddSingleton&lt;IOrderRepository, OrderRepository&gt;();
  services.AddSingleton&lt;ICustomerRepository, CustomerRepository&gt;();
  </code></pre>
  
  <p>If there going to be n number of repository than i have to add everyone of them in startup.cs (according to this code ), what is the solution for that.</p>
  
  <p>So </p>
  
  <p>1.] What does these errors means ?</p>
  
  <p>2.] What will be the correct configuration here ? </p>
  
  <p>3.] What is the way to not to add n number of repository as service here ?</p>
  
  <p>Note: As a mentioned already , this is just to understand the flow of pattern , i will add code for Dapper or EF Core later in this </p>