I have to refactor an application that has a lot of services that depend on each other.
The problem I face is that all services use entities as if they were simple objects like this:
var lst = _context.chapters.Include(some condition).where(some condition).ToList();
foreach(var i in lst)
i.somearray.removeall(some condition);
return _mapper.Map<datatype>(lst)
Now another service calls that method to get that data and do something completely unrelated to the chapter table.(saving a status) What happens the moment that other service calls _context.SaveChanges()? Basically a wipe of half the database and the other half is inconsistent.
What would be the best practice to refactor services like that? And what would be the most elegant solution that allows me to avoid rewriting all of them?
For now im just refreshing everything in the change tracker at critical points, but thats not very reliable and takes about 2 Minutes to complete. Setting the db context to transient doesnt work as some services seem to depend on the scoped context.
I think about creating dtos for every service and mapping them before they are altered. But its a massive application. That could take days.