I'm new to the minimal API in .NET 7's WebAPI using C#. I have numerous services that resemble the following:
我对使用C#的.NET7‘S WebAPI中的最小API还很陌生。我有许多类似以下服务的服务:
public class RepoService {
public async Task<IEnumerable<Repo>> GetRepos()
{
var repos = db.get...;
return repos;
}
public async Task<Repo> UpdateRepo(Repo repo)
{
repo.... = ...;
return repo;
}
}
In the minimal API, I define a route using a URL and a lambda function. This lambda function processes the request and maps it to a specific type. So, I need to inject the RepoService and invoke the corresponding method, passing the necessary parameters.
在最小的API中,我使用URL和lambda函数定义了一个路由。这个lambda函数处理请求并将其映射到特定类型。因此,我需要注入RepoService并调用相应的方法,传递必要的参数。
Essentially, there's an API layer (represented by the lambda function) that bridges the gap between the URL and the service's methods.
从本质上讲,有一个API层(由lambda函数表示)在URL和服务方法之间架起了桥梁。
Is there a way to bypass this API layer? Can I directly pass the service method as the second argument to the route definition?
有什么方法可以绕过这个API层吗?我可以直接将服务方法作为第二个参数传递给路由定义吗?
The only method I've found so far is by converting all the functions to static. However, I'd prefer not to do this, as the service should remain a service and shouldn't need to be aware of the API layer.
到目前为止,我找到的唯一方法是将所有函数转换为静态函数。然而,我不希望这样做,因为服务应该仍然是一个服务,不应该知道API层。
更多回答
Is there a way to bypass this API layer? Can I directly pass the service method as the second argument to the route definition?
Have you tried it? If so and it hasn't work, can you provide code that you're trying to make work?
有什么方法可以绕过这个API层吗?我可以直接将服务方法作为第二个参数传递给路由定义吗?你试过了吗?如果是这样,但它没有起作用,你能提供你试图让它起作用的代码吗?
优秀答案推荐
我是一名优秀的程序员,十分优秀!