How to Expose Class Types from a Component within a DLL

360 Views Asked by At

I am using a component from a company called Devart. This component produces a data model very much like Entity Framework except it is for SQLite. I have my data models and data context, etc very similar to the way Entity Framework works.

I created a Windows .NetFramework DLL Class Library that I will use to handle many of my database related activities. Within this DLL I have my data models and data context which of course contain all of my table class types etc.

Inside my DLL I can write the following code:

   public dbSales.tbl_Customers GetCustomer_byCustomerID(int _customerID)
   {
          dbSales.tbl_Customers selectedCustomer = (from dbContext.tbl_Customers 
                                                    where i.CustID == _customerID 
                                                     Select i).FirstOrDefault();
   }

Then in my other program I want to reference the DLL and that method to retrieve data. I do not know how to expose the types for my tables so that I can do the following: my DLL contains a class called DataHelper.

(The code is written in expanded form for clarity)

DataHandler.DataHelper.tbl_Customers selectedCustomer = new DataHandler.DataHelper.tbl_Customers();
selectedCustomer = DataHandler.DataHelper.GetCustomer_byCustomerID(45);

I do not know how to expose the type: tbl_Customers. It is simply my ignorance. The only work around I have found is to do the following using var. I like to be able to specify my types though and do not like using var. The following works:

var selectedCustomer = DataHandler.DataHelper.GetCustomer_byCustomerID(45);

I've tried making different things public such as the model, or the context, etc. But still it is as if I am only exposing methods and properties. I am simply ignorant about types and how to work with them. I honestly would like to find a good book that explains types and how to work with them.

If anyone can provide assistance with this, it would be greatly appreciated.

1

There are 1 best solutions below

1
On BEST ANSWER

The easy solution is that you can mark a type as public like this:

public class MyClass{
}

Types are internal by default so they can only be accessed from within the same project.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers

A better solution than making your database entity types public would be to create a class library of domain objects. They would be types containing properties but no business logic and their properties would be the same as the properties you want to expose from the corresponding types in your data access project. Then you could return these types from your data access layer using a tool like automapper. This helps encapsulate your data access layer.

https://github.com/AutoMapper/AutoMapper

For example if your data access layer has a Customer type like this

internal class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
}

You domain layer could have a public class called Customer with the same properties. Then you could implement your repository similar to this:

using Domain = MyDomainProject;

public interface ICustomerRepository
{
    Task<Domain.Customer> GetCustomerByIDAsync(int customerID);
}

internal class CustomerRepository : ICustomerRepository
{
    private readonly DbContext context;
    private readonly IMapper mapper;

    public CustomerRepository (DbContext context, IMapper mapper)
    {
        this.context = context;
        this.mapper = mapper;
    }

    public async Task<Domain.Customer> GetCustomerByIDAsync(int customerID)
    {
        var customer = await context.Customer.FirstAsync(c => c.CustomerID == customerID);
        return mapper.Map<Domain.Customer>(customer);
    }
}