title: How can I prevent a class from being injected into other classes via dependency injection?
body: <p>I would like to prevent other developers from deviating from the application's architecture by limiting the classes in which the UnitOfWork class can be injected. Or at least make it more obvious that they're deviating from the accepted pattern.</p>
  <p>We have a collection of services that all extend the abstract class of BaseService. The BaseService class contains the property for the UnitOfWork class which is injected via dependency injection.</p>
  <pre><code>public abstract class BaseService
  {
      protected readonly IUnitOfWork UnitOfWork;
  
      protected BaseService(IUnitOfWork unitOfWork)
      {
          UnitOfWork = unitOfWork;
      }
  }
  </code></pre>
  <p>This means if a class needs the UnitOfWork it'll need to extend the BaseService class, but nothing actually prevents a developer from just injecting the UnitOfWork into some other class that's not a service. If they did this, they could potentially be saving changes outside of the service, which is what we're trying to avoid.</p>
  <p>Is there a way that we can throw a build error or warning based on the UnitOfWork being injected in some other class?</p>