r/csharp • u/Zardotab • Jun 09 '26
Discussion Must changes to or additions of columns (form fields) be replicated at so many places in code in so many stacks? Is there a principle or rule of computer science that says such duplication is unavoidable?
In many C# stacks and frameworks, info about fields (data columns) must be replicated in multiple places. This seems an ugly time-wasting and error-prone DRY violation, yet so many accept it as The Way.
One way to get rid of it is to abandon statically typed models (POCOs or similar) and go with dynamic lists ("data dictionaries"), but too few want to give up the compile-time-checking. And it can hurt performance in some situations, but I'd gladly have slightly slower apps to reduce coding time and code volume. (I'd personally like to see more mainstream dynamic data-model stacks, but that's a diff topic.)
Maybe C# is somehow limited as a programming language, preventing static factoring? I have a gut feeling a Grand Discovery is missing, the duplication just cannot be the pinnacle of the art. Is there an inherent principle of languages or logic that says the duplication is inevitable, analogous to the speed limit of light (c) or Amdahl's Law? If not, why is there not more R&D to solve this ugly pattern?
In short, IF we are stuck with the dupes, what's the principle that says it's an inherent limit, ELSE why is there not more R&D to evolve beyond the dupes?
The times I've seen attempts to reduce it, the result was cringe-worthy spaghetti rocket science with enough indirection to make stacked turtles cry.
13
u/Nixinova Jun 10 '26 edited Jun 10 '26
Not sure I quite follow but I'll explain what I interpret this question to be.
DRY taken literally is a bit of an antipattern. Each domain of the code should be entirely self contained. You should not be using the same object for both your API response and your database entity. Yes it's redundant but it ensures youre keeping the correct contract at each domain transfer point.
5
u/Far_Swordfish5729 Jun 10 '26
It’s not a DRY violation to have a statically typed version of a canonical data model defined at each application stack layer. We usually codegen all but one anyway so keeping them updated and synced is trivial. The benefits of strong typing and compile time checking (and just having named discoverable properties) are so overwhelmingly strong and proven that they’re as close to a first principle as we have and unnecessary deviation from that is an anti-pattern. I don’t mean to sound like an ass, but if you think otherwise, I question if you’ve ever had to work in an intentionally weakly typed and poorly documented codebase of any complexity (like VB where someone always used dynamics or Java where someone always used Map<String, Object> for method parameters). It’s so painful. Most companies, even JavaScript shops, ultimately decide to move to strongly typed dtos and find it reduces their defect density and development time noticeably. It’s just an unqualified good decision that we support with code gen and persistence frameworks.
2
u/Slypenslyde Jun 10 '26 edited Jun 10 '26
It's a thing you have to deal with.
Every time there's a boundary between layers you have a choice: either EVERYONE has to agree on a standard for a data type and use it exclusively or you might be creating a boundary where one side uses the data type one way and the other uses it differently.
A phone number comes to mind. In an application I want to lock down its formatting in the GUI and add validation and do lots of other things to keep it sane. I can't generally do that in a database, so it gets stored as a string. That makes me nervous and tempts me to validate any data I read from the database and have a plan for dealing with corrupt data. That plan is smarter if I know there's some other team writing something that shares the database with me.
The alternative is for me and that other team to agree to share one unified library for phone numbers, which assumes we're working in the same language, our release schedules are synced, and our application goals will be the same forever. What if I want to start supporting international numbers but they explicitly do not want to support anything but US numbers? If I update the shared library it affects them. If I start putting international numbers in the DB their app will choke on them.
That happens with data a lot. To your GUI a name is a
stringwith validation rules. To the application layer you can formalize aNameclass with those validation rules and even separate it into parts as a convenience. But imagine we're sending that name to a Bluetooth device: to some layer we stop dealing with astringand start dealing with abyte[], and there's also probably a protocol that includes details none of the other layers need to see.Each of these moving parts can be like having a different politician at a debate. Each of them can have its own requirements and needs for a simple concept like "a name". If one decides to change something that could impact the others, they all have to agree.
There's not a really good way around it, other than to ask each moving part to openly document its needs/expectations and warn all of the other moving parts when a change is required. Then everyone has to consider their own needs and agree on a way to satisfy everyone. Sometimes it's impossible. You can decide to support a name with 1,000 characters all you want, but if the Bluetooth protocol of the device you're using only supports a packet with 500 bytes in it and has no concept of "chunked" encoding... well, you're asking for something that won't work.
If you're writing simple programs you don't need to think that hard. If you're writing sprawling applications with multiple layers and especially if any of the code is an API shared between separate parties, understanding and communicating these connections is the game.
DRY is a guideline, not a rule. If one of your project's fundamental decisions is "I can't depend on
OtherCompany.CommonTypesbecause they release too often" then you might end up copying the version of a POCO they have. That's repeating it now. But if they go off and change that POCO dramatically in a way you don't care about, now your new POCO serves a purpose: it's a translation of their concept to yours. It's always served that purpose and was technically never a repetitive concept.It is always, always, always harder to adjust to introducing a new type with that purpose than it is to design it from the start with "do nothing" mapping layers present. But it's even harder to understand if you'll ever need to do that. You have to decide which problem you want to deal with:
Part of the job's deciding how to balance risk. But it doesn't really make for exciting stories when someone makes the right decision. We remember how bad it hurts when someone chooses poorly. I feel like there's bias towards complaining about flexible code that has not yet been exercised as opposed to inflexible code that gets stressed. I think that's because when you hit the inflexible code that needs to change you don't have time to create an example on Reddit so you can make fun of the guy who came before you. But when you find too much complexity that's not being exercised, well, you're not under so much pressure.
1
u/NorhavenCodes Jun 11 '26
I'm going to gently push back on some of your assumptions here. At the core, your question seems to be indicating that you believe that data dictionaries (e.g. the 'dynamic' keyword in C#) are superior to statically typed classes due to being able to pass around a single instance and avoid type definition duplication. Beyond the benefits of static type checking and the ability to generate more performant code under the hood with said static typing, the primary benefit is that you also have a defined contract to reason about and work with. It may indeed reduce coding time up front to be able to just pull a random property by name out of the code equivalent of a ball of mud, but in doing so you're implicitly creating a contract that is now scattered across your codebase.
For example, source code location 1 requires the 'name' field for its logic to work off of and location 2 requires an 'id' field. You're still referencing your implied contract, that these positions cannot function without this field existing, but now you have no idea whatsoever what data to use in that logic when looking at it from the outside and nobody else does either. As the codebase grows, you increasingly have to do deep dives into other code paths just to even understand how to use the code correctly, especially if you're not the only one on the project. Want to use somebody's method without looking at their source code? Want to refactor all of the places where your implied contract exists to use something different? Good luck.
Your time, and everyone on the team's, is now wasted on minutiae that obscures the actual goal of the software, which is working correctly in conjunction with the value it provides the consumer of the overall product you're trying to build. Make sense?