Git Product home page Git Product logo

Comments (15)

JonCanning avatar JonCanning commented on May 10, 2024

If I understand this correctly, an HttpHandler would behave like middleware?

IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

@JonCanning that's a nice way of thinking of it, micro middleware that is functional and extends the existing pipeline. I know it's preferable not to introduce an extra parameter but given it comes with many benefits (as outlined in my post), I'm hoping people may warm to it!?

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

Actually, the second example in writing middleware in MS Docs is nearly identical to what I'm doing/proposing, cache a next delegate/function, call the function with a context. The only difference really is that we use an option to allow branching/choosing of multiple paths, If it wasn't for this branching requirement, you would just do a functional wrapper/compose for the requestDelegate and not even the change format, but as we know this is restrictive and takes away a lot of the benefit of being able to 'try' paths with fallbacks (like choose handler)

using Microsoft.AspNetCore.Http;
using System.Globalization;
using System.Threading.Tasks;

namespace Culture
{
    public class RequestCultureMiddleware
    {
        private readonly RequestDelegate _next;

        public RequestCultureMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public Task Invoke(HttpContext context)
        {
            var cultureQuery = context.Request.Query["culture"];
            if (!string.IsNullOrWhiteSpace(cultureQuery))
            {
                var culture = new CultureInfo(cultureQuery);

                CultureInfo.CurrentCulture = culture;
                CultureInfo.CurrentUICulture = culture;

            }

            // Call the next delegate/middleware in the pipeline
            return this._next(context);
        }
    }
}

from giraffe.

JonCanning avatar JonCanning commented on May 10, 2024

I had to write a middleware the other day since I wanted to execute the next delegate and do something afterwards, so I approve.

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

There's that benefit too! I'm fully open to alternative implementations provided we can maintain the TCO & pass-through of tasks from child handlers to avoid wrapping sync in async.

My version with 3 parameters fun next fail ctx -> ... removes the need for an option, allows fail to jump right to finish/failover, all TCO, min stack frames, but is a bigger, more breaking change so getting the balance right is important.

from giraffe.

dustinmoris avatar dustinmoris commented on May 10, 2024

If I understood it all correctly, even though a breaking change, the difference is not huge in how someone would build and compose a web application, but with some great benefits overall. I think I am in favour of this change.

from giraffe.

dustinmoris avatar dustinmoris commented on May 10, 2024

I also referenced your blog post here.

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

Is it easiest if I incorporate the task CE, next continuation together with the new router into my develop fork, few people can test and make sure comfortable, then merge? Do one big breaking change only?

from giraffe.

dustinmoris avatar dustinmoris commented on May 10, 2024

Hi @gerardtoconnor I think we should keep the task CE and the continuation changes as two separate PRs please. I think the continuation is a much less risky change than the task CE, which will require a lot less testing as well.

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

Ok, So would you prefer I do continuation or task CE first?

On the task CE, I have found a way to allow overloading of both Task & Task<'T> without type assertions, using extension methods, should make the changeover more seamless.

I will leave the tree router in a 3rd, last PR then.

from giraffe.

dustinmoris avatar dustinmoris commented on May 10, 2024

I think the continuation could be a quicker gain, but what do you think?

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

Well I think both are important for performance so both need to be done but I guess the continuation is little simpler, less breaking.

One of the reasons I wanted to do both together was to do just one big breaking change as doing two in close succession may be taxing on users, If we merge continuation with the Task CE to follow closely after, would be pointless for users to try upgrade till both are in ... I'm just thinking from users perspective?

I have both continuation & task CE updated in src of my gtoc-develop branch if you want to review, need to update test & sample proj still

from giraffe.

dustinmoris avatar dustinmoris commented on May 10, 2024

I agree with you, however the Task CE is a more complex change IMHO and I would want to test its perf impact more thoroughly before merging, while the continuation change I think of as an elegant architectural change with little risk.

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

Cool, I'll rebase off current async branch and do contination PR in next day or so.

from giraffe.

gerardtoconnor avatar gerardtoconnor commented on May 10, 2024

PR added #71

from giraffe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.