Nested Interfaces and Mega Classes #1651
Replies: 1 comment
-
|
Refit does not support property-style nested interface composition like adminApi.INotification.View(). Each Refit interface maps to one flat client, and the generator keys generated implementations by simple type name, which is why three same-named interfaces in different namespaces collide. Practical patterns that work today:
services.AddRefitClient<IAdminNotificationApi>().ConfigureHttpClient(...);
services.AddRefitClient<IExternalNotificationApi>().ConfigureHttpClient(...);Then group them yourself in a façade if you want adminApi.Notifications.View(): public class AdminApi(IAdminNotificationApi notifications, IAdminUserApi users)
{
public IAdminNotificationApi Notifications { get; } = notifications;
public IAdminUserApi Users { get; } = users;
}This gives the call-site ergonomics you described without unique method names across the whole API, and keeps files split per controller.
The name-collision on same-named interfaces in different namespaces is a real limitation of the generator (it disambiguates by simple name). A thin façade is the cleanest workaround for now. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone 👋 I'm working on some integration tests for a handful of very large ASP.NET applications. In the source code, these endpoints are split up into "controllers", and I'm trying to have my test code somewhat mirror that. It also makes sense to have related methods in different files from a logical perspective. Let's say I have an "Admin" API and an "External" API. For the Admin API, I might have some interfaces in different files like this:
And the External API would look like this:
Of course, when you have three or more interfaces with the same name in one project, Refit refuses to auto-generate implementations of those interfaces, even if they're in different namespaces.
One possible solution is to instead generate a single interface using partials:
This means that every method must have a unique name across the entire API, which does end up feeling pretty redundant and silly.
I was wondering, does anyone know if there's some way to structure your interfaces such that you could consume it like this?
That's probably too specific for an auto-generator, but it seemed like it wouldn't hurt to ask.
Beta Was this translation helpful? Give feedback.
All reactions