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.
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.
public abstract class BaseService
{
protected readonly IUnitOfWork UnitOfWork;
protected BaseService(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
}
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.
Is there a way that we can throw a build error or warning based on the UnitOfWork being injected in some other class?