CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.06k stars 174 forks source link

Custom Action on After hook #248

Closed Jaxelr closed 4 years ago

Jaxelr commented 4 years ago

I currently have a small library i am trying to use for Caching responses, the way the behavior is defined is somewhat similar to the RequiresAuthentication extension method:

        public static void EnableCaching(this CarterModule module)
        {
            module.Before += async (ctx) => await CheckCache(ctx);
            module.After = async (ctx) => await SetCache(ctx);
        }

The problem i'm facing is that the After Delegate has a protected set, which means i cannot set the module.After outside of the CarterModule class. I'm sure there's a reasoning behind this, is there an alternative for me to do this in a more general fashion ?

JoeStead commented 4 years ago

It may be better purposed as middleware before Carter is even hit, so you will do something along the lines of

var cacheResult = await CheckCache(ctx);

if(cacheResult != null)
{
next(); //next here is the carter middleware
}

await SetCache(ctx);

So doing it at the ASP.NET Core middleware level rather than carter.

Hope this helps a bit

Jaxelr commented 4 years ago

Thanks @JoeStead

JoeStead commented 4 years ago

No problem, let me know if you want any help with that middleware

On May 27 2020, at 5:05 pm, Jaxel Rojas notifications@github.com wrote:

Thanks @JoeStead (https://link.getmailspring.com/link/1FC848C5-5B20-4DCC-BDE9-C3620263803E@getmailspring.com/0?redirect=https%3A%2F%2Fgithub.com%2FJoeStead&recipient=cmVwbHkrQUFBWDcyU1BSTkZCRkhJUFRSTUozQ0Y0M0pZTkRFVkJOSEhDS1RKSEpNQHJlcGx5LmdpdGh1Yi5jb20%3D) — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub (https://link.getmailspring.com/link/1FC848C5-5B20-4DCC-BDE9-C3620263803E@getmailspring.com/1?redirect=https%3A%2F%2Fgithub.com%2FCarterCommunity%2FCarter%2Fissues%2F248%23issuecomment-634766741&recipient=cmVwbHkrQUFBWDcyU1BSTkZCRkhJUFRSTUozQ0Y0M0pZTkRFVkJOSEhDS1RKSEpNQHJlcGx5LmdpdGh1Yi5jb20%3D), or unsubscribe (https://link.getmailspring.com/link/1FC848C5-5B20-4DCC-BDE9-C3620263803E@getmailspring.com/2?redirect=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAAX72QUGWUFE7OYYTHTYXLRTU25DANCNFSM4NMIIOIA&recipient=cmVwbHkrQUFBWDcyU1BSTkZCRkhJUFRSTUozQ0Y0M0pZTkRFVkJOSEhDS1RKSEpNQHJlcGx5LmdpdGh1Yi5jb20%3D).