π© Why NOT Use the Explicit Operator for Mapping?
June 1, 2025
π© Why NOT Use the Explicit Operator for Mapping in C#? π€¨
Just because C# lets you do something, doesn't mean you should. Lately, I've seen guys using the explicit operator to map domain models to DTOs. Looks fancy, using something new right? Well, not really you are just dumpling into new features without thinking it through. Here's a quick look:
public class DomainModel { public int Id { get; set; } public string Name { get; set; } } public class Dto { public int Id { get; set; } public string Name { get; set; } public static explicit operator Dto(DomainModel model) { return new Dto { Id = model.Id, Name = model.Name }; } } // Usage: var domainModel = new DomainModel { Id = 1, Name = "Example" }; var dto = (Dto)domainModel;
Sounds cool, but here's why it's not a great idea:
- Not intuitive: Casting should be for simple or related types, not business logic mapping.
- Hides logic:
(Dto)domainModel
looks simple, but there's hidden magic going on. - Hard to maintain: As mapping grows, this gets messy. Reverse mapping? Even worse.
- Breaks clean code: It surprises devs and mixes responsibilities.
- It's just not the C# way: Most devs expect named methods or constructors for this stuff.
- Not the purpose of the explicit operator: It's meant for low-level type conversions, not business logic.
What's the real purpose of the explicit operator
?
It's designed for controlled type conversions between types that are closely relatedβlike converting a value type to another value type, or turning a wrapper object into its underlying value. You use it when automatic (implicit) conversion doesn't make sense or could cause data loss.
In fact: it's for low-level data conversions, not for mapping business objects or models.
public struct Meters { public double Value { get; } public Meters(double value) => Value = value; public static explicit operator double(Meters m) => m.Value; } // Usage: Meters meters = new Meters(12.5); double metersAsDouble = (double)meters; // Explicit, not automatic!
The explicit operator is not meant for mapping business models. This isn't about performanceβ
it's about clarity of intent and maintainability. Use explicit operators for tight type conversions, not for business object mapping.
β Clean Code: Keep It Simple & Obvious
If Uncle Bob saw your code, he'd want mapping to be obvious and easy to find. No tricks, no surprises. The best way? A simple static method or a clear constructor that tells you exactly what's happening:
public class Dto { public int Id { get; set; } public string Name { get; set; } public static Dto FromDomainModel(DomainModel model) { return new Dto { Id = model.Id, Name = model.Name }; } } // Usage: var dto = Dto.FromDomainModel(domainModel);
Or just a regular constructor on your DTO. No magic, no guessing. If mapping ever gets more complex or needs to be reused across your app, that's when you move the logic to a dedicated Mapper
class.
Keep it simple, keep it readable.
π The Takeaway
Don't jump on every new C# feature just because it's there. If it doesn't make your code better, just skip it. So think twice and ask yourself: "Does this really make my code better?". Classic solutions win: your team and your future self will thank you!
#CSharp #CleanCode #Mapping #DotNet #SoftwareCraftsmanship #BestPractices #ExplicitOperator #BestPractices #SoftwareDevelopment #ProgrammingTips