I have tried to trawl through an amazing amount posts – really valuable, because there are other aspects that you can absorb even though it’s not directly linked to the primary question.
In my case, as the title says, I really want to try to understand the “real world” pros/cons of using static classes/methods for data access.
A little more info of what I am using.
-
I am using the Patterns/Practices Enterprise Library 5.0 for the data access blocks (and
caching and so on). -
My actual “business object” is a static class, with a static method to say “getContacts”
in turn that returns say an XML string etc.
So the question really is
Given the structure above, is it “better/more efficient/good practice” to use a static class to return the XML string – again given that I do not need any instance relationship – just a one of “give me the data” and leave.. or is it more accepted to use an instance base class??
I realise the fact there is circumstances for each method, but just trying to get feedback to give me directions to pursue.
Thanks!
David.
Its not a dumb question at all.First I’ll point you to When to Use Static Classes in C#, as there is a lot of great advice there.
Using when to use a static method/class is really all down to the design of your application.
Even so, I would feel its better to use instances rather than just static methods for the long term design. If you’re ever going to use any OO concepts in your application, then static methods will limit you straight away. You can generally create a more flexible system using instances etc.
You also need to be very careful with using static methods in regard to static fields. I’ve seen plenty of examples of developers not understanding exactly what the difference is between a static field and an instance one, and its led to all sorts of problems.
So, to give you a straight answer, I would think you should try to avoid the static methods if you can, since in the long term, it will pay off by going down the OO route.