upizs / PestControl

Self-made bug tracker for personal use. Implementing ASP.NET Core .NET 5 with razor pages, Entity Framework 5.0.9 and AspNetCore Identity 2.2.0
7 stars 0 forks source link

Unit of Work Pattern #5

Open Nobonex opened 2 years ago

Nobonex commented 2 years ago

I'm not sure how much knowledge you have of patterns (I only remember I got here via a Reddit post some time ago xD). It might be a fun thing to try and implement a Unit of Work pattern.

The Unit of Work can be used to push all the changes made to the Context to the DB at once, or to cancel the whole operation. This way you have more control over when the changes get applied and more importantly, introduce a safer approach to communicate with the DB.

Say for instance that you have a call where multiple things have to be saved to the DB. For simplicity we have 4 saves that get called in one request. After the 3rd call to the Save funtion, an Exception occurs. Now you might have 2 or 3 changes applied to your database. I say might, because you do not know whether or not the last call did or did not succeed. And the 4th one didn't get called at all. Quite possibly, you now have incomplete/corrupt data in your database. If this whole process was done with a transaction, none of the changes would've been applied, making it much safer to work with.

upizs commented 2 years ago

@Nobonex Thanks once more for your input. I will check it out and consider it. Implementing it would add more work than I planned but I can see its value now.

Nobonex commented 2 years ago

Haha yeah it's definitely not a must or anything. But as you are learning c# I thought it could be a fun exercise :)

upizs commented 2 years ago

@Nobonex No, you are right. I talk like I have a deadline. But this is my learning project, so I will find time to do it.