C# 如何开始在 .NET 中创建应用程序 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/630971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start creating an application API in .NET
提问by Magnus
In the company I work for we have a desktop application developed in .NET that we like to open up some parts for other software developers.
在我工作的公司里,我们有一个用 .NET 开发的桌面应用程序,我们喜欢为其他软件开发人员开放一些部分。
I see it like some kind of public methods that other software can access. Is there any standard approach, best practices or other experience of this?
我认为它就像其他软件可以访问的某种公共方法。是否有任何标准方法、最佳实践或其他经验?
I guess you could do this with Win32 calls, C/C++ programming etc. But I'm mainly a .NET developer and have some years of experience programmming in mainly VB.NET but also some C#. I hope finding a way to do this within .NET. But it should be accessible for software developed in other languages.
我想你可以用 Win32 调用、C/C++ 编程等来做到这一点。但我主要是一个 .NET 开发人员,有多年的主要 VB.NET 编程经验,但也有一些 C#。我希望在 .NET 中找到一种方法来做到这一点。但它应该可以被其他语言开发的软件访问。
Best regards/ Magnus
最好的问候/马格努斯
采纳答案by NotMe
There are two ways to go about this. The first involves using the Plug In / Module pattern. Start herefor that.
有两种方法可以解决这个问题。第一个涉及使用插件/模块模式。 从这里开始。
A second way is to simply expose your API logic through well documented assemblies.
第二种方法是通过有据可查的程序集简单地公开您的 API 逻辑。
The way you pick really boils down to how you want it implemented. For example, Adobe Photoshop uses the plug-in pattern to great extent. MS Office uses both plug-ins as well as provides an object interface for working with the application.
您选择的方式实际上归结为您希望它如何实施。例如,Adobe Photoshop 在很大程度上使用了插件模式。MS Office 使用插件并提供对象接口来处理应用程序。
UPDATE:Objects in .Net assemblies can be exposed to be consumed by external applications simply by setting the objects scope to Public. This includes windows forms, business objects, enum's, etc.
更新:.Net 程序集中的对象可以公开以供外部应用程序使用,只需将对象范围设置为 Public。这包括窗体、业务对象、枚举等。
Just think about it from the external developer's perspective. What information will they need to have in order to be successful at extending / controlling your product? Whatever that is, put it in the help file.
只是从外部开发人员的角度考虑一下。他们需要哪些信息才能成功扩展/控制您的产品?不管是什么,把它放在帮助文件中。
Also, make use of the auto-documentation features of .Net. This includes adding xml documentation the the IDE already knows how to read and display via intellisense. For example:
此外,请利用 .Net 的自动文档功能。这包括添加 IDE 已经知道如何通过智能感知读取和显示的 xml 文档。例如:
/// <summary>
/// Gets all active documents for the specified customer.
/// </summary>
/// <param name="customerId">The Id of the customer.</param>
/// <returns>A list of documents for this customer.</returns>
public static List<Document> GetDocuments(int customerId)
{
//... do something ...
}
回答by Dave Swersky
Developing a public API depends greatly on the functionality of your program. If you want to expose an API that is accessible to other languages, XML web services is generally the way to do it. This may not be practical for a desktop-only application, however.
开发公共 API 在很大程度上取决于您的程序的功能。如果要公开其他语言可以访问的 API,通常可以使用 XML Web 服务。然而,这对于仅限桌面的应用程序来说可能不切实际。
回答by John Saunders
"Expose the application through a public API" is not a detailed requirement definition. Before designing anything, get detailed requirements.
“通过公共 API 公开应用程序”不是详细的需求定义。在设计任何东西之前,获取详细的需求。
For an API, I recommend that you start with the requirements, and then proceed to use cases. Since this is an API, you should actually write some code using the proposed API, to see how it feels to program it. The code you write should cover the use cases you start with. You might even write that code in unit tests, then create the API through test-driven development and refactoring.
对于 API,我建议您从需求开始,然后继续使用案例。由于这是一个 API,您实际上应该使用建议的 API 编写一些代码,以了解对其进行编程的感觉。您编写的代码应涵盖您开始使用的用例。您甚至可以在单元测试中编写该代码,然后通过测试驱动的开发和重构来创建 API。
But anyway, since use cases focus on how the user will use the software, they will help you focus on what parts of the software the user uses directly. One use case may use another; but if a user does not directly use a particular use case, then that is one that need not be exposed publicly.
但无论如何,由于用例关注用户将如何使用软件,它们将帮助您关注用户直接使用软件的哪些部分。一个用例可能会使用另一个;但如果用户不直接使用特定用例,则该用例无需公开。
Note that this also has helped me in the development of web services. The use cases directly accessed by the user become the operations to expose through the service. Any use case that the user does not directly invoke will remain hidden, as implementation.
请注意,这也有助于我开发 Web 服务。用户直接访问的用例成为通过服务公开的操作。任何用户未直接调用的用例都将作为实现隐藏。
回答by Bob The Janitor
Create an API class that exposes what functions you want people to have access to and Register it for COM interop project ->properties -> build -> Register for COM interop
创建一个 API 类,该类公开您希望人们访问的功能并将其注册到 COM 互操作项目 -> 属性 -> 构建 -> 注册 COM 互操作