I’m trying to design for the following scenario but cannot get my head around it.
I have records in a database table that represent a task the application needs to perform. Each record is mapped to a specific C# function. So adding a row to the table requires a rebuild of the application by creating a specific task for it.
So far, I have added a base class and create a new class every time new functionality needs to be added.
Base Class:
public abstract class TaskBase
{ public abstract void DoWork (); }
public sealed class Task001: TaskBase
{ public override void DoWork () { /* Record-specific code */ } }
public sealed class Task002: TaskBase
{ public override void DoWork () { /* Record-specific code */ } }
It’s difficult to explain why this needs to be done so I won’t go there. Although I am sure there is a better way of achieving this in terms of design and practicality. Any suggestions? I am using C# and .NET’s reflective capabilities really help out here.
You can make the base class as an Interface in a generic DLL, and then for each task you need to have a new DLL with the TASK DoWork implementation based on the base interface.
In your database, for each row you can add the TAKS DLL path (or you can only add the assembly name and make all DLLs to be installed in a pre-defined directory). Then using the .NET reflection you can load the TASK assembly and dynamically invoke the “DoWork” method as all based on your base interface.
This is better to have the code in DB, as tasks dlls will be pre-compiled and more secure. And Also you won’t need to rebuild the main application everytime a new task get added.