假设我有一个 FoodEstablishment 类,现在我想做简单的 CRUD 操作,我该怎么做呢?
我为服务定义了一个接口,但是为什么呢?
public interface IFoodEstablishmentService
{
Task<int> AddAsync(FoodEstablishment oFoodEstablishment);
FoodEstablishment SelectByFoodEstablishmentId(long id);
Task<int> UpdateAsync(FoodEstablishment oFoodEstablishment);
Task<int> DeleteAsync(FoodEstablishment oFoodEstablishment);
}
然后我将为该特定服务实现该合同或接口
public class FoodEstablishmentService : IFoodEstablishmentService
{
public async Task<int> AddAsync(FoodEstablishment oFoodEstablishment)
{
// Insert Operation
return result;
}
public FoodEstablishment SelectByFoodEstablishmentId(long id)
{
// Select Logic
return oFoodEstablishment;
}
public async Task<int> UpdateAsync(FoodEstablishment oFoodEstablishment)
{
// Update Logic
return result;
}
public async Task<int> DeleteAsync(FoodEstablishment oFoodEstablishment)
{
// Delete Logic
return result;
}
}
所以在我的主程序或我希望使用服务的地方,我会这样做
IFoodEstablishmentService oFoodEstablishmentService = new FoodEstablishmentService();
FoodEstablishment oFoodEstablishment = // Input might be from views;
oFoodEstablishmentService.AddAsync(oFoodEstablishment);
所以到目前为止,这似乎是一个额外的步骤,我们可以直接完成
FoodEstablishmentService oFoodEstablishmentService = new FoodEstablishmentService();
FoodEstablishment oFoodEstablishment = // Input might be from views;
oFoodEstablishmentService.AddAsync(oFoodEstablishment);
但是,如果我可能需要通过队列而不是直接将插入逻辑传递给服务器,等待插入操作完成然后返回结果,而不是传递队列然后队列工作者处理这些操作,可能不会排队插入的最佳主意,但对于接口示例来说绝对是好的:-)。所以现在我要做的是,创建另一个类 FoodEstablishment 实现相同的合同 IFoodEstablishment。
public class FoodEstablishmentQueueService : IFoodEstablishmentService
{
public async Task<int> AddAsync(FoodEstablishment oFoodEstablishment)
{
// Insert Queue Operation
return result;
}
public FoodEstablishment SelectByFoodEstablishmentId(long id)
{
// Select Queue Logic
return oFoodEstablishment;
}
public async Task<int> UpdateAsync(FoodEstablishment oFoodEstablishment)
{
// Update Queue Logic
return result;
}
public async Task<int> DeleteAsync(FoodEstablishment oFoodEstablishment)
{
// Delete Queue Logic
return result;
}
}
所以现在如果我想使用队列版本,我会这样做
IFoodEstablishmentService oFoodEstablishmentService = new FoodEstablishmentQueueService();
FoodEstablishment oFoodEstablishment = // Input might be from views;
oFoodEstablishmentService.AddAsync(oFoodEstablishment);
我们可以通过使用类的旧方法来做到这一点,但是这会将类实例化与特定类绑定,这对于扩展来说有点僵硬,现在 FoodEstablishmentQueueService 也可以做其他事情,创建另一个方法直到合同有效,所以接口是联系以确保一致性,想象一个人在做普通版本,另一个人在做队列版本,或者有人在做缓存版本,除非它预先指定了工作合同并且人们最终不会交叉检查所有内容,否则签名可能会出现问题。
同样,让我们考虑其他使用预定义类型(如 IEnumerable)的简单示例。假设我传递了一个 FoodEstablishment 集合列表并返回自定义排序列表
public FoodEstablishment[] SortFoodEstablishment(FoodEstablishment[] list)
{
foreach(var oFoodEstablishment in list)
{
// some logic
}
return sortedList;
}
所以我们会这样使用它
FoodEstablishment[] list = new FoodEstablishment[]{ //some values }
var listSorted = oFoodEstablishmentService.SortFoodEstablishment(list);
但是如果我们发送列表而不是数组呢
List<FoodEstablishment> list = //some values;
var listSorted = oFoodEstablishmentService.SortFoodEstablishment(list);
我们会得到错误,因为它使用类的严格实现,所以我们使用由 List 实现的 IEnumerable,它基本上消除了从 List 到接口的依赖
public IEnumerable<FoodEstablishment> SortFoodEstablishment(IEnumerable<FoodEstablishment> list)
{
foreach(var oFoodEstablishment in list)
{
// some logic
}
return sortedList;
}
所以通常实施很大程度上取决于情况