Upgrade to Pro — share decks privately, control downloads, hide ads and more …

我々のRealmはどこからやってくるのか

 我々のRealmはどこからやってくるのか

DIについての自由研究内容を発表しました。

Tomohiro Nishimura

March 26, 2017
Tweet

More Decks by Tomohiro Nishimura

Other Decks in Programming

Transcript

  1. ͸ͯͳͰԿΛ΍͖͔ͬͯͨ αʔϏεએ఻ίʔφʔ w .BDLFSFM w IUUQTNBDLFSFMJP w ΧΫϤϜ "1*ɺΞϓϦ 

    w IUUQTLBLVZPNVKQ w ͸ͯͳϒϩά ΞϓϦ  w IUUQIBUFOBCMPHDPNHVJEFBQQ w ͸ͯͳϒοΫϚʔΫ ΞϓϦ  w IUUQCIBUFOBOFKQHVJEFBQQ
  2. ΋͏ͪΐͬͱෳࡶͳྫ let configuration = Realm.Configuration(fileURL: url) let realm = try!

    Realm(configuration: configuration) RealmConfiguration configuration =
 new RealmConfiguration.Builder()
 .name("data")
 .build(); Realm realm = Realm.getInstance(configuration); 4XJGU +BWB
  3. Ұൠతͳ$POpHVSBUJPO RealmConfiguration config = new RealmConfiguration.Builder()
 .name("myrealm.realm")
 .encryptionKey(getKey())
 .schemaVersion(42)
 .modules(new

    MySchemaModule())
 .migration(new MyMigration())
 .build();
 
 Realm realm = Realm.getInstance(config); +BWB
  4. ྫ͑͹4JOHMFUPO class Locator { private static let instance = Locator()

    public var realmConfiguration: Realm.Configuration { var configuration = Realm.Configuration() configuration.fileURL = configuration.fileURL! .deletingLastPathComponent() .appendingPathComponent("main.realm") return configuration } } 4XJGU
  5. ઃఆΛ࡞Βͳͯ͘Α͘ͳͬͨ let configuration = Locator.instance.realmConfiguration let realm = try! Realm(configuration:

    configuration) RealmConfiguration configuration = Locator.getInstance().getRealmConfig(); Realm realm = Realm.getInstance(configuration); 4XJGU +BWB
  6. 3FBMN΋ఏڙͯ͠͠·͏ class Locator { static let instance = Locator() public

    var realmConfiguration: Realm.Configuration { return // Realm.ConfigurationΛ࡞Δ } public var realm: Realm { return try! Realm(configuration: realmConfiguration) } } 4XJGU
  7. <࠶ܝ>-PDBUPS class Locator { static let instance = Locator() public

    var realmConfiguration: Realm.Configuration { return // Realm.ConfigurationΛ࡞Δ } public var realm: Realm { return try! Realm(configuration: realmConfiguration) } } 4XJGU
  8. ߽՚ͳ-PDBUPS public class Locator { // ɾɾɾུɾɾɾ public RealmConfiguration getRealmConfiguration()

    { … } public Realm getRealm() { … } public APIClient getApiClient() { … } public Tracker getTracker() { … } public UserRepository getUserRepository() { … } public FriendRepository getFriendRepository() { … } public ItemRepository getItemRepository() { … } } +BWB
  9. 3FQPTJUPSZͷྫ class UserRepository { private let realm = Locator.instance.realm private

    let apiClient = Locator.instance.apiClient func load(byID id: String, callback: (User) -> Void) { // APIݺͼग़͠ } private func save(json: [AnyHashable: Any]) { // Realmʹอଘ } } 4XJGU
  10. <࠶ܝ>3FQPTJUPSZͷྫ class UserRepository { private let realm = Locator.instance.realm private

    let apiClient = Locator.instance.apiClient func load(byID id: String, callback: (User) -> Void) { // APIݺͼग़͠ } private func save(json: [AnyHashable: Any]) { // Realmʹอଘ } } 4XJGU
  11. -PDBUPSΛίϯετϥΫλʹ ౉ͤΔΑ͏ʹ͢Δ public class UserRepository {
 private final Realm realm;


    private final APIClient apiClient;
 
 public UserRepository(Locator locator) {
 realm = locator.getRealm();
 apiClient = locator.getApiClient();
 }
 } +BWB
  12. -PDBUPSΛࠩ͠ସ͑ΒΕΔΑ ͏ʹΠϯλʔϑΣʔεʹ͢Δ interface ILocator {
 RealmConfiguration getRealmConfiguration();
 Realm getRealm();
 APIClient

    getApiClient();
 Tracker getTracker();
 
 UserRepository getUserRepository();
 FriendRepository getFriendRepository();
 ItemRepository getItemRepository();
 } +BWB
  13. *-PDBUPSΛίϯετϥΫλʹ ౉ͤΔΑ͏ʹ͢Δ public class UserRepository {
 private final Realm realm;


    private final APIClient apiClient;
 
 public UserRepository(ILocator locator) {
 realm = locator.getRealm();
 apiClient = locator.getApiClient();
 }
 } +BWB
  14. .PDL-PDBUPSΛ࡞Δ public class MockLocator implements ILocator {
 @Override
 public RealmConfiguration

    getRealmConfiguration() { return // in-memory Configuration }
 
 @Override
 public Realm getRealm() { return Realm.getInstance(realmConfiguration()); }
 
 @Override
 public APIClient getApiClient() { return // MockApiClient }
 
 @Override public Tracker getTracker() { … }
 @Override public UserRepository getUserRepository() { … }
 @Override public FriendRepository getFriendRepository() { … }
 @Override public ItemRepository getItemRepository() { … }
 } +BWB
  15. ςετͷ༷ࢠ let locator = MockLocator() let repository = UserRepository(locator) 4XJGU

    +BWB Locator locator = new MockLocator();
 UserRepository repository =
 new UserRepository(locator);
  16. ͜͏ͩͬͨͷ͕ class UserRepository { private let realm: Realm private let

    apiClient: APIClient init() { realm = Locator.instance.realm apiClient = Locator.instance.apiClient } } 4XJGU
  17. ͜͏ͳΓ·ͨ͠ class UserRepository { private let realm: Realm private let

    apiClient: APIClient init(locator: LocatorProtocol) { realm = locator.realm apiClient = locator.apiClient } } 4XJGU
  18. <࠶ܝ>.PDL-PDBUPSΛ࡞Δ public class MockLocator implements ILocator {
 @Override
 public RealmConfiguration

    getRealmConfiguration() { return // in-memory Configuration }
 
 @Override
 public Realm getRealm() { return Realm.getInstance(realmConfiguration()); }
 
 @Override
 public APIClient getApiClient() { return // MockApiClient }
 
 @Override public Tracker getTracker() { … }
 @Override public UserRepository getUserRepository() { … }
 @Override public FriendRepository getFriendRepository() { … }
 @Override public ItemRepository getItemRepository() { … }
 } +BWB
  19. ͜͏͍͏ײ͡ class UserRepository { private let realm: Realm private let

    apiClient: APIClient init(dependency: UserRepositoryDependency) { realm = dependency.realm apiClient = dependency.apiClient } } 4XJGU
  20. Α͘ͳͬͨ.PDL-PDBUPS class MockLocator: UserRepositoryDependency { var realmConfiguration: Realm.Configuration { return

    .Configuration(inMemoryIdentifier: "test") } var realm: Realm { return try! Realm(configuration: realmConfiguration) } var apiClient: APIClientProtocol { return new MockApiClient(); } } 4XJGU
  21. 4FSWJDF-PDBUPS͸͜ͷΠϯ λʔϑΣʔεl΋z࣮૷͢Δ class Locator: LocatorProtocol, UserRepositoryDependency { var realmConfiguration: Realm.Configuration

    { … } var realm: Realm { … } var apiClient: APIClient { … } var tracker: Tracker { … } var userRepository: UserRepository { … } var friendRepository: FriendRepository { … } var itemRepository: ItemRepository { … } } 4XJGU
  22. 7JFX$POUSPMMFSͩͱ͜͏͍ ͏͜ͱΛ͠·͢ΑͶ override func prepare(for segue: …) { if let

    vc = segue.destination as? NextViewController { vc.locator = Locator.instance } } 4XJGU
  23. $POTUSVDUPS*OKFDUJPO let locator = Locator.instance let repository = UserRepository(locator: locator)

    4XJGU +BWB Locator locator = Locator.getInstance();
 Repository repository = new UserRepository(locator);
  24. 1SPQFSUZ*OKFDUJPO override func prepare(for segue: …) { if let vc

    = segue.destination as? NextViewController { vc.locator = Locator.instance } } 4XJGU
  25. 1SPUPDPM&YUFOTJPO protocol WithLocator { var locator: LocatorProtocol { get }

    } extension WithLocator { var locator: LocatorProtocol { return Locator.instance } } 4XJGU
  26. 1SPUPDPM&YUFOTJPO class RootViewController: UIViewController, WithLocator { override func viewDidAppear(_ animated:

    Bool) { super.viewDidAppear(animated) // ຊ౰͸ userRepository ࢖͑͹ྑͦ͞͏Ͱ͢Ͷ locator.apiClient.fetchUser(byID: “xxxx”) { [weak self] result in try! locator.realm.write { self?.locator.realm.add(User(json: result.json)) } } } } 4XJGU
  27. "DUJWJUZʹ*OKFDU͢Δ༷ࢠ public class MainActivity extends AppCompatActivity {
 @Inject
 Locator mLocator;


    
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 ((App) getApplicationContext())
 .getApplicationComponent()
 .newActivityComponentBuilder()
 .activity(this)
 .build()
 .inject(this);
 }
 } +BWB
  28. ґଘ͕ଟ͍ͱ public class MainActivity extends AppCompatActivity {
 @Inject
 Locator mLocator;


    
 @Inject
 Realm mRealm;
 
 @Inject
 ApiClient mApiClient;
 
 @Inject
 UserRepository mUserRepository;
 
 @Inject
 FriendRepository mFriendRepository;
 } +BWB