© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•8mo ago•
15 replies
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
[HubClient(HubUri = "example")]
public interface IExampleHubClient : IBaseFromOtherAssembly
{
    Task ReceiveExampleCountUpdate(int count);

    [ClientToServerMethod]
    Task<string> SendExampleMessage(string myClientMessage);

    [ClientToServerMethod]
    Task SendWithoutReturnType(string myClientMessage);
}
[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
public 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>
{
    
}
public 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
Proposal: Interface-based definition of HubClients · Issue #74 · ...
Description Currently the source generator runs on HubClients defined via attributes (e.g. the ClientToServerMethod and ServerToClientMethod). Attribute-based example: [HubClient(HubUri = &quot;exa...
Proposal: Interface-based definition of HubClients · Issue #74 · ...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Abstract Class vs Interface
C#CC# / help
10mo ago
❔ abstract class vs interface
C#CC# / help
3y ago
✅ Scoped-based logging: Microsoft interface
C#CC# / help
3y ago
✅ required attribute vs [Required] annotation
C#CC# / help
12mo ago