I think all your intuitions are right.
No no. Static or instance.
This is a design choice (and there are many there). I am pragmatic, so I am considering a design template that generates spaguethi code for poor template design. But a bad design pattern for a project can be a good design pattern for another project. Try reading the Sample First Design book.
Yes, there are interfaces and abstract classes.
A few more thoughts:
I do not think that static methods or classes should be avoided. What to avoid is to skip using a static method or class, for example, skip using everything inside the language. But it’s very difficult to determine what to skip using static, and because static methods or classes are especially dangerous, people like to talk to avoid the static keyword at all. The static method will be in memory if you do not finish your application, so if you do not establish a connection inside the static method, you will have a very bad day.
I have a utility project, and there is a data class inside the utility project. The data class provides access to the database. This is a static class. Why?
Well, firstly, it is static, because the connection string comes from webconfig. Therefore, I have a static constructor (it runs once when the application starts and the class is mentioned) that reads webconfig and stores the string in a static private member variable. I think this is much better than reading the webconfig file and creating a variable with a range of 10 billion once a day. The methods are static, because they are quite simple, which means that they don’t need a large configuration to work, they just need a few parameters, and they are used only in the data access project. All users of my site use the same instance of the method (static), but each uses a static method with different parameters, so they get different answers from it (they share the channel, but drink different water). This is only necessary in order to clear everything (dispose of each instance with an area), because if you do not, they will remain in memory. And finally, my business is about data manipulation, not a static data class means much more memory usage than static (CPU usage is almost the same for both patterns).
public abstract class Data { [...] static Data() { #if DEBUG _Connection = ConfigurationManager.AppSettings["debug"]; #endif #if RELEASE _Connection = ConfigurationManager.AppSettings["release"]; #endif [...] } [...] }
At the end of the day, I use static when:
- If it's simple enough (so that I can control every aspect),
- If it is small enough (I use extension methods for verification, and they are static) and;
- If it is heavily used.
In addition, I use layers to organize my project (poco + factory pattern). I have a utility project, then an entity model project, then an access project, then a business logic project, then a website, api, manager, etc. The classes in the utility project do not interact with each other, but the classes inside the project are entity models (a class can have an instance of another class inside it). The entity model project does not interact with the utility project because they have the same level, they interact with each other at a different level in the access project, but it is more intuitive in the data processing project.