Unit Of Work with Harbin.DataAccess generic repository
Your library is exactly what I have been looking for! Do you have an example utilizing the UOW pattern?
Well, I'm not much familiar with (or a fan of) the UoW pattern, and being honest my first thought would be using Entity Framework which would automatically keep track of all modified entities and can save them all together at once.
The first thing to note is that Harbin Generic Repositories do NOT emulate a collection of objects (as it was described by Eric Evans in "Domain-Driven Design" book), but rather it's just "persistence-based repository" (as described by Vaughn Vernon in "Implementing Domain-Driven Design" book).
Having said that, you could just combine your multiple DbCommands in a single ADO.NET transaction - see example in the https://github.com/Drizin/Harbin/tree/main/src/Harbin.DataAccess. I think that if there are no long operations running between the DbCommands this should work fine (we can leave the transaction open until all different commands finish).
On the other hand, if you really need to keep objects in memory and then just persist at the end, you would have to create your own UoW class. I imagine something like:
- Keeping track of inserted entities, deleted entities, updated entities (maybe multiple entity types?)
- Some error flag (or list of errors) to signal when some error happened
- Commit method would run the inserts, deletes, updates. If using multiple entity types you'd have to figure out (or register) dependency order.
- Commit code would skip/return if some error is flagged.
- Calling code could explicitly invoke Commit in case it needs to read data (like getting created identity key)
- If calling code didn't explicitly invoke you could automatically invoke it during UoW Dispose()
- For a web app you would inject it per-request
Feel free to create a PR if you implement it.