I have an asp.net core project that is divided into the following structure
- Controller
- Store
- Repository
- Store
The controller receives requests, and calls the correct Stores that are responsible for actions against the database (Create Users, Update Records, etc) The way I see it the controller should be agnostic to the database - it should not control the transactions, or even be aware of their existence.
The store creates a transaction, and calls the required repositories, and their required CRUD methods.
This is pretty straight forward. However, often I find myself repeating code - because of the transaction management.
Sometimes I want to add users, and add them to a group - So If I had a GroupsStore
, and a UsersStore
- I now have a GroupsUsersStore
that does exactly the same as those store, but by putting that in one transaction.
This is obviously wrong, but I can't think of a better solution - since transactions can't be nested in sql server, and I want those stores to work independently as well.
How do you solve such issue?
db.SaveChanges()
. That way, it all happens in a single transaction. The downside is that you always need to open and commit a transaction outside of the repository methods. – Ivo Coumans Dec 1 '16 at 14:02AddUser
doesn't just add a user column, it adds other info, and so doesAddGroup
. The Idea is that I want to be able to call them from anywhere (via dependency injection), without calling db.SaveChanges() or even knowing that such a connection to db exists. I could create an even higher level like a service, or something - although I don't like that Idea either. – gilmishal Dec 1 '16 at 14:09