This is a what is the best practice kind of a question.
I am building an app using flutter and I have the below requirements.
I have local (installed on the device) and remote (installed on the server) databases.
I have to build repositories for the local databases. I have many choices for this (SQLITE, Hive, etc.). I have to keep the choice of the database loosely coupled with the application (Repository pattern).
I have to use the BLOC pattern for the state management.
The point where I am struggling is for each type of database, the entity model (I come from an entity framework background and therefore calling it entity model. I don't know what you call it) is different.
For example,
The model for SQLLite (Moor) looks as below
class ToDosSqlLite extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text().withLength(min: 6, max: 32)();
}
The model for Hive looks as below.
class ToDosHive extends HiveObject {
final int id;
final String title;
Person(this.id, this.title);
}
And for any other choice of database, the model will look different.
And I have repository classes as below.
abstract class LocalToDoRepository{
List<What should be the type here?> getAll();
}
class SqlLiteToDoRepository extends LocalToDoRepository{
///overriding won't work here as Type is different from the base class method
@override
List<ToDosSqlLite> getAll(){///implementation}
}
class HiveToDoRepository extends LocalToDoRepository{
///overriding won't work here as Type is different from the base class method
@override
List<ToDosHive> getAll(){///implementation}
}
In SqlLiteToDoRepository, a getAll()
method returns a List<ToDosSqlLite>
and in HiveToDoRepository, same method returns a List<ToDosHive>
.
And below is my Bloc
class ToDoBloc extends Bloc<ToDoEvent, ToDoState>{
final LocalToDoRepository localToDoRepository;
///localToDoRepository object is dependency injected . If I want to use SQLite, I will inject
///SqlLiteToDoRepository and so on.
ToDoBloc ({@required this.localToDoRepository}): super(ToDoInitialState());
}
How do I do this abstraction in an elegant way? Please suggest if you have any ideas.
Thanks in advance.
You may try something like this:
This outputs