felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

Bloc and Application Architecture #604

Closed farrokhpey closed 4 years ago

farrokhpey commented 4 years ago

Hi @felangel , i read your documentation about BLoC in this address: 'Architecture' I've a question about layering you say that we have Data Layer, BLoc and Presentation layers in Data Layer we have DataProvider and Repository in your Repository example code we can see final variables that has not been initialized (even in constructor), if this is true, please tell where and how we should to initialize them and also i need to know can i use static methods in DataProvider and Repository to access method statically? Thank you

tenhobi commented 4 years ago

The code shown in architecture implementation is not a dogma. You can implement it as you like. Initialization of final fields have to be done, of course, in place or using a constructor.

i think it is not implemented 'cause it doesn't matter what is the actual implementation and the code would just be with more noise and for no real benefit.

Also, the actual implementation might (for example the presentation layer; but also the actual data providers (web vs mobile)) depend on the framework used (Flutter, AngularDart or pure Dart).

felangel commented 4 years ago

Hi @farrokhpey 👋 Thanks for opening an issue!

Regarding your question, as @tenhobi mentioned, it does not matter how you choose to initialize them. I would recommend you inject them via constructor (for testing purposes)

class Repository {
    final DataProviderA dataProviderA;
    final DataProviderB dataProviderB;

    const Repository({@required this.dataProviderA, @required this.dataProviderB});

    Future<Data> getAllDataThatMeetsRequirements() async {
        final RawDataA dataSetA = await dataProviderA.readData();
        final RawDataB dataSetB = await dataProviderB.readData();

        final Data filteredData = _filterData(dataSetA, dataSetB);
        return filteredData;
    }
}

Regarding your second question, you can use static methods in your DataProvider and/or Repository where it makes sense (if you need to invoke a method without an instance of the class).

Hope that helps 👍