.NET 如何解决层之间所需的通用性

问题描述 投票:0回答:1

我有一个问题,对某些人来说可能很微不足道。我正在 .NET 中制作一个应用程序,我试图在其中使用分层。应用程序(仅抽象)和基础设施(实现)。我的问题是我想使用 Azure TableClient 来存储 Azure 存储表中的数据。但存储方法需要在该实体上实现 ITableEntity 接口。我只是无法在存储方法的应用程序层中声明这一点,因为我会依赖 Azure.Data.Tables 而我不希望这样。我真的不知道如何用它编写我的界面,然后“覆盖”它,我猜。

应用:

public interface ITableStorage
{
    public Task SaveItemsAsync<T>(IEnumerable<T> entites, string tableName = nameof(T)) where T : ITableEntity; // where T : ITableEntity is problem
}

基础设施:

public async Task SaveItemsAsync<T>(IEnumerable<T> entites, string tableName = nameof(T)) where T : ITableEntity
{
    var tableClient = _tableServiceClient.GetTableClient(tableName);

    ......
}

提前感谢您的建议。

c# .net azure azure-table-storage clean-architecture
1个回答
0
投票

您的界面纯粹基于您希望它实现的目标,而不考虑实现。例如

public interface ICustomerService
{
    public Task AddNewCustomerAsync<T>(Customer customer);
}

完成后添加实现。

public class TableStorageCustomerService : ICustomerService
{
    public async<Task> AddNewCustomerAsync<T>(Customer customer)
    {
        //map your customer class to your table entity
        //save the table entity to your table storage
    }
}

如您所见,您的界面中根本没有实现细节。由于您只使用

Customer
,因此您可以自由地将其映射到您在服务实现中数据库可能需要的任何类。

© www.soinside.com 2019 - 2024. All rights reserved.