Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have read so many conflicting opinions on the use of static methods that my head hurts. Consider the following:

In most of my CRUD applications I use a SqlDataHandler class to handle the interactions with the database for a class (or in some cases group of classes). See below:
C#
public abstract SqlDataHandler
{
    #region Properties
    protected static string ConnectionString { get; set; }
    #endregion

    #region Methods
    protected static DataTable GetDataTable(SqlCommand GetTableCommand)
    {
    	...
    }
    #endregion	
}

public AccountSqlDataHandler : SqlDataHandler
{
    #region Methods
    public static DataTable GetAccountHistory()
    {
    	SqlCommand getAccountHistoryCommand;
    	
    	...
    	
    	return AccountSqlDataHandler.GetDataTable(getAccountHistoryCommand);
    }
    #endregion
    
    #region Ctors
    static AccountSqlDataHandler()
    {
    	AccountSqlDataHandler.ConnectionString = "Connection string for account database";
    }
    #endregion
}

public Account
{
    #region Properties
    public List<HistoryItem> AccountHistory
    {
    	get
    	{
    		List<HistoryItem> accountHistory;
    		
    		accountHistory = this.getItemsFromDataTable(AccountSqlDataHandler.GetAccountHistory());
    		
    		return accountHistory;
    	}
    }
    #endregion
}

As I see it if I use member methods, then either I have to create an AccountSqlDataHandler instance each time, or create an AccountSqlDataHandler member in the Account class. I don't see any advantage to doing this, but from what I'm reading there is an advantage. I would like to understand what it is before I blindly change my methodology.
Posted
Updated 16-Jul-13 6:56am
v4
Comments
jemaritn80 16-Jul-13 11:21am    
I've read that, but I'm not sure I understand. Are you saying this is an appropriate use of a static member, since the users authentication status is the same regardless of the state of the Database object?
Sergey Alexandrovich Kryukov 16-Jul-13 13:04pm    
Do you really understand how static method work, in comparison to instance methods? You need to design code by yourself based on this understanding, instead of seeking for ready-to-use approach.
—SA
Maciej Los 16-Jul-13 13:32pm    
It's an answer!
My virtual 5!
jemaritn80 16-Jul-13 13:34pm    
I disagree. The question is not, "What is the best way to do this?" The question is, "Why is this way not the preferred way?" That question has not been answered.

1 solution

When I started working with VB6, I favored "modules" over "classes", and that's somehow similar, with modules corresponding to "static". The paradigm shift to classes took me some time. An important point was that I can have multiple instances of a class which do not interfere with each other: with "static", there exists only one single item in your whole application - and when you need that one item for two different purposes (in your example: you need to handle two different account histories in your application at the same time), then you'll get into trouble. At a first glance, that may not look like a really big issue, but with growing experience, you'll change the paradigm. We can try to explain, but in the end the enlightenment cannot be brought to you by others, it must come into existence within you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900