Skip to content

Handling deferred conditional execution in .NET such as social network LIKE button after login

License

Notifications You must be signed in to change notification settings

mykeels/Intents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Intents

Ever wanted something like the Facebook LIKE button that defers the LIKE action till the user is authenticated? Or the tweet button that saves the tweet and tweets it only after login?

If you build a web application with these kind of problems, you'd run into problems like these

That's what Intents helps you do.

Target Environment

.NET Framework 4.5.2 (can be upgraded to support 4.6)

How it works

  • Intents are actions that are subscribed to, and executed only when triggered.
    • Because they are actions, each Intent has a lambda expression or Action<TData> which defines the action.
    • When an Intent is subcribed to, it is called a UserIntent and it contains data of type TData which is passed to the Action<TData> for execution.
  • When an Intent is registered, it becomes globally accessible via the IntentManager.GetIntentManager() singleton.
  • Intents can be saved in any storage system. One of my favorite is in Session Memory. You can implement the Intents.IntentRepositoryContract interface to use other storage media.

How to use (Web)

  • Add the Intents and Intents.Web Nuget packages to your project
Install-Package Intents
Install-Package Intents.Web
  • Import Intents and Intents.Web into your global.asax.cs class
using Intents;
using Intents.Web;
  • Register the Intents on Application Startup in your global.asax.cs file
protected void Application_Start() {
    IntentManager.GetIntentManager(new SessionRepository()).Register(new Intent<string>()
        {
            name = "tweet",
            trigger = "login",
            action = (string tweetData) =>
            {
                Console.WriteLine("Tweet: " + tweetData);
            }
        });
} 
//registers an intent to tweet something when "login" is triggered
  • Create Storage for each user when a Session Starts
protected void Session_Start()
{
    IntentManager.GetIntentManager().CreateSessionStorage();
}
  • To trigger our Intent when the user logins in execute the following statement when login is successful.
IntentManager.GetIntentManager().Trigger("login");

This will trigger all "login" intents the user has subscribed to. But how does the user subscribe to Intents?

  • To subscribe to intents, the IntentManager class has an AddIntentData method that handles this.
 IntentManager.GetIntentManager().AddIntentData<string>("login", "tweet", "Hello @MBuhari ... Nigeria supports you :p");
  • The Intents.Web library also comes with an IntentsController.cs class which exposes Mvc JsonResult endpoints with which users can manipulate their Intents via Http Requests.

    It includes the following endpoints:

    • Subscribe (POST or GET)

    Creates a new subscription to a registered Intent

    - trigger (string)
    - name (string)
    - data (stringified JSON data)
    - redirectUri (string) (optional)
    
    • Trigger (GET)

    Triggers all Intents with a particular "trigger" attribute value

    - trigger (string)
    
    • GetIntents (GET)

    Lists all Intents registered on the system

    • GetUserIntents (GET)

    Lists all user subscriptions to Intents

  • Lean back and watch those Intents fly ... :D

Author(s)

  • Ikechi Michael I.

License

This project is licensed under the Apache License

About

Handling deferred conditional execution in .NET such as social network LIKE button after login

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published