DEV Community

Cover image for Polymorphic Type Support in SignalR Hubs
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

Polymorphic Type Support in SignalR Hubs

SignalR, a real-time communication library for .NET applications, introduces exciting new features. The article demonstrates the concept of polymorphic type support within SignalR Hubs.

Overview

Traditionally, SignalR Hub methods expected derived classes as input parameters. However, with the latest enhancements, Hub methods can now accept base classes, enabling more flexible and polymorphic scenarios. This enhancement simplifies code and promotes better design practices.

Implementation Example

Let’s explore an example using C#:

    using Microsoft.AspNetCore.SignalR;
    public class MyHub : Hub
    {
        public void ProcessPerson(JsonPerson person)
        {
            if (person is JsonPersonExtended)
            {
                // Handle JsonPersonExtended logic
            }
            else if (person is JsonPersonExtended2)
            {
                // Handle JsonPersonExtended2 logic
            }
            else
            {
                // Handle other cases
            }
        }
    }
    [JsonPolymorphic]
    [JsonDerivedType(typeof(JsonPersonExtended), nameof(JsonPersonExtended))]
    [JsonDerivedType(typeof(JsonPersonExtended2), nameof(JsonPersonExtended2))]
    private class JsonPerson
    {
        public string Name { get; set; }
        public Person Child { get; set; }
        public Person Parent { get; set; }
    }
    private class JsonPersonExtended : JsonPerson
    {
        public int Age { get; set; }
    }
    private class JsonPersonExtended2 : JsonPerson
    {
        public string Location { get; set; }
    }
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. MyHub: A SignalR Hub that defines the ProcessPerson method. This method accepts a JsonPerson object, which can be either the base class or its derived types.

  2. JsonPerson: The base class representing a person. It includes common properties such as Name, Child, and Parent.

  3. JsonPersonExtended and JsonPersonExtended2: Derived classes that extend JsonPerson. They introduce additional properties (Age and Location, respectively).

Conclusion

By embracing polymorphism, SignalR Hubs become more versatile and adaptable. Developers can now handle diverse data structures efficiently, making real-time communication even more powerful.

Clap if you believe in unicorns & well-structured paragraphs! 🦄📝

Socials: C# Publication | LinkedIn | Instagram | Twitter | Dev.to

buymeacoffee

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more