Search code examples
c#sqliteportable-class-library

C# add reference to sqlite.dll in class library project from another project


Here are the steps I used to build these two Projects ADBTest and DBControl.
ADBTest was built first it implements SQLite DB CRUD functions I added System.Data.SQLite with NuGet.
Ran the project everything functions great!
Next I built the DBControl it is a Windows Forms Class Library.I added this to the ADBTest project.
Added Existing Project and Added Reference to DBControl to ADBTest.
Now I tried to add Reference to the SQLite.dll in ADBTest the DBContro.csproj says it there I think
Here is were I start getting LOST. Will Post some code and a screen shot.
I did various using declaration but that does not work nothing is found to USE.
So where did I make the mistake or is the code design all off?
Also not sure how to call this code from frmStart? Work on that later one question RULE sucks!
DBControl has NO form so when it is done with makeFriendsTable will try to send it back to frmStart round trip flight I hope.

public class DBControl : UserControl
{
    public static string dbName = "APeople.db";
    // Class1

    private void UserControl_Load(object sender, EventArgs e)
    {
        makeDB();
        makeFriendsTable();
    }

    private void makeDB()
    {
        throw new NotImplementedException();
    }

    private void makeFriendsTable()
    {
        throw new NotImplementedException();
    }

    public class MakeDB
    {
        public void makeDB()
        {
            using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
                if (!File.Exists(dbName))
                {
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
        }

Here is the frmStart Call

        private void btnCreateDB_Click(object sender, System.EventArgs e)
    {
        Hide();
        using (var form = new DBControl.DBControl())
        Show();

        //DBControl dB = new DBControl();
        //dB.Show();
    }

The Weird Part after putting the SQLite in after the two projects were joined in Visual Studio
First NuGet insisted they be installed in BOTH Projects
Then the DB and Table were created but I could not find the DB in the Solution Explorer
Went looking in the source folder on C Drive it was in DBCall Debug folder


Solution

  • I think that can be a design problem. Is not so simple as seems to be, and even working with a correct design can bring some problems, so the question for me is useful.

    The common work is made layers of work, one with the interface, one with the logic, one with the data handle.... that's the three layers model. There are several designs to work, but this is the common one.

    The layer that handles the data is often called Data Layer (DAL) and in microservices or SQL servers is often called Data Service (DAS)

    The topic indeed is very large to explain but you can get a reference with the following image. Of course you can see this model in a lot of different ways but you will find the same principle:

    enter image description here

    As I understand from your case, you have a DBControl that handle the data work. So this is the one who carried the reference to the database and SQLite.dll. By the way you can use Entity Framework to work with SQLite and make an scaffolding to the archive.

    In any case, this DBControl will carry the "repositories" with the methods where you make the calls to the database, being ADO or EF. This is the Persistance and can handle also the Domain Model

    Those methods will return not the EF or DB models, instead your own models that will help and work in your "business logic". Those are the classes, objects, models, etc that the DBControl will export for other libraries or the interface layer where the apps will work.

    Since the DBControl will returns your own models, the interface won't need the SQLite reference, but the DBControl reference in order to get the models you work there

    The image is a design to follow, but is not a rule. You can have all the layers in the same project and every layer is a .cs file, or in the same file and every layer is a method, the trick is the organization. And maybe you don't have all the layers, it depends on your design.

    If you require more detail in this answer please comment, I'll glad to help you