हां, मुझे पता है कि इससे पहले पूछा गया है, और हाँ, मुझे पता है कि यह "डिज़ाइन द्वारा" है।डैगर 2: घटक एकाधिक स्कोप्ड घटकों पर निर्भर नहीं हो सकता
लेकिन मैं कुछ इस तरह करना चाहते हैं:
@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);
}
हालांकि, मैं क्या मिलेगा unscoped है, हर बार जब मैं एक JobManager
या एक ScheduleRepository
या कुछ और इंजेक्षन, मैं एक नया मिलता है उदाहरण। एकमात्र तरीका मैं "ठीक" कर सकता था जो यह था।
@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;
}
}
नहीं एक प्रशंसक।
तो, कैसे एक संरचना और एक बड़ा विशाल über ब्लॉब घटक है कि हर एक मॉड्यूल सूचीबद्ध और हर एक प्रावधान विधि (बजाय इन "उप-घटक है बनाने के बिना निर्भरता पेड़, अलग चीर करने के लिए है "घटक निर्भरता)?
मैंने इसके लिए उप-कॉम्पोनेंट का उपयोग करने की कोशिश की, लेकिन फिर आपको अंतिम ApplicationComponent
के लिए प्रत्येक एकल मॉड्यूल प्रदान करना होगा।
मुझे यकीन नहीं है कि यहां क्या करना है। मैंने प्रत्येक AppDataLevelComponent
के लिए प्रत्येक प्रथम-स्तर के घटक और @SubcomponentScope
के लिए @Singleton
निर्दिष्ट करने का प्रयास किया, मैंने प्रत्येक एकल उपसंवेदनशील के लिए एक नया दायरा बनाने का भी प्रयास किया, लेकिन उनमें से दोनों "एकाधिक स्कोप्ड घटकों पर निर्भर नहीं हो सकते" के साथ विफल रहे।
संपादित करें: जाहिर आदेश scoped प्रदाताओं प्राप्त करने के लिए, गुंजाइश के साथ घटक अंकन में पर्याप्त नहीं है - आप @Provides
एनोटेट तरीकों के लिए गुंजाइश भी निर्दिष्ट करना होगा।
@Module
public class RepositoryModule {
@Provides
@Singleton
public PersonRepository personRepository() {
return new PersonRepositoryImpl();
}
@Provides
@Singleton
public ScheduleRepository schedulesRepository() {
return new SchedulesRepositoryImpl();
}
}
इस बीच, मैं इस Ü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 {
कहाँ xyzComponent
कक्षाएं सिर्फ प्रावधान तरीकों स्टोर करने के लिए इंटरफ़ेस हैं ...
(कृपया ध्यान दें। किthis structure is an anti-pattern as described by Martin Fowler, and you should organize modules based on features/activities, और उन्हें घटक निर्भरता का उपयोग कर subscoped घटकों में बनाने के घटक निर्भरता subscope लिए किया जाता है आपके सुपरस्कोप घटक, और "उत्तराधिकारी" निर्भरता प्रदाताओं।)