Open
Description
As in the example, usecase's parameters are set via UserModule
:
@Module
public class UserModule {
private int userId = -1;
public UserModule() {}
public UserModule(int userId) {
this.userId = userId;
}
@Provides @PerActivity @Named("userDetails") UseCase provideGetUserDetailsUseCase(
UserRepository userRepository, ThreadExecutor threadExecutor,
PostExecutionThread postExecutionThread) {
return new GetUserDetailsUseCase(userId, userRepository, threadExecutor, postExecutionThread);
}
}
But there are some cases when buildUseCaseObservable
depends on some dynamic parameters. I tried to create an interface to provide these parameters and let the view (in MVP) implement it and pass them to UserModule
. With this approach if the view is a Fragment
then I have to re-create UserModule
again in the Fragment
.
public interface UserIdProvider {
int getUserId();
}
Any suggestion, recommendation?
Activity
[-]Best practice to pass parameter to an UseCase?[/-][+]Best practice to pass dynamic parameters to an UseCase?[/+]android10 commentedon Aug 10, 2015
So in that case, buildUserCaseObservable is no longer useful and you will have to pass them via setter (with defensive code and failing fast in case all the dynamic parameters are not satisfied).
I used this approach due to dagger instantiating my objects thus, being able to pass the user id via module constructor parameter.
spirosoik commentedon Sep 10, 2015
@android10 yes that's true, but how do you pass dynamics parameters for login process for example? I mean in this case there isn't a predefined parameter but you must setup the submitted details? Which is your approach on this? I know that this is a question but it's really interesting to give us an example.
For this case the Dagger module must be dynamic so I suppose that there is 3 solutions (I think):
@PerFragment
also)or ????
There is a problem only this because you can't use multiple scopes for example for data mappers. So we must remove scopre for datamappers for example in order to be able to seen into the both scopes! Right? Any other suggestions maybe cleaner than this one?
spirosoik commentedon Sep 13, 2015
@android10 please when you have free time it will be nice to give us a better approach by your side.
spirosoik commentedon Oct 27, 2015
@android10 I know that is closed this issues but I want to know if our approach is acceptable.
Currently we have multiple forms so for example I have an invite form with multiple attrs.
Let's Say I have
InviteActivity
InviteView
InvitePresenter
InviteComponent
in their referral package explicitly.In my
InviteModule
of dagger I have something like this:In the
InviteComponent
I have thisSo on click of the form I am just doing this:
this.getComponent(InviteComponent.class).plus(new InviteModule(textemail, textBusinessName);
in order to use the attributes into the UseCase. Any suggestion for a different approach?
MehdiChouag commentedon Nov 28, 2015
@spirosoik Does the solution example in your last comment works nice with your needs ?
spirosoik commentedon Nov 29, 2015
@MehdiChouag It's easier to keep the abstraction as it is with
buildUseCaseObservable()
but you can use this. Remember that in this method you must pass a Domain model not simple parameters.protected abstract Observable buildUseCaseObservable(Object...param);
or you can re-create the component during login
Another way is to use
@PerFragment
scopeI am suggesting the first solution.
@android10 @lalongooo if you have a better solution than the first one, I would love to listen.
MehdiChouag commentedon Nov 29, 2015
@spirosoik For me the first solution seems to be more appropriate.
Rather using
Object
, we may using template for parameters, like :And using it like this :
spirosoik commentedon Nov 29, 2015
@MehdiChouag Yes of course I just send you the logic. This is depends in your case but as I said before it will be nice to use domain models to use cases, not plain params.
For example I am passing into the UseCase a UserModel which I am transforming this into User domain model or for credentials you can have a different object for this.
MehdiChouag commentedon Nov 29, 2015
@spirosoik Can we avoid to create a UserModel then transforming into a User domain ?
And instead directly use the User domain, that wouldn't break dependency rules.
spirosoik commentedon Nov 29, 2015
@MehdiChouag Yes there is a great discussion (you are in) on this subject and I will support @android10 on this that each layer must have the View Models and each layer should have its model to deal with in order to avoid coupling between layers. It's clear that you are breaking the dependency.
22 remaining items