Posted application framework, it seems to me, following the principles of clean architecture. There was a question - how to combine parts of the application without using dagger2 and similar tools? This is how I see the structure of the application.

// Layer UI public class Activity implements IView { Presenter presenter; // Кто передает презентер в активити? Или пускай сама и создаёт? } // Layer Presenters public class Presenter implements IPresentation{ interface IView { // ... } Presenter(IView view, Command cmd1, Command cmd2) { // Кто ещё, кроме презентера, может знать о вьюхах и кейсах (точнее об их интерфейсах)? } } // Layer Use Cases interface IPresentation { // ... } public interface Command { void execute(); } public class Start implements Command { public Start(IPresentation presentation, Entity entity) { // Кто создает экземпляр этого класса? } public void execute() { // ... } } public class Stop implements Command { public Stop(IPresentation presentation, Entity entity) { // ... } public void execute() { // ... } } // Layer Repository public class Repository { // В принципе, можно сделать методы статичными public Entity getEntity() { // ... return entity; } public void updateEntity(Entity entity) { // ... } } // Layer Entities public class Entity { // Ну тут вопросов нет. Ничего не знает и знать не хочет о нашем приложении } 
  • one
    Your question is about how to make your analog dagger or how to make it work on a different principle? - JuriySPb
  • @Yuriy SPb, I did not delve into the principle of work of Dagger. I read about it superficially and realized that this tool is most likely intended to solve the problem posed in the question. Before dealing with Dagger, I would like to make his work "hands". - alvoro
  • one
    Explore dependency injection pattern - georgehardcore
  • @georgehardcore is essentially the best answer. I looked through the article on the wiki - where I started to do it myself. A separate class for injection and drove. - alvoro

1 answer 1

Perhaps the article https://arturdryomov.online/posts/a-dagger-to-remember/ from Juno will help you (it’s made on Kotlin, it has convenient tools to write less code). They suggest doing something like this:

 interface RepositoryModule { val repository: Repository class Impl : RepositoryModule { override val repository by lazy { Repository.Impl() } }} interface RepositoryModule { val repository: Repository class Impl : Module { override val repository = Repository.Impl() } } interface Context : RepositoryModule, ServiceModule { class Impl( repositoryModule: RepositoryModule, serviceModule: ServiceModule ) : Context, RepositoryModule by repositoryModule, ServiceModule by serviceModule } 

In general, there is no magic in Dagger - look at the code that they generate, you can write it by hand, only long and dreary.

  • The question was how not to use Dagger, your answer seems to me a little strange :) - mit