DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Super Signals in ASP.NET Core

Super Signals in ASP.NET Core

We take a look at an open source project created by a DZone member that makes it easier to share data between entities in a web app.

Thomas Hansen user avatar by
Thomas Hansen
CORE ·
Apr. 05, 19 · Tutorial
Like (4)
Save
Tweet
Share
12.73K Views

Join the DZone community and get the full member experience.

Join For Free

I work almost exclusively with ASP.NET Core and .NET Standard, and I attempt to make everything as modular as possible. This creates a problem for me, since every now and then I need the ability to have one module invoke functionality, or retrieve data from another module. In a truly modular design, though, the handled info and its handler should not know anything about each other, and they should not be dependent upon any aspect of the other. Super signals to the rescue!

Web app data transfer

For instance, in my own library, Magic, I have a user domain type in its auth module. Other modules might be interested in being notified whenever a user is deleted for some reason. I could, of course, expose a classic event on my user service or something, but this would create a dependency between these two different projects. Which again would result in my inability to easily exchange any of these two projects with alternative implementations. So we want a mechanism that allows for cross module communication, without dependencies, preferably without the signaled and the signaling entities knowing anything about each other's existence. Below is how I chose to implement this:

/*
 * Deletes the user with the specified id.
 */
public void Delete(Guid id)
{
    using (var transaction = Session.BeginTransaction())
    {
        /*
         * Here we signal any other modules that a user is
         * going to be deleted.
         */
        _signaler.Signal("user.deleted", new JObject
        {
            ["id"] = id,
        });
        base.Delete(id);
        transaction.Commit();
    }
}

Other modules again might "subscribe" to the above "signal," by creating a class that implements the ISlot interface. Below is an example of another class subscribing to the above signal.

[Slot(Name = "user.deleted")]
public class UserDeletedSlot : ISlot
{
    public void Signal(JObject input)
    {
        var userId = input["id"].Value<Guid>();
        /*
         * Use the userId for whatever purpose you see fit here.
         * Notice, you can also return values to caller by
         * simply modifying the "input" JObject, and add values
         * that will be returned to the caller.
         */
    }
}

Notice that these two places do not need to share as much as a single POCO type. They don't even need to know about the existence of each other, and, in fact, any of these modules could be completely removed from the AppDomain without causing either runtime failure or compilation failure. Yet still you can easily serialize rich POCO graph types, due to the usage of JObject as the data transfer mechanism.

I refer to this idea as "signal" and "slots," named after its inspiration from the Qt library that was the first ones to use this naming convention (as far as I know). However, due to the zero dependencies idea, passing data instead of strongly typing it, I have taken the liberty to refer to the idea as "Super Signals."

Check out the entire code base here.

ASP.NET ASP.NET Core Signal

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • Pipes And Filters Pattern
  • How To Use Terraform to Provision an AWS EC2 Instance
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: