Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Super Signals in ASP.NET Core

DZone 's Guide to

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.

· Web Dev Zone ·
Free Resource

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.

Topics:
architecture ,design patterns ,c# ,web dev ,asp.net core tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}