Continuing from Our Scrutor Journey...
In the previous blog, Scrutor Makes .NET DI Easy Part - I we looked at how Scrutor simplifies service registration in .NET. It showed how to scan assemblies automatically and register services in bulk, making it easier to avoid writing repetitive AddScoped<> lines and keeping the codebase cleaner.
If you haven’t read it yet, I suggest checking it out first. It will help you understand what Scrutor is and how it works.
In this blog, we’ll explore two more powerful features.
- Registration strategies
- Decorator pattern
Let’s dive in and see how Scrutor makes all of this much easier and cleaner compared to plain MS.DI.
What Are Registration Strategies?
By default, .NET’s built-in DI adds every service you register. If you do this
It gets added to the service collection. But what if the same interface is already registered?
- Should we keep the old one?
- Should we replace it?
- Or add both?
That decision is called Registration Strategy. Scrutor provides you a method called .UsingRegistrationStrategy(...) that you can use to control what happens when a duplicate is found.
❌ Problem with MS.DI
MS.DI doesn't let you control this directly. If you do this:
Then SecondService will replace FirstService silently (for GetService
✅ How Scrutor Solves It
Scrutor gives you control using .UsingRegistrationStrategy(...)
Examples
- Append (Default) Adds everything, last one wins
- Skip Keeps the first one and ignores duplicates:
- Replace Overrides any earlier registration
💰Bonus: You can also specify what kind of replacement
- ServiceType: Replace any existing registration for the interface
- ImplementationType: Only replace if both interface and implementation match
Now you can say:
- Skip: Don’t register if already exists.
- Replace: Replace old one.
- Append: Keep both (useful for IEnumerable
).
Simple and safe!
What is the Decorator Pattern?
The Decorator Pattern means wrapping one service inside another to add extra behavior. Let’s say you have:
Now you want to add logging before and after, without changing ReportService. You create:
❌ MS.DI: No Decorator Support
MS.DI does not support decorators directly. So you need to build it manually:
It is a bit messy and difficult to manage..
✅ Scrutor: Super Easy Decoration
With Scrutor, it’s just:
Even for multiple decorators, it handles the order:
This wraps like:
🧠 Final Thoughts
MS.DI is great for simple cases. But if you:
- Have a modular project
- Use plugins or microservices
- Want better control over service registrations
- Want to add decorators without the mess
…then Scrutor is your friend.
Whether you're building a big app or a small one, Scrutor makes your dependency injection cleaner and smarter.