I’m currently having a Class named “SqlData”, which contains all the Methods needed to Connect to my MS-SQL Database.
It contains methods for Inserting, Deleting and Updating different kinds of tables – and therefore is used in many Windows of my WPF application.
Let’s say that nearly 90% of my WPF-Windows are calling at least three Methods of my SqlData Methods for Loading, Inserting and Updating different records…
At the moment, I need to instantiate my Sql-Class in every Window – therefore I’m thinking of making the entire Class static so I don’t need to instantiate it every time?
But also I’ve read not to use static classes while communicating with external Servers like WebServices or Databases.
Could you give me any advice on how I should go on?
Following a few Methods used in my Class (bool returns true, when the statement completed, otherwise false):
public DataTable GetAllSomething(DataTable _data)
public bool WriteSomething(Object something, out int insertedId)
public bool DeleteSomething(Object something)
Thank you!
The time taken to instantiate a class in .NET is so ridiculously low that you should not be worried about. Personally I don’t use static classes because they introduce strong coupling between the different layers of an application making them more difficult to unit test in isolation.
So I prefer to abstract all database access behind an interface (or abstract class) and then provide an implementation of this interface against a specific database.