Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern, explicit or implicit save?

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.

like image 296
Jesus Rodriguez Avatar asked Jul 31 '10 21:07

Jesus Rodriguez


People also ask

Should repository be scoped or transient?

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.

Should I use repository pattern with dapper?

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.

What type of pattern is repository?

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.

What is the benefit of repository pattern in Entity Framework?

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.


1 Answers

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.

like image 123
Jerod Houghtelling Avatar answered Oct 16 '22 22:10

Jerod Houghtelling