Static class or regular class with interface?
Hello,
I've been building a QR code generator for a while. It's a simple method call
QrCodeGenerator.Generate("data", options)
.
It is a static method/class, I was wondering if it would be better to supply the class trough an interface, making it available trough DI. Suppose it's going to be a nuget package, would it make a difference if it was supplied trough DI, instead of a static class?12 Replies
It depends
Like literally everything else
A static class is easy to access, but generally you should only do this for more basic helper methods and things that don't depend on eachother
A QR code generator sounds like it could use dependencies. At the very least logging.
Also, it's easier to test the generator when it's a DI injected class
Maybe one thing to also consider is if the generator persists a state. If it does it should probably not be static.
ergonomics-wise it might be more convenient to persist the configuration instead of having to pass it every time if you typically use the same configuration
There is no state at all
Right, this one makes a difference
you could have it both ways, a stored configuration but still allow passing one to override the stored one
That is what I currently have:
I guess my initial design can stay afterall
if you have stored configuration then it shouldn't be static imo
The class only contains values, like default colors
yeah that wouldn't be a static class then, as soon as there's mutable state i make them regular classes
(including the one storing the options instance)
I thought state is a class property which would make a class not thread safe
But I will modify it accordingly
Would this not be thread safe? Each call would create a new options instance
all my comments are related to persisting a configuration in the class as a property or field, yes
Some additional thoughts on this, just for more options for you:
Static classes and methods aren't incompatible with DI or testing! You could define a delegate matching the function signature and register that, and then inject it as you would an interface.
You could even simplify the delegate and "hide" the config to standardise it across your app
But as others say, keep this stateless if you keep it static.
you just read it
ofc it will be thread safe