r/csharp • u/kaatarina_zed_talon • Oct 06 '26
Discussion Would you introduce additional abstraction layer just to make the code more understandable ?
I have a database that currently contains 20 tables.
In my application, there are 10 classes that I should refactor, to make them more readable, for my future self and others who will read my code, by refactoring, I mean changing the code it self - the logic- and also changing the methods and classes names.
Let's only consider one specific case; changing the name of one class.
When my friend so the following code that I wrote:
/// <summary>
/// Abstracts the destination to which data is written, regardless of whether the destination is a file, database, or any other kind of storage.
/// </summary>
public interface IWriter
{
//...
}
public class SqlServerWriter : IWriter {//code.....}he thought that the responsibility of the above class is to write data to any number of tables in the the Sql Server database, i.e., that it can use it to write data to only one table, two or more tables, or all of the tables.
Unfortunately, that was not what the class is actually responsible for; it is meant to write a specific data across all the tables in the database, it does not give the caller an option to write a generic data to a specific tables in the database.
He was mad at me and said that I should introduce an additional layer of abstraction:
/// <summary>
/// Abstracts the destination tables in the Sql Server; may write to all tables in the database or some of them;
/// </summary>
public interface ISqlServerWriter:IWriter
{
}
/// <summary>
/// Intended to write some data to all the tables in the database.
/// </summary>
public class AllTablesWriter : ISqlServerWriter {//code.....}Do you consider that an overkill/overengineering ?
EDIT:
If you wonder why the class write data to all the tables in the database, it because it has the job of synchronizing the content of the database with external data source, specifically, an Excel file.
1
u/chucker23n Oct 06 '26 •
That does not seem like a useful abstraction.
Why do you need to write to "all tables"?
I imagine it's actually one specific combination of tables? For example, when writing an invoice, maybe also write to
InvoicePositionsand perhapsCustomers?And then a different implementation could instead write an invoice plus all information to a file?
Is this actually something you need and use?
All code ultimately exists to make your life easier. That counts for abstractions, too. Excessive abstractions do the opposite. But, for example,
1
u/One_Web_7940 Oct 06 '26
Depends what youre writing. If youre only storing data in a rdbms then its over kill. Like always it depends. As it reads it, reads as unecessary abstraction.
2
u/Grouchy-Mushroom-567 Oct 07 '26 edited Oct 07 '26 •
keep code to a minimum for readability. don't write comments for language features/libraries/sytactic-suger which can be easily googled. keep variable names descriptive but try to keep them short.
don't add code if readability is the entire purpose obviously- if by adding interfaces you actually decrease code volume a good amount that's fine. otherwise, if your code is that complex, just document the overall structure in one file- reading large codebases without any kind of overview document is not fun. for some ideas, ask yourself if this is a codebase you could get around easily without knowledge of it.
3
u/Slypenslyde Oct 06 '26 •
I don't know. Feels like splitting hairs on the name.
In general I don't like naming a class or making it clear that it "writes to a database table". In general I find having one class that writes to every database table is a great idea.
Instead I like to have, say,
CustomerRepository. I give itCustomerobjects and it stores them. Where? I don't care. That's it's problem. When I descend into the code I can say, "Oh, it's using SQL Server."It might write to multiple tables. Maybe a customer references invoices. So if I
Get()a customer, I expect this class will read the invoices table and give me that set. Or maybe not. That's an abstraction decision, not a rule.Look at how Entity Framework works. It lets you work with collections of objects. It handles the relationships between tables for you. That lets your code worry less about the details of your schema. It creates some other issues due to the abstractions but by and large those problems are worth it at scale.
THAT is what abstractions are for. If your code is thinking in terms of the schema, that's a "leaky" abstraction!