Closed archanyhm closed 1 year ago
This is a fair point. I was trying to copy the python version a bit too closely there. Do you suggest a non-static class with static data? Or some other, single place where the Game object instance is kept?
Refactored Game to be a non-static object, configured as a service and inserted into the ViewModels via the same method the router is. I'm not very familiar with these codding patterns, so I almost certainly did something less than optimal (game should probably be interface-based, from what I've read). But it works, so I'm counting that a victory.
https://github.com/keyraven/ClanGen-CSharp/blob/f155ae5f56ee7b8cec15dd9b49b8d7d0bfccd882/Clangen/Models/Game.cs#L26
One of the biggest no-nos in modern development probably relates to global (and static) state. Especially static initialization is (one) root of big evil when things potentially cause the program to crash or otherwise suffer from unexpected behaviours because something goes wrong when including some file that does static initialization. Even if you only have one instance, ever (unless you have very strong arguments in favour of singletons), don't make them static.
Static classes with static data are usually pretty unbearable to test and you don't really want state to be manipulated from anywhere in the code without any sort of direct dependency. OOP is supposed to help with encapsulation and decoupling components; this is the polar opposite. Relying on some other classes static state means code is coupled very tightly.