Direct OO pattern

The two developers have an argument about which template is correct. I would be very glad if someone could tell me who is right and why.

Developer 1:

class a has two functions:

Remove(int item);
Save();

      

Reasoning:

a.Remove(1);
a.Remove(3);
a.Save();

      

This way you can remove items from the internal list without saving changes to the database. As soon as you call a.Save (), the internal state of the object will be saved in the database.

Developer 2:

class a has only one function

Remove(List<int> items);

      

Reasoning:

a.Remove(new List<int>{1,2});

      

This way you don't have to remember saving, and class a can only save once.

Are there design templates or other documents that prove the right way, or is it just style?

+3


source to share


1 answer


As this is a very synthetic example, it is hard to say that the right path does not provide knowledge of the real context.

In most real-world cases, the first solution is better because of the need to reduce the number of queries and the duration of database queries.

Take a look at the repository templates and units of work.

General sequence of actions:

  • Create a block of work.
  • Do some data manipulation with a repository (which shows up in UoW)
  • Do other data manipulation using the repository
  • Replace changes via Unit of Work.


Depending on the implementation of the Unit of Work, you can

  • Create a physical transaction (or connection) at the beginning and push changes immediately. This is the flexible equivalent for the second solution.
  • Create a physical transaction and commit it at the end with UoW.Commit
  • Implementation of various autonomous locks (pessimistic or optimistic).

Thus, it is rigged. The solution with one method is less difficult, especially if you need to add another action, i.e.

Remove(1,2);
Add(5);

      

Talking about data access and domains, in the long run it can leak db logic and others that are currently recognized as anti-patterns (in most cases) such as Active Record.

+1


source







All Articles