Home java Working with the Repository Pattern

Working with the Repository Pattern

Author

Date

Category

Common task, data obtaining.
For example, I get the data on the profile from the server. After receiving caching in local memory. After that, in all places in the application where the profile needs, get data from the local repository (saved data).
The SAVE method is used only when data caching into local repository.

could be done like this:

Interface DataRepository EXTENDS DataSaverepository, DatagetRepository {
}
Interface DatasaverPository {
   Boolean Savedata (DATA DT);
}
Interface DatageTRepository {
   Data getdata ();
}

Realization of the local repository

class localRepository Implements DataRepository {
   @Override
   Data GetData () {
     // Receive data from the local database
   }
   @Override
   Boolean Savedata (Data DT) {
      // Keep the data to the local database
   }
}

External repository implementation 1

class networkrepository implements datagetrepository {
   @Override
   Data GetData () {
     // Get data from the Internet
   }
}

It seems to be more logical, and it seems to be xp @ ny somehow ..


or so:

how I imagine it:

interface datarepository {
   Data getdata ();
   Boolean Savedata (DATA DT);
}

Realization of the local repository

class localRepository Implements DataRepository {
   @Override
   Data GetData () {
     // Receive data from the local database
   }
   @Override
   Boolean Savedata (Data DT) {
      // Keep the data to the local database
   }
}

External repository implementation 1

class networkrepository Implements DataRepository {
   @Override
   Data GetData () {
     // Get data from the Internet
   }
   @Override
   Boolean Savedata (Data DT) {
      // server does not provide a preservation method, can only get from the server
   }
}

And objects that need a profile information know only the DataRepository interface and do not depend on the implementation.

how such situations are solved (when it is impossible to implement the interface method)?
Return Throw New Exception (“Not impl”) or false?

Or is it completely different to design interfaces for repositories?

offtop found in the English-speaking Internet article in which They write that you do not need to do the methods of Save, Update in the repositories, but I do not quite agree with this.


Answer 1, Authority 100%

You can use the Clean Architecture approach – divide the application by layers (its responsibility and isolation)

For example, the request “give profile” comes from the View layer (presenter / ViewModel or something else), this request goes to a layer of business logic (use / interactor / domain or something else), this level should know from Request a profile. Suppose a profile can be obtained only from the outside, then the appeal is transmitted to the next level, call it conditionally RePO – this level, generates a request to the appropriate API of the external system, gets the answer and converts it to the object-understandable object and returns it. Next, business logic can convert the received profile into some other form, understandable layer view.

In your case, since the profile needs to be saved, the business logic can be injecting two repo – one to work with local data, the second – remote. Thus, having received a query from the View Layer Logic, it may first request local data if there is no, request data from the outside, to get them, save to the local repo and return to the View level.

Errors can wear different character – related to a specific business process and others. Where to handle you to solve you. Obviously, business logic errors should be processed at the business logic level and convert to understandable errors of the current business process. And for example, it fell off / unavailable by time-ait server, should be processed in general.

Thus, view / domain / repo do not know anything about each other’s work, only use their embedded objects using interface methods available.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions