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

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

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

Fb69c72d5af289d6674d2659246a5291?s=128

Tomohiro Nishimura

March 26, 2017
Tweet

Transcript

  1. 3.

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

    w IUUQTLBLVZPNVKQ w ͸ͯͳϒϩά ΞϓϦ  w IUUQIBUFOBCMPHDPNHVJEFBQQ w ͸ͯͳϒοΫϚʔΫ ΞϓϦ  w IUUQCIBUFOBOFKQHVJEFBQQ
  2. 8.

    ΋͏ͪΐͬͱෳࡶͳྫ 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. 9.

    Ұൠతͳ$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. 11.

    ྫ͑͹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. 12.

    ઃఆΛ࡞Βͳͯ͘Α͘ͳͬͨ let configuration = Locator.instance.realmConfiguration let realm = try! Realm(configuration:

    configuration) RealmConfiguration configuration = Locator.getInstance().getRealmConfig(); Realm realm = Realm.getInstance(configuration); 4XJGU +BWB
  6. 13.

    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. 15.
  8. 16.

    <࠶ܝ>-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
  9. 17.

    ߽՚ͳ-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
  10. 25.

    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. 27.

    <࠶ܝ>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
  12. 28.

    -PDBUPSΛίϯετϥΫλʹ ౉ͤΔΑ͏ʹ͢Δ public class UserRepository {
 private final Realm realm;


    private final APIClient apiClient;
 
 public UserRepository(Locator locator) {
 realm = locator.getRealm();
 apiClient = locator.getApiClient();
 }
 } +BWB
  13. 29.

    -PDBUPSΛࠩ͠ସ͑ΒΕΔΑ ͏ʹΠϯλʔϑΣʔεʹ͢Δ interface ILocator {
 RealmConfiguration getRealmConfiguration();
 Realm getRealm();
 APIClient

    getApiClient();
 Tracker getTracker();
 
 UserRepository getUserRepository();
 FriendRepository getFriendRepository();
 ItemRepository getItemRepository();
 } +BWB
  14. 30.

    *-PDBUPSΛίϯετϥΫλʹ ౉ͤΔΑ͏ʹ͢Δ public class UserRepository {
 private final Realm realm;


    private final APIClient apiClient;
 
 public UserRepository(ILocator locator) {
 realm = locator.getRealm();
 apiClient = locator.getApiClient();
 }
 } +BWB
  15. 31.

    .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
  16. 32.

    ςετͷ༷ࢠ let locator = MockLocator() let repository = UserRepository(locator) 4XJGU

    +BWB Locator locator = new MockLocator();
 UserRepository repository =
 new UserRepository(locator);
  17. 34.

    ͜͏ͩͬͨͷ͕ class UserRepository { private let realm: Realm private let

    apiClient: APIClient init() { realm = Locator.instance.realm apiClient = Locator.instance.apiClient } } 4XJGU
  18. 35.

    ͜͏ͳΓ·ͨ͠ class UserRepository { private let realm: Realm private let

    apiClient: APIClient init(locator: LocatorProtocol) { realm = locator.realm apiClient = locator.apiClient } } 4XJGU
  19. 36.
  20. 37.

    <࠶ܝ>.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
  21. 41.

    ͜͏͍͏ײ͡ class UserRepository { private let realm: Realm private let

    apiClient: APIClient init(dependency: UserRepositoryDependency) { realm = dependency.realm apiClient = dependency.apiClient } } 4XJGU
  22. 42.

    Α͘ͳͬͨ.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
  23. 43.

    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
  24. 58.

    7JFX$POUSPMMFSͩͱ͜͏͍ ͏͜ͱΛ͠·͢ΑͶ override func prepare(for segue: …) { if let

    vc = segue.destination as? NextViewController { vc.locator = Locator.instance } } 4XJGU
  25. 74.
  26. 79.

    $POTUSVDUPS*OKFDUJPO let locator = Locator.instance let repository = UserRepository(locator: locator)

    4XJGU +BWB Locator locator = Locator.getInstance();
 Repository repository = new UserRepository(locator);
  27. 80.

    1SPQFSUZ*OKFDUJPO override func prepare(for segue: …) { if let vc

    = segue.destination as? NextViewController { vc.locator = Locator.instance } } 4XJGU
  28. 87.

    1SPUPDPM&YUFOTJPO protocol WithLocator { var locator: LocatorProtocol { get }

    } extension WithLocator { var locator: LocatorProtocol { return Locator.instance } } 4XJGU
  29. 88.

    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
  30. 92.

    "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
  31. 93.

    ґଘ͕ଟ͍ͱ public class MainActivity extends AppCompatActivity {
 @Inject
 Locator mLocator;


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