dart-mod-player icon indicating copy to clipboard operation
dart-mod-player copied to clipboard

Extract the external calls for a facade class

Open wesleyfuchter opened this issue 4 years ago • 0 comments

Wouldn't make sense to use the Facade design pattern to extract the external calls for a separated class and import that class on the other dart code that uses it. The logic behind it to call the C function is ugly and extracting for a single class would make your code better.

class OpenMtpLookupFacade {
    
    final DynamicLibrary dyLib;
    const OpenMtpLookupFacade() {
         dyLib = DynamicLibrary.open(libraryPath);
    }
    
    void openModFile(String file) {
        final OpenModFile openModFileC = dyLib.lookup<NativeFunction<openModFile_native>>('open_mod_file').asFunction();
        openModFileC(file.toNativeUtf8());
    }

}

and in the other places:

final openMtp = OpenMtpLookupFacade();
openMtp.openModFile(myFancyString);

For the clients of this class the FFI is totally transparent, they won't know you're calling C :D

https://github.com/moduslabs/dart-mod-player/blob/12e885d71256d5181622fe6dcbd8dd2eaa8e00a5/lib/dart_openmpt.dart#L62

wesleyfuchter avatar Sep 16 '21 19:09 wesleyfuchter