C# 如何从DLL中的组件公开类类型

C# 如何从DLL中的组件公开类类型,c#,windows,dll,types,.net-framework-4.8,C#,Windows,Dll,Types,.net Framework 4.8,我使用的是一家名为Devart的公司的组件。该组件生成的数据模型与实体框架非常相似,只是它用于SQLite。我的数据模型和数据上下文等与实体框架的工作方式非常相似 我创建了一个Windows.NetFramework DLL类库,我将使用它来处理许多与数据库相关的活动。在这个DLL中,我有我的数据模型和数据上下文,其中当然包含我所有的表类类型等 在我的DLL中,我可以编写以下代码: public dbSales.tbl_Customers GetCustomer_byCustomerID(

我使用的是一家名为Devart的公司的组件。该组件生成的数据模型与实体框架非常相似,只是它用于SQLite。我的数据模型和数据上下文等与实体框架的工作方式非常相似

我创建了一个Windows.NetFramework DLL类库,我将使用它来处理许多与数据库相关的活动。在这个DLL中,我有我的数据模型和数据上下文,其中当然包含我所有的表类类型等

在我的DLL中,我可以编写以下代码:

   public dbSales.tbl_Customers GetCustomer_byCustomerID(int _customerID)
   {
          dbSales.tbl_Customers selectedCustomer = (from dbContext.tbl_Customers 
                                                    where i.CustID == _customerID 
                                                     Select i).FirstOrDefault();
   }
然后在我的另一个程序中,我想引用DLL和该方法来检索数据。我不知道如何公开表的类型,以便执行以下操作:我的DLL包含一个名为DataHelper的类

(为了清晰起见,代码以展开形式编写)

我不知道如何公开类型:tbl_客户。这完全是我的无知。我发现的唯一解决方法是使用var执行以下操作。我希望能够指定我的类型,但不喜欢使用var。以下操作有效:

var selectedCustomer = DataHandler.DataHelper.GetCustomer_byCustomerID(45);
我尝试过公开不同的东西,比如模型、上下文等,但仍然好像我只是公开方法和属性。我只是不知道类型以及如何使用它们。我真的很想找到一本解释类型和如何使用它们的好书


如果有人能提供帮助,我们将不胜感激。

简单的解决方案是,您可以将类型标记为public,如下所示:

public class MyClass{
}
默认情况下,类型是内部的,因此只能从同一项目中访问它们

比公开数据库实体类型更好的解决方案是创建域对象的类库。它们将是包含属性但不包含业务逻辑的类型,并且它们的属性将与要从数据访问项目中的相应类型中公开的属性相同。然后,您可以使用automapper之类的工具从数据访问层返回这些类型。这有助于封装数据访问层

例如,如果您的数据访问层具有如下类型的
Customer

internal class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
}
域层可以有一个名为
Customer
的公共类,该类具有相同的属性。然后,您可以实现与以下类似的存储库:

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);
    }
}
使用Domain=MyDomainProject;
公共接口ICCustomerRepository
{
任务getCustomerByDaSync(int customerID);
}
内部类CustomerRepository:ICCustomerRepository
{
私有只读DbContext上下文;
专用只读IMapper映射器;
公共CustomerRepository(DbContext上下文,IMapper映射器)
{
this.context=上下文;
this.mapper=mapper;
}
公共异步任务GetCustomerByDaSync(int customerID)
{
var customer=wait context.customer.FirstAsync(c=>c.CustomerID==CustomerID);
返回mapper.Map(客户);
}
}

好的,谢谢。我将处理您在此处提供的信息。我没有意识到类型是这样工作的。我以前考虑过创建自定义类对象并重新映射,但后来认为这可能比需要做的工作更多。谢谢
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);
    }
}