Milan Jovanović Profile picture
May 29 3 tweets 1 min read Twitter logo Read on Twitter
What is the simplest way to create middleware in .NET?

Middleware allows you to introduce additional logic before or after executing an HTTP request.

There are three approaches to creating middleware:
- With Request Delegates
- By Convention
- Factory-Based

#dotnet Image
The simplest one is using request delegates.

But you're missing out on:
- Strong typing
- Dependency injection

I think it makes sense for simple use cases.
If you want to learn more about .NET and software architecture, consider subscribing to my newsletter.

→ Join 22.000+ engineers here: milanjovanovic.tech

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Milan Jovanović

Milan Jovanović Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mjovanovictech

Apr 19
What is a webhook?

Imagine this: You have a website or an application, and you want it to automatically do things without you having to trigger them manually.

That's where webhooks come in! Image
Webhooks are like little messengers that allow one system to send a message or data to another system in real time whenever a specific event happens.

It's like getting a notification on your phone when someone comments on your social media post or when you receive an email.
Webhooks work similarly but for systems and applications.

A webhook is a way for one system to send a message or data to another system in real-time whenever something important happens.

There are many ways that webhooks can be helpful.
Read 10 tweets
Apr 19
Do you want to get better at writing high-quality code?

Here is what I would do:
- Study Object-Oriented Programming and Design patterns
- Try to introduce all the possible abstractions
- Have an epiphany all of a sudden
- Strive to simplify my code
- Prosper Image
This process typically takes a few years, as you can see in the graph.

The graph shows a code complexity curve across years of programming.

We all have to go through the phases of complexity to learn to appreciate simple code.
I'm somewhere past the epiphany phase, starting to appreciate simple code again.

I hope this is evident from the recent shift in my writing, advocating for simplifying things.

Of course, I still appreciate a good design pattern or two.

Who doesn't?
Read 6 tweets
Apr 17
What is the required keyword in C#?

It was added in C# 11.

When creating a new object instance, you can use the required keyword to indicate that a field or property must be initialized.

Have you used the C# required keyword already?

#dotnet Image
If you try to create a new object instance and don't assign a value to a required property, you'll get a compile error.

Before this, you had to use constructors with all the required values as parameters.
Now you can also work with object initializers and not worry about members not having a value assigned.

In combination with nullable reference types, you can create a robust design.
Read 4 tweets
Mar 29
Most people think Redis is just used for caching.

But it can do so much more! Here are a few use cases for Redis:
🔐 Distributed locks

Redis can be used as a distributed lock to ensure that only one process can access a resource.

This can be useful in distributed systems where multiple processes might try to access the same resource simultaneously.
📈 Counters

Redis can be used to maintain counters, such as the number of times a particular event occurs.

This can be useful in various applications, such as tracking the number of page views on a website.
Read 10 tweets
Mar 28
Performance problems are part of programming.

I've run into them a thousand times.

APIs, algorithms, databases - you name it.

Over time, I learned something:

Every single problem is a lesson.
It forces you to stop, think through, and find a way around it.

API output caching made no difference?
Try something else.

Linear complexity algorithm is worse?
Try something else.

Database index didn't help?
Try something else.
So, when you face a tricky problem in your job, remember:

The problem isn't the end. It's an opportunity.

Find another solution: Try it. Test it. Make it better.
Read 6 tweets
Mar 20
What's the easiest way to significantly improve API performance?

You can introduce a memory cache and get a 10x boost in speed.

Querying from a cache is typically faster than querying a database.

You're using a fast access path by querying with a given key.
A cache like Redis is commonly used.

However, do also consider the IMemoryCache in .NET.

It's excellent for simple caching scenarios or when you don't need a distributed cache.

Your only limit is the available RAM on the server.
You can use the Decorator pattern to add a caching component to your services where you want to improve performance.

But what are the downsides?

The cache store is volatile, and you can lose data quickly.
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(