两种不同类的扩展方法

两种不同类的扩展方法

本文介绍了两种不同类的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,在C#中,我通过OData API消耗数据.实际上,有两个不同的OData端点(两个不同的模型),其中一个是另一个的超集.

I have a situation where in C# I consume data via an OData API. Actually, there are two different OData endpoints (two different models), where one is a superset of the other.

我创建了两个不同的C#项目(A和B),其中包含与两个API交互的代码.该代码是使用插入OData Connected服务扩展自动生成的.结果,项目A和项目B都包含一个类 ODataService ,这是要与之交互的主要类型.根据我需要完整的API还是简化的API,我分别引用项目A或B.

I have created two different C# projects (A and B) which contain code to interact with the two API's. The code is automatically generated using Unchase OData Connected Service extension. As a result, project A and project B both contain a class ODataService, which is the main type to interact with. Depending on whether I need the full API or the reduced API, I reference project A or B correspondingly.

我想在单独的项目C中为 ODataService 类编写扩展方法,这样无论我选择引用项目A(完整API)还是项目,我都可以使用这些扩展方法.B(简化的API).

I would like to write extension methods for the ODataService class in a seperate project C, such that I can use these extension methods no matter if I choose to reference project A (full API) or project B (reduced API).

什么是一个好的方法?

推荐答案

一种可能的解决方案是:

One possible solution would be:

  1. ODataService -s创建一个接口(例如: IODataService )
  2. 为实现接口的服务创建部分类:
    (例如:公共子类ODataService1:IODataService {...} )
  3. IODataService
  4. 创建扩展方法
  1. Create an interface for ODataService-s (e.g.: IODataService)
  2. Create partial classes for your services that implement the interface:
    (e.g.: public partial class ODataService1: IODataService {...})
  3. Create the extension methods for the IODataService

这篇关于两种不同类的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:09