Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Extract the external calls for a facade class #18

Open
wesleyfuchter opened this issue Sep 16, 2021 · 0 comments
Open

Extract the external calls for a facade class #18

wesleyfuchter opened this issue Sep 16, 2021 · 0 comments

Comments

@wesleyfuchter
Copy link

wesleyfuchter commented Sep 16, 2021

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

final OpenModFile openModFileC = dyLib.lookup<NativeFunction<openModFile_native>>('open_mod_file').asFunction();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant