C#C
C#6mo ago
Dr_Cox1911

✅ Attribute based definition vs. Interface based definition

Greetings,

I'm working on my Source Generator that generates boilerplate code for SignalR communication.
For this I have basically two ways of defining the interface the generator uses to generate the actual code.

I've tried my best to describe it in this issue: https://github.com/MichaelHochriegl/SignalRGen/issues/74

But here is the quick comparison:
  1. Attribute based```csharp[HubClient(HubUri = "example")]public interface IExampleHubClient : IBaseFromOtherAssembly{ Task ReceiveExampleCountUpdate(int count); [ClientToServerMethod] Task<string> SendExampleMessage(string myClientMessage); [ClientToServerMethod] Task SendWithoutReturnType(string myClientMessage);}```
  2. Interface based```csharppublic interface IInterfaceBasedHubServerMethods{ // This will have the server-to-client methods Task SentByTheServer(string message);}
public interface IInterfaceBasedHubClientMethods
{
// This will have the client-to-server methods
Task<string> SentByTheClient(string message);
}

[HubClient(HubUri = "interface-based-hub")]
public interface IInterfaceBasedHub : IBidirectionalHub<IInterfaceBasedHubServerMethods, IInterfaceBasedHubClientMethods>
{

}
```
Which do you prefer and why?
Thanks for your feedback ❤️
GitHub
Description Currently the source generator runs on HubClients defined via attributes (e.g. the ClientToServerMethod and ServerToClientMethod). Attribute-based example: [HubClient(HubUri = &quot;exa...
Was this page helpful?