1

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.

  1. I am using the Patterns/Practices Enterprise Library 5.0 for the data access blocks (and caching and so on).

  2. 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.

Dav.id
  • 2,757
  • 3
  • 45
  • 57
  • how would you handle data connections with static classes? (concurrency). would you use a singleton? – RPM1984 Nov 03 '10 at 00:00
  • Sorry I should have mentioned, rightly or wrongly, my classes simply return data (in the first instance) to an AJAX frontend.. essentially disconnected.. i.e. I am not holding any connections open.. then as I need to update the data for example, that is a separate process.. the app I am writing is totally disconnected in that nature at least. – Dav.id Nov 03 '10 at 00:16

1 Answers1

1

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.

Community
  • 1
  • 1
jasper
  • 3,424
  • 1
  • 25
  • 46
  • Hey thanks for the post.. yes gut feeling was to use instances.. just tempting to use static to reduce the need to instantiate to call what is essentially a "fire and forget" process. However not knowing what is down the track makes instances probably the choice.. thanks for posting!! and thanks for the link to the other SO post too! – Dav.id Nov 03 '10 at 00:23
  • Actually that link to the other post was excellent! the accepted answer was great, really explained what I needed!! thank you again!! – Dav.id Nov 03 '10 at 00:31