It's a weird question I know :)
I really like to do the things in the right way and I have a doubt.
I know about making a interface, using DI...
My question is:
Is better to have a method like "SaveChanges" that you have to call manually everytime you add / delete / whatever an object?:
_repo.Add(blah);
_repo.SaveChanges();
Or is better to save changes inside every method that modify the data?
On the other hand, should I have the connection always opened or have I to close it?
Im learning DB4O and I have a Close method that I call when I have to use the repo on another place (Like in another windows, I close before I open the window).
Thank you.
If you want an instance that lasts for the duration of a user request (that eg might hold details of the user), then use scoped. If you want a new instance every time the container is asked to provide one (a resource access request that needs disposing quickly for example), then use transient.
Large-scale, complex projects benefit the most from readability improvments, and when using Dapper and C# the Dapper Base Repository pattern allows for better code readability and a single point of failure by creating a base class with generic methods that allow for querying and execution against a SQL database.
The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.
The Repository Pattern allows us to create an abstraction layer between the data access layer and the business logic layer of an application. So, this Data Access Pattern offers a more loosely coupled approach to data access.
I personally like the SaveChanges method to be separated. I think it allows for more flexibility in the consuming applications. Which means it can have more reuse.
For example, having it separate allows a 'transaction' approach where the repository can be continually modified and then if everything is acceptable the save method is called.
On the other side, if you want to save immediately without a separate call, you can create another version of the repository which calls the save method during CRUD operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With