0

I'm planning to extend my application. At the moment, I have Data.dll that uses Data Access that uses SQL Server with Stored Procedure. What I would to do is to make it flexible where changing the DLL will change the Data Access to any provider like MySQL or Oracle. The goal is to avoid making changes on the Main Application. I'm just not sure what is the best way to implement this in C#. At the moment, I'm using win32 but wanted to use the same concept on my other ASP.NET Core API. I'm planning to use App.config for Win32 or Web.config for the WebApi to make changes on the configuration.

-----UPDATE to MAKE IT MORE CLEARER ---------

I've just created 3 Projects Project# 1 - A DLL that will Perform CRUD on SQL Server and produce a file called DLLClassLibrary.dll

public class Data
{
    public string GetName()
    {
        return "NAME DLL SQL SERVER";
    }
}

Project# 2 - A DLL that will Perform CRUD on MySQL and produce a file called DLLClassLibrary.dll

public class Data
{
    public string GetName()
    {
        return "NAME DLL MYSQL";
    }
}

Project# 3 - The MAIN app where I will consume the DLL. On the program, I've created a reference to a file called DLLClassLibrary.dll.

public partial class frmMain : Form
{
    DLLClassLibrary.Data data = new DLLClassLibrary.Data();
    public frmMain()
    {
        InitializeComponent();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        txtOut.Text = data.GetName();
    }
}

Test Done using Project # 1 generated DLL

enter image description here

Test Done using Project # 2 generated DLL

enter image description here

As you can see by placing the same dll file the behaviour changes. The question is what is the best way to implement these 2 DLLs without removing one another and without Hard Coding it on my Main. Or is that the only option?

  • 1
    What are you actually asking here? You don't have a defined question that isn't too broad for this site. – Thom A Jul 26 '20 at 12:39
  • I'm wondering if there's a simple way of implementing DLL. Is it just a matter of replacing DLL with the same function signatures? or are there anything that I need to be aware of? – Reydan Gatchalian Jul 26 '20 at 12:50
  • so you want to share your data access project (dll) between a win32 app (presumably a desktop/service app) and a .Net core web app? and you want a your data access layer to handle different providers like mysql and oracle? – Glynn Hurrell Jul 26 '20 at 12:59
  • 1
    Consider programming against the ADO.NET IDb* interfaces and using a factory pattern to create the provider-specific objects. – Dan Guzman Jul 26 '20 at 13:01
  • But, you should not use specific functions which are only working in MySQL or ORACLE, or MSSQL., like i.e. `STRING_AGG` , `GROUP_CONCAT`, `LISTAGG` (for respectively, MSSQL, MySQL, Oracle) – Luuk Jul 26 '20 at 13:07
  • Sorry for the confusion. I've updated the ask with a simple example. – Reydan Gatchalian Jul 26 '20 at 13:26

1 Answers1

2

I read this question as meaning you'd like to be able to change the DLL used for DB access by providing the DLL and changing your *.config file without recompiling.

To do that you would have to put the name of the DLL or the path to it in your config file and load the DLL dynamically from your application.

I'm not sure if you're changing your database often enough to make it worth it, but let's assume it is.

The way to do this is different between the old .NET framework for Windows and .NET Core.

In the classic .NET framework, you would use AppDomains to dynamically load DLLs at runtime, and then reflection or dynamics can be used to access the types and methods in the DLL.

Loading DLLs at runtime in C#

    var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");

    var class1Type = DLL.GetType("DLL.Class1");

    //Now you can use reflection or dynamic to call the method. I will show you the dynamic way

    dynamic c = Activator.CreateInstance(class1Type);
    c.Output(@"Hello");

In .NET Core, AppDomains aren't a thing, and you'd use an AssemblyLoadContext (only in .NET Core 3.0 and above).

There's a nice tutorial on Microsoft Docs on how to do that to realize plugins for your application:

https://learn.microsoft.com/dotnet/core/tutorials/creating-app-with-plugin-support

HalvarF
  • 135
  • 5
  • So can I have a logic within the Main program to read the config to check what type of DB access to use i.e. if dbconfig == 'SQLSEVER' load this DLL else load another DLL? How does other applications implements plugins like PAINT.NET. You place the dll in a folder and the application will just add the functionality in the app. – Reydan Gatchalian Jul 26 '20 at 13:30
  • I found a good Implementation of Plugins. https://www.codeproject.com/Articles/6334/Plug-ins-in-C Although, this is not what I have in mind but I can use this for this question. – Reydan Gatchalian Jul 26 '20 at 17:40