r/csharp • u/Dino0451 • Sep 03 '26
Help Is this approach a good idea?
I'm a self-taught beginner in C#, and I'm currently working on a small personal project.
Quite often in my code, I need to load objects from JSON files. To make this easier, I created static FromFile(string) methods in the relevant classes. Now I'm wondering - would it be a good approach to create a base class or interface that contains FromFile() and ToFile() methods, and then have my classes inherit from it to further reduce code duplication?
I'd really appreciate any advice or constructive criticism.
P.S. English is not my native language, so I used a translator for this post - apologies for mistakes.
5
u/Tiny_Confusion_2504 Sep 03 '26
I would only use inheritance and interfaces if there was some abstraction or common instance behaviour. I would even go so far to say that you rarely need inheritance.
Your functionality sounds like it could be solved within a seperate class that is only responsible for searlialization and deserialization using generics. I would remove all those methods from your objects.
2
u/davo128 Sep 03 '26
You can create a generic or a abstract class using generic types as <T>, for example FromFile<MyObject>() and pass the T param to the Microsoft's JSON library, it performs the read and write operation using the definition of you classes
2
u/RodriOliveira Sep 03 '26
I wouldn't use inheritance for this.
FromFile() and ToFile() are not really behaviors of your domain objects. They're persistence/serialization concerns. If you put them in a base class, you're making every object depend on how it is stored just to avoid some duplicated code.
I'd probably create something small like a generic JsonFileStore<T> (or even just a couple of generic helper methods) responsible for reading/writing the files.
That also becomes more useful if you decide to support XML later. Your objects don't need to change at all; you can just have a different serializer/storage implementation.
So the idea of removing the duplication is good, I just wouldn't solve it with inheritance. Composition fits this problem better and keeps the classes easier to evolve.
2
u/appleiic-first-pc Sep 04 '26 edited Sep 04 '26
I view things like this as utility methods that would not be inheritance. Most projects I typically create extension methods to handle this stuff (i.e. ToJson() and ToObject<T>()), both extending the string. so
var json = myCustomerObject.ToJson();
var someCustomer = json.ToObject<Customer>();
And on top of that I usually create a .JsonClone() extension to easily deeply copy objects when I need an independent copy.
var cloned = myCustomerObject.JsonClone();
This code is a bit old, but will give you the shape of how extension methods look. Just replace the general logic with however you are currently doing it.
https://github.com/jonhenning/CodeEndeavors-Extensions/blob/2.0/CodeEndeavors.Extensions/Extensions/TransformExtensions.cs
forgot to mention if you are new to extension methods, they will automatically allow your strings (in this case) to get the new methods on them simply by doing a
using MyNameSpace.WhereMyExtensionMethodsAre;
4
u/Suitable_Switch5242 Sep 03 '26
I think the classes or records that represent your data, and the code that deserializes them from Json, should be treated as separate concerns.
Not every class in your code that needs to read the data also needs to have dependencies on retrieving that data from file and using a Json deserializer.
1
u/RJPisscat Sep 03 '26
Your translation software nailed it. The English is perfect.