yes, know has been asked before, , yes, know "by design".
but i'd this:
@component(modules = {realmmodule.class}) public interface realmcomponent { realm realm(); } @component(modules = {repositorymodule.class}) public interface repositorycomponent { personrepository personrepository(); schedulerepository schedulesrepository(); } @component(dependencies = {realmcomponent.class, repositorycomponent.class}) public interface appdatacomponent extends realmcomponent, repositorycomponent { } @applicationscope @component(dependencies = {appcontextcomponent.class, appdatacomponent.class, appdomaincomponent.class, apppresentationcomponent.class, apputilscomponent.class}) public interface applicationcomponent extends appcontextcomponent, appdatacomponent, appdomaincomponent, apputilscomponent, apppresentationcomponent { void inject(customapplication customapplication); void inject(dashboardactivity dashboardactivity); } however, unscoped, every time inject jobmanager or schedulerepository or else, i new instance. way "fix" this.
@module public class jobmanagermodule { private jobmanager jobmanager; @provides public jobmanager jobmanager(context context) { if(jobmanager == null) { jobmanager = new jobmanager(context, new configuration.builder(context).networkutil( new wifiormobilenetworkutil(context)).build()); } return jobmanager; } } not fan.
so, how 1 meant structure , rip apart dependency tree, without making 1 big gigantic über blob component has every single module listed , every single provision method (instead of these "subcomponent" component dependencies)?
i tried using subcomponents this, have provide every single module final applicationcomponent.
i'm not sure here. tried specifying @singleton every first-level component , @subcomponentscope every appdatalevelcomponent, tried making new scope every single subcomponent, both of them failed "cannot depend on multiple scoped components".
edit: apparently in order scoped providers, marking components scope not enough - must specify scope @provides annotated methods too.
@module public class repositorymodule { @provides @singleton public personrepository personrepository() { return new personrepositoryimpl(); } @provides @singleton public schedulerepository schedulesrepository() { return new schedulesrepositoryimpl(); } } in meantime, ended übercomponent.
@singleton @component(modules = { appcontextmodule.class, dbmappermodule.class, dbtaskmodule.class, realmmodule.class, repositorymodule.class, interactormodule.class, servicemodule.class, presentermodule.class, xmlpersistermodule.class }) public interface applicationcomponent extends appcontextcomponent, appdatacomponent, appdomaincomponent, apputilscomponent, apppresentationcomponent { where xyzcomponent classes interfaces store provision methods...
(please note that this structure anti-pattern described martin fowler, , should organize modules based on features / activities, , make them subscoped components using component dependencies. component dependencies used subscope superscope components, , "inherit" dependency providers.)
i had same problems not while ago , ended using same ubercomponent approach except use @subcomponents in order organize things , not have modules listed in ubercomponent (i call "top" or "app's" component).
you may see example here: how migrate missing inject module complete = false dagger 1 dagger 2
Comments
Post a Comment