CSharpProlog icon indicating copy to clipboard operation
CSharpProlog copied to clipboard

Best way to make the PrologEngine available in an app?

Open d-vyd opened this issue 4 years ago • 3 comments

Hi folks,

I'm fairly new to C#. I'd like to integrate PrologEngine prolog = new PrologEngine(persistentCommandHistory: false); into my Xamarin.Forms application in such a way that the variable prolog is easily available to query and modify the knowledge database. Does this mean a global variable? A repository? What is the easiest way of doing this? What is the best practice?

-david

d-vyd avatar Jan 24 '22 14:01 d-vyd

I just created a static class to hold the PrologEngine. That approach seems to work fine.

d-vyd avatar Jan 24 '22 16:01 d-vyd

I just created a static class to hold the PrologEngine.

I think it is not so bad implementation.

We may be able to use "DI" (dependency injection) architecture for that purpose, and DI might be a better way. Maybe you know, global variables are usually evil because it makes a program to be fragile.

So I recommend learning about "DI" architecture in the near future.

But anyway, using a global variable for storing a Prolog engine is not so bad at starting point.

jsakamoto avatar Jan 25 '22 23:01 jsakamoto

P.S. On "Xamarin.Forms", we can use the "ServiceCollection" class for the DI container.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollection?view=dotnet-plat-ext-6.0

// At the startup of a program...
var services = new ServiceCollection();
services.AddSingleton(() => new PrologEngine(persistentCommandHistory: false));
ServiceProvider = services.BuildServiceProvider(); // "ServiceProvider" is a global variable.
// When the "PrologEngine" instance is required...
var prolog = ServiceProvider.GetRequiredService<PrologEngine>(); // retrieve the PrologEngine instance via the "ServiceProvider" global variable.

jsakamoto avatar Jan 25 '22 23:01 jsakamoto