ios - MVVM, dependency injection and too many constructor parameters -


i have been doing ios development using mvvm , dependency injection couple of months , happy results. code clear , easier test. have been stragling problem haven't found solution felt confortable with.

in order understand problem want give little bit of context. last app have been working architectured in following way / layers:

  • model
  • view models
  • view / view controllers
  • services: classes know how deal external services twitter, facebook, etc.
  • repositories: repository class knows how interact resource of application's rest api. lets have blog application, have users resources , posts resources. each of thoses resources have several method. there 1-to-1 relation between resources , repositories.

when applications starts have bootstrap class initializes app , creates main view model. have restriction view models can create other view models. example in case of having view contains list of elements (in ios represented uitableview) , detail view each of thoses elements presented pushing navigation stack after tapping on element in list. make view model attached table view controller create detail view model. table view controller listens table view model , presents detail view model creating detail view controller , passing view model. view controller not know how create view model knows how create view controller view model.

is responsability of parent view model pass dependecies child view model.

the problem comes when view model deep in view hierachy needs dependencies parent controllers not require. example service access external web service. because parent not have dependency have add dependecy list, adding new parameter constructor. imagine how goes if grand parent not have dependecy either.

what think solution? possible solutions:

  • singletons: harder test , gloabl state
  • a factory class: set of factory knows how create types of object. example servicefactory , repositoryfactory. service factory have method create services like: twitterservice, facebookservice, githubservice. repository factory know how create repository each of api resources. in case of having few factories (2 or 3) view models dependent on factories.

for have chosen factory class solution because don't need use singletons , can treat factory other dependecy makes relatively easy test. problem kind of feels object , having factory don't know real dependecy needs view model, unless inside constructor's implementation check factory methods being called.

here couple of suggestions.

  • best coding practices suggests if using more 3 parameters, should use class host parameters.
  • another approach separate data services [repositories] out, line task based service. in line viewmodel (or controller) if viewmodel uses customers , orders, use 2 services - 1 crud operations on customers, , 1 crud operations on orders. could, however, use service deal operations needed viewmodel. task based approach used in designing windows communication foundation services , web services.

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -