简体   繁体   中英

Clean Architecture - how to address database transactions?

In 'clean architecture' the interactors (use cases) are responsible for defining busines logic. Most of the examples define use cases in such a way:

public MyUseCase() {

  public boolean execute(...) {
    int id = repository.insert(a)
    if(id > 0) {
      b.aId= id;
      repository.insert(b);
      ...
    }
  }
}

Interactors use mostly simple CRUD like operations or queries on repository. The above example is synchronous for the case of simplicity but you can find repos with the same approach using asynchronous solutions like callbacks or rxjava.

But what about use case inegrity. For instance, you can't be 100% sure that after inserting a it will still be there when you insert b . What if after inserting a you get some RepositoryException while inserting b .

All the repos I've seen so far don't take it into account, so my question is:

What's the solution of the above problem in clean architecture?

This answer may be kinda late, but I have struggled with the same problem and came to a conclusion that transaction management is, in fact, part of a Use Case - like, "If something goes wrong with B, revert A's state". So, it can and should be explicitly stated within your UseCase, probably with some kind of "DataManagerRepo", like this:

public MyUseCase() {

    public boolean execute(...) {
        dataManagerRepository.openTransaction()
        try {
            int id = repository.insert(a)
            if(id > 0) {
            b.aId= id;
            repository.insert(b);
            ...
        }
        catch (MyException exc) {
            dataManagerRepository.rollbackTransaction()
        }

        dataManagerRepository.commitTransaction()
    }
}

Names may vary to abstract away integrity mechanism, but the idea is the same. I hope this will help somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM