skip to Main Content

I’ve created an interface for my use cases that returns a Future, Is it good approach to have an interface for all use cases?

What if I need a Usecase to return Streams instead of a single return (Future), for example listen to snapshots from Firebase Cloud Firestore collection, should I have two interfaces?

import 'package:equatable/equatable.dart';
import '../errors/failure.dart';
import 'package:dartz/dartz.dart';

abstract class Usecase<Input, Output> {
  Future<Either<Failure, Output>> call(Input input);
}

class Input<T> extends Equatable {

  final T parameter;
  Input(this.parameter);

  @override
  List<Object?> get props => [parameter];
}

class NoInput extends Equatable {

  @override
  List<Object?> get props => [];
}

2

Answers


  1. Your use cases only need interfaces if you want to hide the implementation of your use case from its callers (usually the controllers) OR when you want to replace the implementation later on, e.g. at run-time based on some configuration). My use cases often don’t have any interfaces.

    If your use case has multiple "clients" and you want to keep those logically separated then interface segregation could be useful. If you only have e.g. one controller calling multiple APIs on your use case then a single interface is usually good enough.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search