XRepository is based on QBRepository by QuickBirds Studios. It is lightweight implementation of Repository pattern in Swift.
👋🏻 Getting started
Cornerstones of this project are protocol Repository
and class AnyRepository
as its generic implementation. Repository
supports basic and advanced CRUD operations. Also, you have access to out-of-the-box implementations of a few popular storages based on: UserDefaults
, RealmSwift
, FileManager
, CoreData
. But you can also create your own implementation of those ones or any other storage mechanism.
public protocol Repository {
associatedtype Model
...
}
public final class AnyRepository<Model>: Repository {
...
}
🔧
Usage
Since Repository
requires associated value to it, we use its generic implementation AnyRepository
. Usage is simple:
class ChurchesViewModel {
...
init(_ churchesRepository: AnyRepository<Church>) {
let localChurch = Church(id: "hillsong-lviv", name: "Hillsong Lviv", family: "hillsong-family")
let stateChurches = [Church(id: "hillsong-lviv", name: "Hillsong Lviv", family: "hillsong-family"), Church(id: "hillsong-odesa", name: "Hillsong Odesa", family: "hillsong-family")]
// Create
churchesRepository.create(localChurch)
churchesRepository.create(stateChurches)
// Read
let allChurches = churchesRepository.getAll()
let hillsongChurch = churches.getElement(withId: "hillsong")
let hillsongFamilyChurches = churches.getElements(filterBy: \.family == "hillsong")
// Update
churchesRepository.update(Church(id: "hillsong", name: "Hillsong Kyiv", family: "hillsong-family"))
// Delete
churchesRepository.deleteAll()
churchRepository.delete(localChurch)
churchRepository.delete(stateChurches)
}
...
}
let churchesUserDefaultsStorage = UserDefaultsRepository<Church>()
let churchesRealmStorage = RealmRepository<Church>()
let churchesCoreDataStorage = CoreDataRepository<Church>()
let churchesFileSystemStorage = FileSystemRepository<Church>()
// Any repository will fit
let churchesViewModel = ChurchesViewModel(churchesRepository: AnyRepository(churchesRealmStorage))
⚡️
Rx
XRepository supports reactive wrapper over AnyRepository
let churchesRepository: AnyRepository<Church>!
churchesRepository.rx.getElements(sortedBy: \.name)
.subscribe(onNext: { churchesOrderedByName in
...
})
.disposed(by: bag)
If you want a pure reactive repository implementations for popular storages, check my latest project: ReactiveXRepository
🍴
Instalation
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. It is in early development, but Alamofire does support its use on supported platforms.
Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/sashkopotapov/XRepository.git", .upToNextMajor(from: "1.0.0"))
]
👤
Author
This framework is created by Sashko Potapov.
📃
License
XCoordinator is released under an MIT license. See License.md for more information.