C
C#5mo ago
Natro

Repo pattern - is it okay to call one repo from multiple services?

Hi, pretty much the title summarizes it all. In one code base I am refactoring I am seeing a lot of calling from multiple services classes to one repository - most likely to bypass circular dependencies. Is it a good practice? Why yes? Why not? I usually try to keep calls between services to make sure business logic is checked at all times.
9 Replies
Pobiega
Pobiega5mo ago
No description
Pobiega
Pobiega5mo ago
As long as your repositories do only their intended job, which is to act upon an external data source, I see no problems with several services/command handlers/other business logic containers re-using the same repo. In fact, I'd be suprised if you didn't have this.
Natro
Natro5mo ago
What about if we have: OfficeService OfficeRepository EmployeeService EmployeeRepository and in EmployeeService we need to access something about OfficeService - e.g. get office by id? In this code base it's solved by just calling repo layer - OfficeRepository. But with this we are not checking some other logic in OfficeService - like "Can this user access this entity?"
mindhardt
mindhardt5mo ago
Worth noting: don't do repo pattern with EFCore (Entity Framework Core) In case if that's what you were doing
Pobiega
Pobiega5mo ago
And this is why I dislike the service+repo pattern - it very rarely matches up to your real dependencies therefor I much prefer command handlers, where you dont have to think "hm, does this fit best in X or Y service", its just its own thing. Regardless, in this actual case that you are asking about here, the answer is boring - it depends. specifically, it depends on if the logic of checking who can access the office etc is relevant to this specific action on your service
Natro
Natro5mo ago
yeah i wish we had ef core in place but codebase is 95% dapper and 5% ef core. Would be better to go with ef core for this project and obviously i would do no repo pattern with ef core this intrigued, do u have some example / pseudo code to explain it further? This could solve big concern I am dealing - I am also asking does it fit into A or B?
mindhardt
mindhardt5mo ago
GitHub
MO.DiscordBot/Application/Discord/Tags/RenameTagRequest.cs at maste...
Discord bot made with .NET 8 and vsa-like architecture - MindHardt/MO.DiscordBot
mindhardt
mindhardt5mo ago
Keep in mind that I have my own RequestHandler interfaces They are injected directly into a discord command action (similar fashion to asp.net controller action)
mindhardt
mindhardt5mo ago
GitHub
MO.DiscordBot/Bot/Commands/TagCommands.cs at 846f40d035b7de77d50520...
Discord bot made with .NET 8 and vsa-like architecture - MindHardt/MO.DiscordBot