user2233440
user2233440

Reputation: 165

Add .NET dll's at runtime or a less error-prone alternative?

Another one of those "Best way to X" but I have a question about how I should go about designing this certain piece of software I am working on.

I have this C# WPF software (lets call it ENGINE) that iterates over a list of TASKS and calls the same three methods on each TASK: initialize(), calculate(), and save().

//Simplified obviously (I use indexes vs. foreach because of other code I left out)
while(currentOperatingIndex < tasks.Count)
{
    Task currentTask = tasks.ElementAt(currentOperatingIndex);
    currentTask.initialize();
    currentTask.calculate();
    currentTask.save();
    currentOperatingIndex++;
}

Now these TASKS I need to load dynamically into the ENGINE program at runtime. The engine program will never be re-built or chang when a new TASK is created. So I designed it such that each TASK will be in its own library DLL. Thus, the engine will iterate a folder of DLL files at runtime and load in each DLL into the assembly.

I designed it like this because each TASK initializes, calculates and saves differently. For example, say I want to add a new task, I dont have to change the ENGINE's code, or any other DLL, I just create a new DLL for the TASK, make sure it has those 3 functions, and put it in the folder. My goal is that when a new TASK is needed a programmer can open up visual studio, create a new C# library, create three functions, and write the implementation.

Is this the best way to handle a 'plugin-like' functionality?

Hopefully I am clear.

Upvotes: 1

Views: 162

Answers (2)

user2233440
user2233440

Reputation: 165

As mentioned by Alexei Levenkov in a comment to my question if you are looking to dynamically add plugins to a program without adding extra code to include them, use MEF where it imports many plugins into a catalog and you can use it from there. Thanks everyone for your input.

Upvotes: 0

Kenan Kocaerkek
Kenan Kocaerkek

Reputation: 319

Define an interface (for example ICalcTask) has three methods: initialize, calculate, save.

Write plugin class library dlls including classes implemeting ICalcTask interface.

Let your host application scans dlls containing ICalcTask implementations in plugins directory.

Upvotes: 1

Related Questions