How to access a function in my MainForm from my DLL

I have a MainForm class where I have a function to retrieve my settings that are stored in my database.

So far so good.

Then I have a plugin (DLL) that I load at startup, but I would like to use the same function to retrieve the settings in my DLL.

If I need to access the functions in my DLL, I would just myDLL.function(....) , but I cannot use MainForm.function from my DLL, since my DLL does not know MainForm.

So, how do I pass my settings between my main application and my dll?

+4
source share
2 answers

Just to let you know the terminology, you're looking for "Inversion of Control" or "IoC."

There are several ways to implement this, including Injection of Dependency and callbacks (like delegates), as in Nico's answer. There are also service locators (although many consider this to be an "anti-pattern") and "Factories".

Personally, I prefer the Injection Dependency approach:

Basically, your DLL (aka ) needs an object that can execute a function, but it needs someone else (the caller) to implement the actual logic.

So, all you have to do is create an interface in your DLL that defines the type of object you want:

 Namespace DLL Public Interface IDataRetriever Public Function GetData() As Object End Interface End Namespace 

Then, in the MainForm project that references your DLL, simply create a class that implements this interface:

 Public Class DataRetriever Implements DLL.IDataRetriever Public Function GetData() As Object Implements DLL.IDataRetriever.GetData //... Return New Object() End Function End Class 

(Note that any class can implement an interface, including a class that already exists or even MainForm itself. You do not need to create a new class just for the interface - although make sure that you follow the separation of problems .)

Now, when you call the DLL, you can pass it your DataRetriever, and your DLL will know what it is connected with.

 Namespace DLL Public Class Utility Public Shared Function DLLFunction( retriever as IDataRetriever ) retriever.GetData() End Function End Class End Namespace 

 Class MainForm Sub Example() DLL.Utility.DLLFunction( New DataRetriever() ) End Sub End Class 
+4
source

It depends on what settings you have.

The cleanest way is probably to get the settings in the main application and pass it (as a parameter) to the DLL. You can specify an interface or something for the actual settings data. This generic type can be in a project referenced by both the DLL and the main application project.

Another way is to pass in the delegate. Instead of passing the full configuration data, you pass the delegate to a method that can retrieve the data.

+2
source

Source: https://habr.com/ru/post/1486108/


All Articles